如何在Autogenerated Gridview中动态添加链接按钮

时间:2016-12-09 05:45:34

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

如何在Autogenerated 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>

动态地将数据绑定到此网格视图...每次都会更改标题和数据。

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

请帮助.........................

1 个答案:

答案 0 :(得分:0)

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

  1. DataKeys,GridView4 Dataset
  2. 中逗号分隔的字段列表
  3. 启用行选择
  4. 添加行选择处理程序

    <asp:GridView ID="GridView4" runat="server" 
                  AutoGenerateSelectButton="True"
                  OnSelectedIndexChanged="GridView4_SelectedIndexChanged"
                  DataKeyNames="XXX,YYY,ZZZ">
    
       ...
    </asp:GridView
    
  5. 或者,上述所有内容都可以在后面的代码中完成。

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

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