0

How to add Link Button In dynamically in Autogenerated Gridview and How write onclick event for that link button.

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

Binded Datas to this gridview dynamically..the Header and data's changes everytime.

Need to bind all the datas in link button and to write code for that button click.

Please help.........................

Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
Hari
  • 11
  • 1
  • 11

1 Answers1

0

You don't need to add a link button.
You need to add

  1. DataKeys, a comma separated list of fields from GridView4 Dataset
  2. Enable row selection
  3. Add a row selection handler

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

Alternatively, the above can all be done in the code behind.

Selecting a row will fire the event handler where you retrieve the data Keys

    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();
    }
fnostro
  • 4,531
  • 1
  • 15
  • 23
  • Even in a fully dynamic scenario, if *"Grid2"* is to be populated by selections from *"Grid1"* there needs to be some agreement, at least on data type and field position in order to populate the second grid – fnostro Dec 12 '16 at 17:52