简体   繁体   中英

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

here is my aspx view

            <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

If you are looking for Linkbutton object, thenyou can use FindControl method as:

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 . If you need all Linkbutton instances, then you can loop through the datarows to get each linkbutton ids.

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;
      }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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