简体   繁体   English

如何获得button2上的Button1 ID单击服务器端ASP.NET C#

[英]How to get Button1 Id on button2 click on server side asp.net c#

here is my aspx view 这是我的aspx视图

            <asp:GridView ID="gvdatasubcategory" runat="server" AllowPaging="false" AllowSorting="false"
            CssClass="gvdatarow" ShowHeader="false" AutoGenerateColumns="False" OnRowCommand="gvdatasubcategory_RowCommand">
            <Columns>
                <asp:TemplateField ItemStyle-Font-Names="Estrangelo Edessa" HeaderStyle-Font-Names="Estrangelo Edessa">
                    <ItemTemplate>
                        <div class="subcategory_type">
                            <div id="abd" runat="server">
                                <asp:LinkButton ID="button1" runat="server" CssClass="subcategory_name"
                                    Width="80px" Height="26px" Text='<%#DataBinder.Eval(Container.DataItem, "SubCategory")%>'
                                    CommandName="Test"></asp:LinkButton>
                            </div>
                        </div>
                    </ItemTemplate>
                    <HeaderStyle Font-Names="Estrangelo Edessa" Width="5px" />
                    <ItemStyle Font-Names="Estrangelo Edessa" Width="5px" Wrap="false" HorizontalAlign="Center" />
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
<asp:Button ID="button2 " runat="server" CssClass="category_name" Text="getid"
                                OnClick="button2 _Click" />

these buttons are in gridview and i need to get the first button id on second button click in code behind Thank you in advance Raveendra 这些按钮在gridview中,我需要获取第二个按钮上的第一个按钮ID,并在后面的代码中单击。提前致谢Raveendra

If you are looking for Linkbutton object, thenyou can use FindControl method as: 如果要查找Linkbutton对象,则可以使用FindControl方法,如下所示:

LinkButton button1 = (LinkButton)gvdatasubcategory.Rows[0].Cells[0].FindControl("button1");
string buttonid = button1.ClientID; //gives client side id of the Linkbutton

But keep in mind that grid should contain th rows, otherwise you will get null . 但是请记住,grid应该包含第th行,否则您将得到null If you need all Linkbutton instances, then you can loop through the datarows to get each linkbutton ids. 如果需要所有Linkbutton实例,则可以遍历数据行以获取每个linkbutton ID。

You can try this code below: 您可以在下面尝试以下代码:

 protected void button2_Click(object sender, EventArgs e)
 {
      foreach(GridViewRow row in  gvdatasubcategory.Rows)
      {
          LinkButton btn = (LinkButton)row.FindControl("button1");
          string strClientID = string.Empty;
          strClientID = btn.ClientID;
      }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM