Vinay
Vinay

Reputation: 59

how send the value of the selected row in gridview to next page..,

below is the code which is written in the Page where we want it..

protected void Page_Load(object sender, EventArgs e)
{
   if (this.Page.PreviousPage != null)
   {
      int rowIndex = int.Parse(Request.QueryString["RowIndex"]);
      GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("gv1");
      GridViewRow row;
      row = GridView1.Rows[rowIndex];
      lblid.Text = row.Cells[1].Text;
      lblname.Text = (row.FindControl("lblName") as Label).Text;
      lblmt.Text = row.Cells[3].Text;
      lblyer.Text = row.Cells[4].Text;
   }
}

Upvotes: 0

Views: 961

Answers (1)

Tummala Krishna Kishore
Tummala Krishna Kishore

Reputation: 8271

You have a few options, consider

  1. Session state
  2. Query string

Session state

If you are going to send data between pages, you could consider the use of Session State.

ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. HTTP is a stateless protocol. This means that a Web server treats each HTTP request for a page as an independent request. The server retains no knowledge of variable values that were used during previous requests. ASP.NET session state identifies requests from the same browser during a limited time window as a session, and provides a way to persist variable values for the duration of that session. By default, ASP.NET session state is enabled for all ASP.NET applications. Best of all, it is easy!

Put data in (for example on default1.aspx)

Session["ID"] = lblid.Text;
Session["Name"] = lblname.Text;
Session["Amt"] = lblmt.Text;
Session["Year"] = lblyer.Text;

Get it out (for example on default2.aspx)

string Id= Session["ID"] 
string Name= Session["Name"] 
string Amout = Session["Amt"] 
string year = Session["Year"] 

Query string

If you are sending small amounts of data (eg id=4), it may be more practical to use query string variables.

You should explore the use of the query string variables, e.g.

http://www.domain.com?param1=data1&param2=data2

You can then get the data out like

string param1 = Request.QueryString["param2"]; // value will be data1
string param2 = Request.QueryString["param2"]; // value will be data2

You can use something like How do you test your Request.QueryString[] variables? to get the data out.

If you want to know more about querystring variables check out their wikipedia article

Upvotes: 1

Related Questions