Question

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.

Was it helpful?

Solution

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>

OTHER TIPS

ASPX CODE

<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"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

CS CODE

public partial class Tests : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindHostelDetails();
        }
    }
    protected void BindHostelDetails()
    {
        gvrecords.DataSource = DBData();
        gvrecords.DataBind();
    }
    protected void gvrecords_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string HostelCode = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "HostelCode"));
            LinkButton lnkbtnresult = (LinkButton)e.Row.FindControl("lnkbtn");
            lnkbtnresult.Text = HostelCode;
        }
    }
    protected void lnkbtn_Click(object sender, EventArgs e) 
    {

        LinkButton lnkbtn = sender as LinkButton;
        GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
        int hostelcode = Convert.ToInt32(gvrecords.DataKeys[gvrow.RowIndex].Value.ToString());
        string HostelName = gvrow.Cells[0].Text;    
        Response.Write("<script> alert('" + "Hostel Name :"+ HostelName +" Hostel Code :"+ hostelcode + "'); </script>");
    }
    List<DTest> DBData()
    {
        List<DTest> _Dt = new List<DTest>();
        _Dt.Add(new DTest { HostelName = "Alpha", HostelCode = "1" });
        _Dt.Add(new DTest { HostelName = "Bravo", HostelCode = "2" });
        _Dt.Add(new DTest { HostelName = "Charlie", HostelCode = "3" });
        return _Dt;
    }
}
public class DTest
{
    public string HostelName { get; set; }
    public string HostelCode { get; set; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top