locked
Passing Value /Sending Data from one page of asp.net Label field to asp.net textbox field in another page RRS feed

  • Question

  • User-1279044681 posted

    Hi Friends,

    i am working with a  Asp.net web application using C#, it has two pages, page1 and page2.

    Page1 is having a asp.net label with some value in it like Name and a asp.net button,

    and Page2 is having a asp.net Textbox.

    on button click i want the value(Name) in label of Page1 should transfer to the  textbox of Page2.

    Even if i directly open Page2 then also, Page2 should not popup any error like null reference or some other

    Hope i have explained my issue, please help,

    Thanks all

    Monday, April 3, 2017 8:44 AM

Answers

  • User-1509636757 posted

    Use QueryString to pass value.

    In your Page1.aspx, pass label value in QueryString (encode it as well):

            protected void btnRedirect_Click(object sender, EventArgs e)
            {
                Response.Redirect("Page2.aspx?lblval=" + HttpUtility.UrlEncode(lblValue.Text));
            }

    and in your Page2.aspx in Page_Load event check for null and set value to TextBox (decode it as well):

            protected void Page_Load(object sender, EventArgs e)
            {
                //-- check for null, if this page is opened directly.
                if (null != Request.QueryString && 
                    !string.IsNullOrEmpty(Request.QueryString["lblval"]))
                {
                    TextBox1.Text = HttpUtility.UrlDecode(Request.QueryString["lblval"]);
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 3, 2017 8:58 AM