2

My problem is that the Page_load fires before any button click event on the page. Say if I wanted to post back to the same page and transfer some data in the querystring, then on page_Load read that data and populate gridview depending on the value in the querystring (details.aspx => details.aspx?id=2).

How can I do that? Thank you!

1
  • I have updated the answer as per your comments. Commented Jul 19, 2011 at 13:28

3 Answers 3

4
private void Page_Load()
{
    if (IsPostBack)
    {
        // DO what you need to do from the postback here.
    }
}

UPDATE:

if you use are redirecting to the page it is no longer a postback (Response.Redirect). In that case the option I see is in page_load you check for a specific querystring parameter and act on it.

  if(!String.IsNullOrEmpty(Convert.ToString(Request.QueryString["myQueryParam"])))
  {


  }
Sign up to request clarification or add additional context in comments.

3 Comments

this does not answer my question. Try it yourself. Say you have "response.redirect("default.aspx?id=" + txt.text) and in the page_load you have if(postback){ response.write(request.querystring["id"]) } and see what happens. This is not the answer to my question.. You should not answer if you are not sure
Well. I don't see anywhere in your question, you saying you use response.redirect..
@Johan: Redirecting is not the same as Posting Back.
3

The Request.Form.Keys collection contains the button id that caused the postback. In effect you can use this to detect the button that was clicked.
Alternatively use ASP hyperlink control and put the querystring url into the NavgateUrl property. (unless you have more postback data required from the details page)

if you are doing data binding the something like this would work

<asp:HyperLink ID=”linkTest1” runat=”server” NavigateUrl='<%# Eval(“ColumnName”, “Details.aspx?ID={0}”)' %>

Or you can set the ID from code behind like ths

linkTest.NavigateUrl = "Details.aspx?ID=" + IdVariable;

This would be better than doing the response.redirect

Comments

1

It seems he just simply asked how he can redirect to same page with query string. Its kinda late but I'm surprised why no one has mentioned the following:

 void Page_Load( ... )
    {
        if (!Page.IsPostBack)
        {                
          Response.Redirect("Details.aspx?id=2");
        }
    }

Of course I have just write redirect URL based on your explanation. You can set that url and query string programmatically.

Hope this helps others.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.