如何在gridview2 - asp.net中的button1click上获取gridview1中的链接按钮ID

时间:2013-06-18 07:44:44

标签: c# asp.net onclick asplinkbutton aspbutton

我已经放置了两个gridview控件,其中我有按钮。我在gridview1中有linkbutton,在gridview2中有button1。 我需要在button1上点击grdiview2中的linkbutton id。

这是我的代码片段:

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

                                                                                                    

这是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>

服务器端代码:

我试过这个但没有运气。!

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

需要帮助。 三江源。

1 个答案:

答案 0 :(得分:0)

试试这个

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

您的代码失败的原因是您将网格视图转换为无法使用的链接按钮。