How to parse data from Gridview row to another pag

2019-09-20 06:06发布

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

1条回答
The star\"
2楼-- · 2019-09-20 06:13

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;
查看更多
登录 后发表回答