【问题标题】:How to add Link Button In dynamically in Autogenerated Gridview如何在自动生成的 Gridview 中动态添加链接按钮
【发布时间】:2016-12-09 05:45:34
【问题描述】:

如何在自动生成的 Gridview 中动态添加链接按钮以及如何为该链接按钮编写 onclick 事件。

<asp:GridView ID="GridView4" runat="server" BorderColor="#3366CC" BorderStyle="None"
                            BorderWidth="1px" CellPadding="4" ShowHeaderWhenEmpty="True" Width="996px" HeaderStyle-Wrap="false"
                            ItemStyle-Wrap="false" OnRowDataBound="GridView4_RowDataBound">

                            <PagerSettings Mode="NextPreviousFirstLast" FirstPageText="First" LastPageText="Last"
                                NextPageText="Next" PreviousPageText="Previous" />
                            <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                            <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" Height="10px" />
                            <RowStyle ForeColor="white" HorizontalAlign="Center" Font-Names="Microsoft Sans Serif" />
                            <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                            <SortedAscendingCellStyle BackColor="#EDF6F6" />
                            <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                            <SortedDescendingCellStyle BackColor="#D6DFDF" />
                            <SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>

动态绑定数据到这个gridview..每次标题和数据的变化。

需要绑定链接按钮中的所有数据,并为该按钮的点击编写代码。

请帮忙............

【问题讨论】:

  • 要为什么动作添加按钮?
  • 我想从 gridview 中获取数据,并根据我想将值绑定到另一个网格中的值。

标签: c# asp.net gridview web-applications asplinkbutton


【解决方案1】:

您无需添加链接按钮。
你需要添加

  1. DataKeys,来自 GridView4 数据集的以逗号分隔的字段列表
  2. 启用行选择
  3. 添加行选择处理程序

    <asp:GridView ID="GridView4" runat="server" 
                  AutoGenerateSelectButton="True"
                  OnSelectedIndexChanged="GridView4_SelectedIndexChanged"
                  DataKeyNames="XXX,YYY,ZZZ">
    
       ...
    </asp:GridView
    

或者,以上都可以在后面的代码中完成。

选择一行将触发您检索数据键的事件处理程序

    protected void GridView4_SelectedIndexChanged( object sender, EventArgs e )
    {
        // Retrieve data from selected row
        String field1 = (String) GridView1.SelectedDataKey.Values[ "XXX" ];
        int field2 = (int) GridView1.SelectedDataKey.Values[ "YYY" ];
        double field3 = (double) GridView1.SelectedDataKey.Values[ "ZZZ" ];

        PopulateYourOtherGridviewDataSource(field1, field2, field3);
        GridViewOther.DataBind();
    }

【讨论】:

  • 即使在完全动态的场景中,如果 "Grid2" 将由来自 "Grid1" 的选择填充,则需要达成一些协议,至少在数据类型和字段位置上以填充第二个网格
猜你喜欢
  • 2014-12-10
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-15
  • 1970-01-01
相关资源
最近更新 更多