Programatically binding an event in an ASP.NET 4.0 application?

There are a couple parameters to this. When is the link being re-created on the page's postback? It needs to be done before Load finishes, so that the link's event is there to be handled by the postback.

Up vote 0 down vote favorite share g+ share fb share tw.

I have searched for most of the day for an answer to this but I do not seem to be able to find an answer so here is my situation. I have a ModalPopupExtender in a GridView in which I create dynamically some LinkButtons. For (int I = 0; I ")); } that is how I proceed.

I have found that the AddEventHandler was the best way to do that. My problem is that when I click on the LinkButton, I see the page do a Postback, but nothing happens. When I click on the LinkButton, it should call the AutomaticSearch function that is in the background code.

I have put a break point in the said function and discovered that it never goes through it after I clicked the LinkButton, it goes directly to the Page_load function. Here is how my Automatic Search function looks like: protected void AutomaticSearch(object sender, EventArgs e) { LinkButton btn = new LinkButton(); btn = (LinkButton)sender; //a substring is needed here because the ID of the link button for the employees is made dynamically //and only the first part is hard coded (lbPnlEmployee) // if you put a longer length, it will return an error because some IDs are smaller (ie: lbTitle) switch (btn.ID. Substring(0,7)) { case "lbDivis": ddlSearchParameter.

SelectedIndex = 5; break; case "lbLocat": ddlSearchParameter. SelectedIndex = 6; break; case "lbTitle": ddlSearchParameter. SelectedIndex = 3; break; case "lbPnlDi": ddlSearchParameter.

SelectedIndex = 5; break; case "lbPnlLo": ddlSearchParameter. SelectedIndex = 6; break; case "lbPnlMa": ddlSearchParameter. SelectedIndex = 0; break; case "lbPnlEm": ddlSearchParameter.

SelectedIndex = 0; break; } txtValue. Text = btn. Text; GridViewBinding("disp_nm", "ASC"); } c# asp.net gridview event-handling link|improve this question edited Nov 21 '11 at 16:38Joel Coehoorn123k34225421 asked Apr 1 '11 at 18:35Hugo102.

– Mike M. Apr 1 '11 at 18:40 no it does not, that is the problem – Hugo Apr 1 '11 at 18:58.

There are a couple parameters to this. When is the link being re-created on the page's postback? It needs to be done before Load finishes, so that the link's event is there to be handled by the postback.

If you are doing that, also try adding the event again in the link's preRender. I'd do something like, lbEmployee. PreRender += new EventHandler(LbEmployee_PreRender); protected void LbEmployee_PreRender(object sender, EventArgs e) { LinkButton link = sender as LinkButton; link.

Click += new EventHandler(AutomaticSearch); }.

It goes in the preRender function when creating the LinkButton, but the problem is still the same when clicking on teh created link after wards – Hugo Apr 1 '11 at 19:00 Ok, but when you get back to the page on the postback, remember Asp. Net doesn't remember state, so its a brand new page request so your control hasn't been made yet. So there is nothing to process unless the control with the event being wired up must happen again prior to Load finishing, or it won't go to your click event.

– Chuck Savage Apr 2 '11 at 23:32 I worded that poorly, trying again. Your control doesn't exist until you tell the page it exists. If you tell the page after its done processing events then it won't process your click event.

– Chuck Savage Apr 2 '11 at 23:34 sS I have to call the automaticSearch in the Load, if I understood correctly, or before it. – Hugo Apr 4 '11 at 12:33.

Due to the mentions of 'dynamic link buttons', 'GridView' and 'Page_load' but no event handler, it looks like you're suffering from the usual ASP. Net Page Lifecycle problem. Basically what's happening is this: The page is initialized, Page_Load() event is called Page_Load() (probably) loads some data source, sets the DataSource on the GridView, and calls GridView.DataBind() Some further event handler in the chain sets events on the linkbuttons (I'd assume OnItemDatabound).

The thing you're missing here is that on the postback fired by clicking the link, Page_load will be run again. If you're not checking if this is a postback, the grid will be recreated, and any controls which had events bound to them will disappear and be recreated. The event you were counting on running is now gone, as it's owner just disappeared.

TL;DR version: Wrap your GridView. Datasource = something; GridView.DataBind(); calls in an if (!IsPostback()) { statement. Dealing with Postbacks and Page Lifecycle is the bane of an ASP.

Net programmer's existence if you don't fully understand how this works. Read these articles for more info: http://msdn.microsoft.com/en-us/library/ms178472.aspx http://www.4guysfromrolla.com/articles/092904-1.aspx http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx.

I do check if it is a postback in my page load if (!Page. IsPostBack) { GridviewBinding("name","ASC"); } – Hugo Apr 1 '11 at 19:18 Please post more of your code then, if you are already doing this, so we may be able to better understand what you are doing (possibly incorrectly). – Kilanash Apr 1 '11 at 19:23.

You may want to try this syntax: lbEmployee= new LinkButton(); lbEmployee. Text = dtEmployees. Rowsi0.ToString(); lbEmployee.

Command += new CommandEventHandler(AutomaticSearch); private void AutomaticSearch(object sender, CommandEventArgs e) { // Logic }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions