I am trying to open other page and pass data for selected row in gridview to be displayed in that page when user click the select button. i want to display the data in text box. so that, user can make editing from that page. i dont know how to pass the data for that selected row to be displayed in other page. Can someone help me??

Dani AI

Generated

For the clearest, most maintainable pattern is to send only the primary key (ID) of the selected row to the edit page and have that page re-query the database and populate the textboxes. Storing an entire DataTable in Session makes the app heavier and risks stale data; sending the ID and loading the single record keeps memory use low and data consistent.

Example GridView pattern (store PK in DataKeyNames and use RowCommand to redirect):

<asp:GridView ID="gv" runat="server" DataKeyNames="Id" OnRowCommand="gv_RowCommand">
  <Columns>
    <asp:ButtonField Text="Edit" CommandName="EditRow" ButtonType="Button" />
  </Columns>
</asp:GridView>
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "EditRow")
    {
        int rowIndex = Convert.ToInt32(e.CommandArgument);
        string id = gv.DataKeys[rowIndex].Value.ToString();
        Response.Redirect("Edit.aspx?id=" + Server.UrlEncode(id));
    }
}

On Edit.aspx, validate the query string, parse the ID, then fetch the single record from the database and populate the textboxes (do not trust client data; check permissions):

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string idStr = Request.QueryString["id"];
        if (!string.IsNullOrEmpty(idStr) && int.TryParse(idStr, out int id))
        {
            var record = LoadRecordById(id); // query DB
            if (record != null) { /* assign textboxes */ }
        }
    }
}

Notes and troubleshooting: ’s Session suggestion works for tiny, non-sensitive values; ’s approach (save DataTable in Session) can work but causes memory and web‑farm/session‑state issues and may return stale rows. Alternatives: Server.Transfer + Context.Items to pass objects without query strings (keeps original URL), POST forms, or encrypt the query string if IDs are sensitive. Always validate IDs, check DataKeyNames is set, ensure DataBind ran before reading DataKeys, and use optimistic concurrency (timestamp/rowversion) when saving edits.

Recommended Answers

All 2 Replies

>i dont know how to pass the data for that selected row to be displayed in other page. Can someone help me??

Use Session state.

Click handler of button of page1.aspx

Session["firstname"]=textbox1.Text;
Session["lastname"]=textbox2.Text;

Page_load handler of page2.aspx

if(!IsPostBack){
   if(Session["firstname"]!=null) 
          TextBox1.Text=Session["firstname"].ToString();

   if(Session["lastname"]!=null) 
          TextBox1.Text=Session["lastname"].ToString();

  }

do one thing, before binding gridview with datatable or dataset, store whole datatable or dataset in Session.

Now I assume you have select button for each row in gridview. So you can pass the ID of particular row in query string of the page you want to open.

Now in page2, just cast session into datatable or dataset whatever you are using. Also store fetch ID from query string and pass it in tables Select() method. The Select() method of DataTable return array of DataRow.

So once you have data in datarow array, you can assign value from it to your textbox on page.

hope this will help you..


I am trying to open other page and pass data for selected row in gridview to be displayed in that page when user click the select button. i want to display the data in text box. so that, user can make editing from that page. i dont know how to pass the data for that selected row to be displayed in other page. Can someone help me??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.