I am binding a grid with DataTable, there are two columns which I am using, and the result in GridView1 is is,
HostelName | HostelCode
Alpha | 1
Bravo | 2
Charlie | 3
Now I want this HostelCode as a LinkButton for all the records which are in database, so that I can do further actions while clicking LinkButton. Any help ??
I am using this code, but its not working,
for (int i = 0; i < dt.Rows.Count; i++)
{
LinkButton lb = new LinkButton();
lb = (LinkButton)GridView1.SelectedRow.FindControl("lbtnSelect");
lb.Text = dt.Rows[1].ToString();
}
lbtnSelect is the ID of my linkbutton.
You can use link button in template field of gridview and Eval function to bind value in linkbutton in aspx page.
<asp:GridView runat="server" ID="gvrecords" CssClass="Gridview" DataKeyNames="HostelCode"
AutoGenerateColumns="false" HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White"
OnRowDataBound="gvrecords_RowDataBound">
<Columns>
<asp:BoundField DataField="HostelName" HeaderText="Hostel Name" />
<asp:TemplateField HeaderText="Hostel Code">
<ItemTemplate>
<asp:LinkButton ID="lnkbtn" runat="server" OnClick="lnkbtn_Click" Text='<%#Eval("HostelCode")'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>