Tutorial :Post value to new window from Gridview Selected column


I tried to post the selected ID number from source page into a new target page(pop up window) and display more details on that page.

I have googled the varies solutions but seem like they are a bit out of date. Here are my questions:

What's the best way to post it (session? cookie?) or Page.PreviousPage.FindControl? or add a control into <ItemTemplate>? or Javascript?

I don't want to use a query string since I don't want the user to manually modify it.

What should I write on the target page to grab that value?

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)  {      string testID= GridView1.SelectedRow.Cells[3].Text;      // eg. Session["ID"] = testID;        //eg. open new pop up window ("target.aspx");  }  

Many thx


Although you could use store your selected value on a session variable or even cross-page postbacks, my preferred method and overall standard method is to use a simple querystring value.

1) Grab the value and redirect with the desired ID.

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)      {              var testID = gridView1.SelectedRow.Cells[3].Text;          Response.Redirect("target.aspx?Id=" + Server.UrlEncode(testID));      }  

2) On the "target.aspx" page, load the values from the url ("http://localhost/target.aspx?Id=125")

  protected void Page_Load()      {          if (!IsPostBack)          {              var querystringValue = Request.QueryString["Id"];              if (!string.IsNullOrEmpty(querystringValue))              {                  // do something.              }          }      }  

Passing variables between pages using QueryString.

Edit: Although I wouldn't do such a thing (I like to keep my websites as stateless as possible), the same principle applies to session variables.

protected void Page_Load()      {          if (!IsPostBack)          {              var sessionValue = Session["Id"];              if (!string.IsNullOrEmpty(sessionValue))              {                  // do something.              }          }      }  

That being said, I don't understand what's the problem with querystrings. Sure, the user can modify it and insert bogus values, but that's where your server-side validations come into place. And hey, if google uses querystrings, they can't be all that bad.


Have you tried Cross Page Postback? It sounds like that is what you're looking for.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »