How to pass information between web forms in asp.net

后端 未结 5 1097
面向向阳花
面向向阳花 2021-01-02 14:18

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

相关标签:
5条回答
  • 2021-01-02 14:43

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

    0 讨论(0)
  • 2021-01-02 14:50

    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

    0 讨论(0)
  • 2021-01-02 14:50

    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.

    0 讨论(0)
  • 2021-01-02 15:01

    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...

    0 讨论(0)
  • 2021-01-02 15:03

    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

    0 讨论(0)
提交回复
热议问题