How to pass information between web forms in asp.n

2019-03-28 11:20发布

问题:

how to send some information from one web form to another web form in asp.net first web form is HumanList.aspx that shows the list of humans in a GridView component. when user click edit link, i want to pass humanID (value of human record ID) from HumanList.aspx to another web form named HumanEdit.aspx. In humanEdit.aspx, i want to load that human (With humanID) and fill human information into text boxes.

回答1:

There are many ways to pass params between pages.

  1. Use Querystring.

Source page - Response.Redirect("second.aspx?param=value");

Destination page - Request.QueryString["param"].

  1. Use Session.

Source page - Session["param"] = "value"; value is set here.

Destination page - Session["param"].ToString(). value is retrieved here.

  1. Use PreviousPage property. Note: This applies if you redirect from first.aspx ( just an example here), to second.aspx , then you can use PreviousPage.<propname> in second.aspx to values set in first.aspx.

In second.aspx you need to add directive like this <%@ PreviousPageType VirtualPath="~/first.aspx" %>.

  1. Use Request.Form to read posted values.

If there are input, dropdownlist existing in source page and you are posting value to second.aspx, then you can read posted value using Request.Form["input_id"].

  1. Use cookies to transfer values. First save some value to cookie from first.aspx and read that value from second.aspx.

Refer to MSDN for more info - MSDN link



回答2:

Consider using session or query string.

Session could be used like

pass value

 Session["value"]=someValue;

get value like

 var value= Session["value"].toString();

You could pass values with the help of properties too



回答3:

I would suggest storing it in the session state: http://msdn.microsoft.com/en-us/library/ms178581.aspx



回答4:

Well, there are many ways to pass information page to page.

one primary way is by QueryString

//To Send
private void submit_Click(object sender, System.EventArgs e)
{
    string ID = String.Empty;

    ID = "192" // Have your ID Here

    Response.Redirect("humanEdit.aspx?ID=" + ID );
}


//To Receive
private void Page_Load(object sender, System.EventArgs e)
{
   String ID = String.Empty;
   ID=Request.QueryString["name"];
}

Another Method is Sessions

//Store your ID from Sending Page
Session["ID"]= "143"; //Example ID

//To Recieve
private void Page_Load(object sender, System.EventArgs e)
{
   String ID = String.Empty;
   ID=Session["ID"].toString();
}

And there are many other ways too...



回答5:

Say You have a gridview1 with a button field Say "Modify" First Provide a command Name to that field Say "Mod" Now in Gridview1_RowCommand() function of HumanList.aspx page write the code like this-

switch (e.CommandName.Trim())
{
case "Mod":
int r1=Int32.Parse((e.CommandArgument).ToString());
Session["id"]=GridView1.DataKeys[r1].Value.ToString();
Response.Redirect("HumanEdit.aspx?id=" + Session["id"]);
break;
}

Now in HumanEdit.aspx.cs you can make your own class and with in that class you can have a datatable object.Store the Sql query in that object and call that class in page_load(). Say class is

Public void show()
{
Within datatable object you can write your query like this
select x,y,z, from tbl_name where id=" + Request.QueryString["id"]
if(dt.Rows.Count>0)
{
Xtext.Text=dt.Rows[0]["x"].ToString();
Ytext.Text=dt.Rows[0]["y"].ToString();
Ztext.Text=dt.Rows[0]["z"].ToString();
}
}

This much of code is enough to get what you want.