Click here to Skip to main content
15,944,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Gridview as follows

Link Course Code Days Startdt Enddt

Click here ASM CC 5 10 May 2015 15 May 2015
21 May 2015 25 May 2015

When i click the link button in gridview i want to get Course(ASM) for that i written code as follows

protected void LinkButton2_Click(object sender, EventArgs e)
{
for(int i= 0; i < gvRank.Rows.Count; i++)
{
string Course = gvRank.SelectedRow.Cells[1].Text;
Session["course"] = Course;
{
Response.Redirect("Eligibility.aspx");
}
}
}

But When i debug and check in below line as follows

string Course = gvRank.SelectedRow.Cells[1].Text;

In the above line course name is not retrived.

please help me what is the problem in my above code

What I have tried:

Gridview as follows

Link Course Code Days Startdt Enddt

Click here ASM CC 5 10 May 2015 15 May 2015
21 May 2015 25 May 2015

When i click the link button in gridview i want to get Course(ASM) for that i written code as follows

protected void LinkButton2_Click(object sender, EventArgs e)
{
for(int i= 0; i < gvRank.Rows.Count; i++)
{
string Course = gvRank.SelectedRow.Cells[1].Text;
Session["course"] = Course;
{
Response.Redirect("Eligibility.aspx");
}
}
}

But When i debug and check in below line as follows

string Course = gvRank.SelectedRow.Cells[1].Text;

In the above line course name is not retrived.

please help me what is the problem in my above code
Posted
Updated 28-Feb-16 23:29pm
Comments
[no name] 29-Feb-16 2:33am    
Provide aspx code gridview gvRank because we are not sure what exactly your link is. Secondly provide also code-behind code when a link clicks.

1 solution

Please find below code to resolve your problem,

Aspx:

ASP.NET
<div>
            <asp:gridview runat="Server" id="gvRank" autogeneratecolumns="False" autogenerateselectbutton="True" onselectedindexchanged="gvRank_SelectedIndexChanged" style="z-index: 100; left: 266px; position: absolute; top: 71px" cellpadding="4" forecolor="#333333" gridlines="None" width="306px" height="137px" xmlns:asp="#unknown">
                <columns>
                    <asp:boundfield headertext="Rowid" datafield="Rowid" />
                    <asp:boundfield headertext="Name" datafield="Name" />
                    <asp:boundfield headertext="Marks" datafield="marks" />
                </columns>
                <footerstyle backcolor="#507CD1" font-bold="True" forecolor="White" />
                <SelectedRowStyle BackColor="Teal" ForeColor="Maroon" Font-Bold="True" />
                <pagerstyle backcolor="#2461BF" forecolor="White" horizontalalign="Center" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <alternatingrowstyle backcolor="White" />
                <rowstyle backcolor="#EFF3FB" />
                <editrowstyle backcolor="#2461BF" />
            </asp:gridview>

            <br />
            <br />
            <asp:textbox id="txtrowid" runat="server" style="z-index: 101; left: 365px; position: absolute; top: 251px" xmlns:asp="#unknown"></asp:textbox>
            <asp:textbox id="txtname" runat="server" style="z-index: 102; left: 365px; position: absolute; top: 283px" xmlns:asp="#unknown"></asp:textbox>
            <asp:textbox id="txtmarks" runat="server" style="z-index: 103; left: 365px; position: absolute; top: 315px" xmlns:asp="#unknown"></asp:textbox>
            <asp:label id="Label1" runat="server" style="z-index: 107; left: 305px; position: absolute; top: 253px" xmlns:asp="#unknown">
                Text="RowId"></asp:label>
            <asp:label id="Label2" runat="server" style="z-index: 105; left: 303px; position: absolute; top: 287px" xmlns:asp="#unknown">
                Text="Name"></asp:label>
            <asp:label id="Label3" runat="server" style="z-index: 106; left: 306px; position: absolute; top: 318px" xmlns:asp="#unknown">
                Text="Marks"></asp:label>

        </div>



Code Behind:

C#
public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gvRank.DataSource = GetData();
                gvRank.DataBind();
            }
        }

        private DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Rowid");
            dt.Columns.Add("Name");
            dt.Columns.Add("Marks");
            DataRow dr = null;
            for (int i = 0; i < 5; i++)
            {
                dr = dt.NewRow();
                dr["RowId"] = "RowId" + i;
                dr["Name"] = "Name" + i;
                dr["Marks"] = "Marks" + i;
                dt.Rows.Add(dr);
            }
            return dt;
        }

        protected void gvRank_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtrowid.Text = gvRank.SelectedRow.Cells[1].Text;
            txtname.Text = gvRank.SelectedRow.Cells[2].Text;
            txtmarks.Text = gvRank.SelectedRow.Cells[3].Text;

        }
    }


Thanks,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900