Share via


Adding a Trigger to an UpdatePanel in code behind

Question

Thursday, June 21, 2007 5:58 PM

I need to add in code a trigger to an UpdatePanel. I've been reserching, and i cannot find a way to do it. The UpdatePanel has a property called Triggers, which is a collection of UpdatePanelTriggerCollection. The contructor of this collections takes the panel to which it belongs to. If you try to add a new item to the collection, it expects an UpdatePanelTrigger item. If you go to that class, it's constructor is protected, so I guess they are expecting that if you are going to add a new control to the trigger collection, it has to inherit from the UpdatePanelTrigger?

Has anyone added in code a Trigger to an UpdatePanel?

All replies (12)

Friday, June 22, 2007 12:03 PM âś…Answered

Well, it turns out that my logic was correct. The UpdatePanel has a Trigger collection, and that collection is an UpdatePanelTriggerCollection. when you attemp to add a new trigger, it is expecting an UpdatePanelTrigger item.  That class is abstract, and its contructor is protected. So following the line of thought, there should be a class that inherits from this one, and it is UpdatePanelControlTrigger, but that does not solve the problem either.

But again, this one is abstract as well and the contructor protected. So I went back to the ASPX, and tried to add a new trigger. Guess what? You can either add an AsyncPostBackTrigger or a PostBackTrigger . So I went back to the code behind class, and I found the two classes: AsyncPostBackTrigger and PostBackTrigger. Botth of them inherit from the UpdatePanelControlTrigger class, and this have the constructor public, and it's got three properties: ControlId, which you can get and set, EventName, string also that you can get or set, and Owner(inherits from UpdatePanelTrigger), which only you can get ( UpdatePanel object). You set the ControlId and the EventName, and you in your UpdatePanel, you add the object.

It will look something like this:

//Creates a new async trigger
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
            
//Sets the control that will trigger a post-back on the UpdatePanel
trigger.ControlID = "btnCommit";

//Sets the event name of the control
trigger.EventName = "Click";

//Adds the trigger to the UpdatePanels' triggers collection
pnlMain.Triggers.Add(trigger);

 Hopes this helps someone someday.


Friday, June 22, 2007 2:46 AM

 HI,

 You have to use ScriptManager's RegisterAsyncPostBackControl method to add the a trigger in code behind.

eg: ScriptManager1.RegisterAsyncPostBackControl(Button1) 


Friday, June 22, 2007 9:31 AM

Thanks a lot... but how does the update panel know what event to produce the post-back? And if I have two update panels on the page, how does it know in which update panel I want to trigger the post back?


Tuesday, July 10, 2007 1:56 AM

helped me... thanks champ ;)


Tuesday, October 9, 2007 11:33 AM

Awsome, thanks for this....

BTW, the reason that I needed this is because I needed to establish a conditional trigger for a control that is part of the MasterPage, and thus not availiable at design time to panels that are inside the Content section.

Also BTW, you can assign one trigger to multiple UpdatePanels. For example, I have a dashboard with 5 panels, all of which need to update either on sort of the grids that are in them, or when a selection is made on a master filter that is part of the MasterPage. I was able to create one trigger and add it to all of the collections and it works perfectly.


Monday, October 22, 2007 10:30 AM

this doens't quite work for me...

I have to do a async / sync call depending on the status of a checkbox... is this possible?

 I tried to keep the button outside the updatepanel and assign it as a async trigger on the checkbox's click changed event. that doesn't seem to work though.. any thoughts?


Wednesday, October 24, 2007 11:09 AM

You may have to use FindControl in order to access the control at run time, depending on where your control is placed.


Friday, January 4, 2008 12:01 PM

Hi guys,
I have the same problem.

you can found my project in: Xzshare Download Link

The problem is the following:

When I click on the Button1 of the WebUserControl1 the UpdatePanel was refreshed and all the elements are deleted, but I would like change only the label inside the WebUserControl1.

Another suggestion, is correct the way that I choose to load the WebUserControls?


Sunday, January 6, 2008 4:54 AM

No one can help me? 


Sunday, June 1, 2008 10:40 AM

<asp:Button ID="Button1" runtat="server" onPreRender="addTrigger_PreRender" />

Code Behind protected void addTrigger_PreRender(object sender, EventArgs e)
{

Button btn = sender as Button;

// if your script manager is in you master page 
ScriptManager manager = (ScriptManager)Master.FindControl("ScriptManager1");
manager.RegisterPostBackControl(btn)

// if you script manager is in you aspx page
ScriptManager1.RegisterPostBackControl(btn)

 // if you want to register an asynchronous  post back
ScriptManager1.RegisterAsyncPostBackControl(btn) 

}


Thursday, February 12, 2009 1:00 PM

I hope you can assist me, this is the only post that seems to correctly address this issue.  I have implemented your code, but I still get no post back from the button when it is press.  I click the button, and nothing happens.  Removing the Panel from the ModalPopupExtender gets the proper results from the button press; database fields are updated and text is displayed on the page.  So the code is working, just the ModelPopup link that is not.

 Is there possibly a way to force a real post back instead of an AJAX post back?

  

Button bvar = new Button();
pv3.Controls.Add(bvar);
bvar.ID = "ButtonVariableMPUPdate" + bvi.batchvariableid.ToString();
bvar.Text = "Update";
bvar.CommandArgument = bvi.batchvariableid.ToString();
bvar.Click += new EventHandler(UpdateBatchVariableData);

AsyncPostBackTrigger apbt = new AsyncPostBackTrigger();
apbt.ControlID = "ButtonVariableMPUPdate" + bvi.batchvariableid.ToString();
apbt.EventName = "Click";
UpdatePanel1.Triggers.Add(apbt);

 

 


Tuesday, June 16, 2009 12:19 PM

 Use PostBackTrigger instead of AsyncPostBackTrigger