【问题标题】:ASP.NET Gridview ButtonField onclick fires containing row's onclick eventASP.NET Gridview ButtonField onclick 触发包含行的 onclick 事件
【发布时间】:2014-01-15 07:04:59
【问题描述】:

我有一个 gridview 行,单击时必须执行回发 A,而该行中有一个按钮域,单击时必须执行回发 B。问题是,当我单击按钮域时,event1 和 event2 都会被触发。下面是代码。

protected void gdv_RowCommand(object sender, GridViewCommandEventArgs e)
{
    string arg = Convert.ToString(((System.Web.UI.WebControls.CommandEventArgs)(e)).CommandArgument);

    if (e.CommandName == "Command1")
    {
        doEvent1(arg);
    }
    else if (e.CommandName == "Command2")
    {
        doEvent2(arg);
    }
}

protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {            
        LinkButton b1 = (LinkButton)e.Row.Cells[0].Controls[0];
        matchesButton.CommandArgument = arg1;

        LinkButton rowLink = (LinkButton)e.Row.Cells[1].Controls[1];
        rowLink.CommandArgument = arg2;

        e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(rowLink, "");
    }
}

这是gridview的asp代码

<Columns>
    <asp:ButtonField DataTextField="f1" HeaderText="H1" CommandName="Command1" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:LinkButton ID="btn1" runat="server" Text="" CommandName="Command2" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

【问题讨论】:

    标签: c# javascript jquery asp.net gridview


    【解决方案1】:

    尝试使用这个

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Command2")
        {
           // Your Code here
        }
    }
    

    要在网格视图中查找控件,请使用此代码

    LinkButton lnkbtn= (LinkButton)e.Row.FindControl("btn1");
    

    【讨论】:

    • 你不能像这样使用FindControlGridViewCommandEventArgs。关注此answer,了解如何正确访问FindControl
    【解决方案2】:

    如果您不想要单独的标题,请尝试在同一 &lt;asp:TemplateField&gt; 中添加两个按钮

    <Columns>
      <asp:TemplateField>
        <ItemTemplate>
    
           <asp:Button ID="button" runat="server" CommandName="Command1" />
           <asp:LinkButton ID="btn1" runat="server" CommandName="Command2" />
    
         </ItemTemplate>
     </asp:TemplateField>
    </Columns>
    

    如果您想要单独的标题,请创建两个单独的 &lt;asp:TemplateField&gt;,然后在其中添加按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-30
      • 2013-03-12
      • 2016-03-17
      • 2018-03-10
      • 2013-04-25
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多