ASP.NET Gridview ButtonField onclick会触发包含行的onclick事件

时间:2014-01-15 07:05:00

标签: c# javascript jquery asp.net gridview

我有一个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>

2 个答案:

答案 0 :(得分:1)

尝试使用此

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

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

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

答案 1 :(得分:0)

如果您不想要单独的标题,请尝试在同一个<asp:TemplateField>添加该按钮

<Columns>
  <asp:TemplateField>
    <ItemTemplate>

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

     </ItemTemplate>
 </asp:TemplateField>
</Columns>

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