0

我已经放置了两个 gridview 控件,其中有按钮。我在gridview1 中有linkbutton,在gridview2 中有button1。我需要在 grdiview2 中单击 button1 上的链接按钮 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;
    }
 }

需要帮忙。谢谢你。

4

1 回答 1

0

尝试这个

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

您的代码失败的原因是您将网格视图投射到一个不起作用的链接按钮上。

于 2013-06-18T16:38:49.100 回答