locked
Querystring is not working on reloading a page RRS feed

  • Question

  • User-875506825 posted

    I've a Linkbutton control in the master page. And in the content page(Home.aspx) i'm trying to navigate it an aspx page(Apply.aspx) passing a value using querystring. Everything is working fine when i click the apply linkbutton control for the first time i.e i'm able to get the value  the querystring and able to use it.

    But when i click on the same linkbutton second time, i'm not getting the querystring value?how to persist the querystring value on reloading?

    Friday, January 21, 2011 2:28 AM

Answers

  • User-660870441 posted

    when i click on the same linkbutton second time, i'm not getting the querystring value?how to persist the querystring value on reloading?
    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    When we click a LinkButton, the Query String will be retained automatically by default. At least, it works fine for me. Here is how I test it.<o:p></o:p>

    I create a new Master Page in the project, add a LinkButton to it, then nothing in the click event handler:<o:p></o:p>

    protected void LinkButton1_Click(object sender, EventArgs e)<o:p></o:p>

    {<o:p></o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    Then I create a page using the master page; add this code in the Page_Load method:<o:p></o:p>

    protected void Page_Load(object sender, EventArgs e)<o:p></o:p>

    {<o:p></o:p>

        Response.Write(Request.QueryString["a"]);<o:p></o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    Then I debug the page by inputting the URL with http://localhost:35646/WebForm4.aspx?a=hello. The page displays the Query String value fine. Even though I click the LinkButton multiple times, it is still displaying the Query String correctly.<o:p></o:p>

    One possibility I guess is that you are redirecting the page in the click event handler of the LinkButton. Like the following:<o:p></o:p>

    protected void LinkButton1_Click(object sender, EventArgs e)<o:p></o:p>

    {<o:p></o:p>

        Response.Redirect("~/WebForm4.aspx");<o:p></o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    If that is the case, you can persist with the Query String parameters programmatically via code. See the follow code for example:<o:p></o:p>

    protected void LinkButton1_Click(object sender, EventArgs e)<o:p></o:p>

    {<o:p></o:p>

        System.Text.StringBuilder queryStringBuilder = new System.Text.StringBuilder();<o:p></o:p>

        foreach (string key in Request.QueryString.Keys)<o:p></o:p>

        {<o:p></o:p>

            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(Request.QueryString[key]))<o:p></o:p>

            {<o:p></o:p>

                queryStringBuilder.AppendFormat("{0}={1}&", key, Request.QueryString[key]);<o:p></o:p>

            }<o:p></o:p>

        }<o:p></o:p>

    <o:p> </o:p>

        Response.Redirect("~/WebForm4.aspx?" + queryStringBuilder.ToString());<o:p></o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    If this above does not solve your issue, please feel free to let me know. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 25, 2011 1:41 AM

All replies

  • User1992938117 posted

    You can use session["variable"] = query Value;

    Friday, January 21, 2011 3:56 AM
  • User-875506825 posted

    Yes i know using a session variable will work. But i want to know how to retain the value of querystring on reloading a page in general.

    Thanks for you reply

    Friday, January 21, 2011 4:14 AM
  • User712082397 posted

    When you create the link on the page first time, save the URL (that it points to) in session object. Now on subsequent requests, get the value from session and set the value of linkbutton.

    Friday, January 21, 2011 9:04 AM
  • User-660870441 posted

    when i click on the same linkbutton second time, i'm not getting the querystring value?how to persist the querystring value on reloading?
    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    When we click a LinkButton, the Query String will be retained automatically by default. At least, it works fine for me. Here is how I test it.<o:p></o:p>

    I create a new Master Page in the project, add a LinkButton to it, then nothing in the click event handler:<o:p></o:p>

    protected void LinkButton1_Click(object sender, EventArgs e)<o:p></o:p>

    {<o:p></o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    Then I create a page using the master page; add this code in the Page_Load method:<o:p></o:p>

    protected void Page_Load(object sender, EventArgs e)<o:p></o:p>

    {<o:p></o:p>

        Response.Write(Request.QueryString["a"]);<o:p></o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    Then I debug the page by inputting the URL with http://localhost:35646/WebForm4.aspx?a=hello. The page displays the Query String value fine. Even though I click the LinkButton multiple times, it is still displaying the Query String correctly.<o:p></o:p>

    One possibility I guess is that you are redirecting the page in the click event handler of the LinkButton. Like the following:<o:p></o:p>

    protected void LinkButton1_Click(object sender, EventArgs e)<o:p></o:p>

    {<o:p></o:p>

        Response.Redirect("~/WebForm4.aspx");<o:p></o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    If that is the case, you can persist with the Query String parameters programmatically via code. See the follow code for example:<o:p></o:p>

    protected void LinkButton1_Click(object sender, EventArgs e)<o:p></o:p>

    {<o:p></o:p>

        System.Text.StringBuilder queryStringBuilder = new System.Text.StringBuilder();<o:p></o:p>

        foreach (string key in Request.QueryString.Keys)<o:p></o:p>

        {<o:p></o:p>

            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(Request.QueryString[key]))<o:p></o:p>

            {<o:p></o:p>

                queryStringBuilder.AppendFormat("{0}={1}&", key, Request.QueryString[key]);<o:p></o:p>

            }<o:p></o:p>

        }<o:p></o:p>

    <o:p> </o:p>

        Response.Redirect("~/WebForm4.aspx?" + queryStringBuilder.ToString());<o:p></o:p>

    }<o:p></o:p>

    <o:p> </o:p>

    If this above does not solve your issue, please feel free to let me know. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 25, 2011 1:41 AM