如何在asp.net中获取GridView单元格值 [英] How to get the gridview cell value in asp.net

查看:206
本文介绍了如何在asp.net中获取GridView单元格值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gridview如下

链接课程代码天数Startdt Enddt

单击此处ASM CC 5 2015年5月10日2015年5月15日
2015年5月21日2015年5月25日

当我单击gridview中的链接按钮时,我想获取该课程的Course(ASM),如下所示:

受保护的void LinkBut​​ton2_Click(对象发送者,EventArgs e)
{
for(int i = 0; i< gvRank.Rows.Count; i ++)
{
字符串Course = gvRank.SelectedRow.Cells [1] .Text;
Session ["course"] =课程;
{
Response.Redirect("Eligibility.aspx");
}
}
}

但是当我调试并检查以下行时,如下所示

字符串Course = gvRank.SelectedRow.Cells [1] .Text;

在上面的行中,不会检索课程名称.

请帮我上面的代码有什么问题

我尝试过的事情:

Gridview如下

链接课程代码天数Startdt Enddt

单击此处ASM CC 5 2015年5月10日2015年5月15日
2015年5月21日2015年5月25日

当我单击gridview中的链接按钮时,我想获取该课程的Course(ASM),如下所示:

受保护的void LinkBut​​ton2_Click(对象发送者,EventArgs e)
{
for(int i = 0; i< gvRank.Rows.Count; i ++)
{
字符串Course = gvRank.SelectedRow.Cells [1] .Text;
Session ["course"] =课程;
{
Response.Redirect("Eligibility.aspx");
}
}
}

但是当我调试并检查以下行时,如下所示

字符串Course = gvRank.SelectedRow.Cells [1] .Text;

在上面的行中,不会检索课程名称.

请帮我上面的代码是什么问题

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

推荐答案

请找到下面的代码来解决您的问题,

Aspx:

Please find below code to resolve your problem,

Aspx:

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

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,


这篇关于如何在asp.net中获取GridView单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆