views:

625

answers:

5

When loading a page for the first time (!IsPostback), I am creating a button in code and adding it to my page, then adding an event handler to the click event.

However, when clicking the button, after the page reloads, my event handler does not fire.

Can anyone explain why?

+2  A: 

You need to add the button always not just for non-postbacks.

Brad Wilson
A: 

That is because the event binding that happens needs to be translated in to HTML. This postback that happens if bound to the page between OnInit and OnLoad. So if you want the button to bind events correclty make sure you do the work in OnInit.

See the Page Life Cycle explaination.

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Nick Berardi
A: 

If you are not reattaching the event handler on every postback, then the event will not exist for the button. You need top make sure the event handler is attached every time the page is refreshed. So, here is the order of events for your page:

  1. Page is created with button and event handler is attached
  2. Button is clicked, causing a postback
  3. On postback, the page_load event skips the attaching of the event handler becaue of your !IsPostback statement
  4. At this point, there is no event handler for the button, so clicking it will not fire your event
Mark Struzinski
+2  A: 

@Brad: Your answer isn't complete; he's most likely doing it too late in the page lifecycle, during the Page_Load event.

Okay, here's what you're missing.

ASP.NET is stateless. That means, after your page is rendered and sent to the browser, the page object and everything on it is destroyed. There is no link that remains on the server between that page and what is on the user's browser.

When the user clicks a button, that event is sent back to the server, along with other information, like the hidden viewstate field.

On the server side, ASP.NET determines what page handles the request, and rebuilds the page from scratch. New instances of server controls are created and linked together according to the .aspx page. Once it is reassembled, the postback data is evaluated. The viewstate is used to populate controls, and events are fired.

This all happens in a specific order, called the Page Lifecycle. In order to do more complex things in ASP.NET, such as creating dynamic controls and adding them to the web page at runtime, you MUST understand the page lifecycle.

With your issue, you must create that button every single time that page loads. In addition, you must create that button BEFORE events are fired on the page. Control events fire between Page_Load and Page_LoadComplete.

You want your controls loaded before ViewState information is parsed and added to controls, and before control events fire, so you need to handle the PreInit event and add your button at that point. Again, you must do this EVERY TIME the page is loaded.

One last note; page event handling is a bit odd in ASP.NET because the events are autowired up. Note the Load event handler is called Page_Load...

Will
A: 

Hi, I have problem in Ispostback.I am getting every time false and The button click event is not firing event. Please help me out. I am using javascript code in blow.

function btn_click(keyword) { window.location="/website/search/productsearch.aspx?q=" + keyword; } and calling on page load.

if (!Page.IsPostBack) { txtProductSearch.Attributes.Add("onfocus", "return EmptySearchBox('" + txtProductSearch.ClientID + "')"); txtProductSearch.Attributes.Add("onblur", "return FillSearchBox('" + txtProductSearch.ClientID + "')"); txtProductSearch.Attributes.Add("onkeypress", "return clickButton(event,'" + imgBtnProductSearch.ClientID + "','" + txtProductSearch.ClientID + "')"); imgBtnProductSearch.Attributes.Add("onclick", "btn_click('" + txtProductSearch.Text + "')"); }

    //if (txtProductSearch.Text != "Search Products" & !string.IsNullOrEmpty(txtProductSearch.Text))
    //{
    //    Response.Redirect("~/website/search/productsearch.aspx?q=" + txtProductSearch.Text);
    //}
}
protected void imgBtnProductSearch_OnClick(object s, EventArgs e)
{
    string keyword = txtProductSearch.Text;
    DataTable dtChapterResult;
    DataTable dtArticleResult;
    if (keyword != "Search Products" & !string.IsNullOrEmpty(keyword))
    {
        Response.Redirect("~/website/search/productsearch.aspx?q=" + keyword);
    }
}