0

i have placed two gridview controls,in which i have buttons. i have linkbutton in gridview1 and button1 in gridview2. i need to get the linkbutton id on button1 click in grdiview2.

here is the snippet of my code:

     <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="lnkGridSubCategory" runat="server" CssClass='<%# "CategoryTab" + Eval("id") %>'
                                    Width="80px" Height="26px" Text='<%#DataBinder.Eval(Container.DataItem, "SubCategory")%>'
                                    CommandName="Test"></asp:LinkButton>
                            </div>
                        </div>

here is gridview 2:

    <asp:GridView ID="Categorygvdata" runat="server" AllowPaging="false" AllowSorting="false"
            CssClass="gvdatarow" ShowHeader="false" DataKeyNames="Id" AutoGenerateColumns="False"
            OnSelectedIndexChanged="Categorygvdata_SelectedIndexChanged">
            <HeaderStyle BackColor="#013a04" Height="25px" ForeColor="White" />
            <Columns>
                <asp:TemplateField ItemStyle-Font-Names="Estrangelo Edessa" HeaderStyle-Font-Names="Estrangelo Edessa">
                    <ItemTemplate>
                        <div class="category_type">
                            <asp:Button ID="Button1" runat="server" CommandName="FilterCategory" CommandArgument='<%# Eval("Id") %>'
                                CssClass='<%# "CategoryTab" + Eval("Id") %>' Text='<%# Eval("Category") %>' OnCommand="Button1_Click" />
                        </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>

server side code:

i have tried this but no luck.!

   protected void Button1_Click(object sender, CommandEventArgs e)
{
LinkButton GridView1 = (LinkButton)gvdatasubcategory.FindControl("Categorygvdata");



    foreach (GridViewRow row in gvdatasubcategory.Rows)
    {
        LinkButton btn = (LinkButton)row.FindControl("lnkGridSubCategory");
        string strClientID = string.Empty;
        strClientID = btn.ClientID;
    }
 }

Need help. Thankyou.

vaishnavi
  • 1
  • 1
  • 5

1 Answers1

0

Try this

LinkButton lnkGridSubCategory = (LinkButton)gvdatasubcategory.FindControl("lnkGridSubCategory");
foreach (GridViewRow row in gvdatasubcategory.Rows)
{   
    string strClientID = string.Empty;
    strClientID = lnkGridSubCategory.ClientID;
}

The reason why your code is failing is that you are casting a grid view to a link button which will not work.

abidmix
  • 1,748
  • 1
  • 16
  • 26