Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

How to parse data from Gridview row to another page using C# and ASP.NET

I would like to parse a row of data from Gridview in ASP.NET to a second page displaying contents of the row data from the previous page. My Gridview has already been linked to the database.

My current Gridview looks something like this:

enter image description here

I would like to achieve this when I click on the send details hyperlink:

enter image description here

If I click on the second row the data from that row will display in the next page

The following codes are what I had put under my link button itemTemplate:

<ItemTemplate>
       <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
       <asp:LinkButton ID="viewTours" runat="server" CommandName="view" CommandArgument='<%# Bind("name") %>' PostBackUrl='<%#"~/details.aspx?RowIndex=" + Container.DataItemIndex %>'>View</asp:LinkButton>
        </ItemTemplate>

This is my page load method from the second page where I want to load the data from.

     protected void Page_Load(object sender, EventArgs e)
    {

    if (this.Page.PreviousPage != null)
    {
        int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
        GridView GridView = (GridView)this.Page.PreviousPage.FindControl("GridView");
        GridViewRow row = GridView.Rows[rowIndex];
        name.Text = (row.FindControl("name") as Label).Text;
        //name.Text = row.Cells[0].Text; (this did not work either, i got the same error)
    }
}

However I get a squiggly line on name.Text saying that it does not exist, even though I do have a label with the id name in the design view of my html (second) page.

How can I make it such that I can parse data from the selected Gridview row to another page? Assuming that I can customize the second page and put the labels wherever I like.

This is the code from my gridview. As my binding has been done through the UI I dont have much codes for it, except to redirect the selected gridview row to another page.

    protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
{
    Session["tour"] = GridView;
    Response.Redirect("tourDetails.aspx");


}

I still get the error that the label text does not exist, when it actually exists in the page itself.

enter image description here

Image of the Label with the id=name:

enter image description here

My label DOES contain runat="server": enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are confusing binding data with structure. When you try to get a control with the name key, you really mean Label1, so change

name.Text = (row.FindControl("name") as Label).Text;

to

name.Text = (row.FindControl("Label1") as Label).Text;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...