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:
I would like to achieve this when I click on the send details hyperlink:
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.
Image of the Label with the id=name:
My label DOES contain runat="server":
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…