开发者

posting to the same page with query string ASP.NET

开发者 https://www.devze.com 2023-03-21 03:07 出处:网络
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 d

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 (d开发者_StackOverflowetails.aspx => details.aspx?id=2).

How can I do that? Thank you!


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"])))
  {


  }


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消