【问题标题】:Get two click events on gridview在gridview上获取两个点击事件
【发布时间】:2014-01-26 17:38:51
【问题描述】:

我目前有 2 行代码会影响单击 GridView 中的一行。如果我将其中任何一个留在原地,它们就会自行工作。如果我将两者都放入 GridView1_RowDataBound 中的唯一一个有效。 GridView1_RowDataBound 方法用于在单击时更改 GridView 中的选定行。 GridView1_RowCreated 代码用于使点击使页面上元素的 ID 变得可见。我真的两个都需要。有没有办法把它们结合起来?

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
}

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
 {
      e.Row.Attributes.Add("onclick", "setVisible('cfilterpopup')");
 }

【问题讨论】:

    标签: c# javascript asp.net gridview onclick


    【解决方案1】:

    我会这样做:

    编辑: OnClientClick 不适用于 GridViewRow。您可以尝试以下方法:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
        e.Row.Attributes.Add("onclick", "setVisible('cfilterpopup');__doPostBack('" + GridView1.ClientID + "', 'Select$" + e.Row.RowIndex + "');");
    }
    

    说明:在第二行中,我们正在更改属性以注入我们的setVisible() 方法并调用__doPostBack()

    编辑2:现在有两个问题:

    1. cfilterpopup 将在回发时再次不可见。

    2. Onclick 属性在回发时被清除

    如何解决 #1:

    在标记中添加隐藏字段。在setVisible() 中将隐藏字段值设置为“1”。在 body onload 上添加一个方法来扫描这个值并使该部分可见/不可见。

    您的 javascript 标记可能如下所示:

    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function setVisible(itm) {
               document.getElementById('<%=hdnStatus.ClientID %>').value="1";
            }
    
            function showcfilterpopup() {
                var status = document.getElementById('<%=hdnStatus.ClientID %>').value;
                document.getElementById('cfilterpopup').style.visibility = status == "1" ? "visible" : "hidden";           
            }
        </script>
    </head>
    <body onload="showcfilterpopup();">
        <form id="form1" runat="server">
        <div>
            <div id="cfilterpopup" style="display:none">
                <p>cfilterpopup</p>
            </div>
            <asp:HiddenField ID="hdnStatus" Value="0" runat="server" />
            <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"></asp:GridView>
        </div>
        </form>
    </body>
    </html>
    

    【讨论】:

    • 我尝试了第一个。我第一次尝试时的错误是语法错误。现在单击它时,它不会设置 cfilterpopup Visible,但它确实为 GridView 设置了 selectedrow。代码e.Row.Attributes.Add("OnClientClick", "setVisible('cfilterpopup');"); 即使我注释掉另一个代码也不起作用。
    • 我已经编辑了我的答案。 OnClientClick 不适用于 GridViewRow。
    • 如此接近。这确实执行了两者,但不是我期望的方式。 cfilterpopup 会在一瞬间变得可见,然后又变得不可见。
    • 回发后变得不可见。如果你想保持它的状态,你可以将状态保存在一个隐藏的字段中,在 body onload 读取状态并设置可见性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多