views:

127

answers:

2

I have a user control that is pretty basic. It contains several TextBox controls, a few DropDownList controls, a save Button and a cancel Button. I would like to use this control in two different modes. The first mode is in the normal postback mode to do the save and cancel actions. The second mode would use AJAX to do the save and cancel actions.

Is it possible to wrap the contents of the control in an UpdatePanel and then be able to turn on/off whether or not the UpdatePanel does AJAX or PostBack for the control events? Or would I be better served by just creating two new controls (1 with UpdatePanel, 1 without) to house the one old control?

+1  A: 

The easiest way to just 'turn off' your UpdatePanel is to set EnablePartialRendering to false in the ScriptManager. I'm not sure that this is the best solution. I would recommend adding your controls to an update panel in your codebehind Page_Load event handler based on a boolean flag.

void Page_Load() {
    if(IsAjaxy) {
     upAnUpdatePanel.Controls.Add(tbSomeTextBox);
    }
    else {
     this.Controls.Add(tbSomeTextBox);
    }

Something along those lines should work just fine.

Zachary Yates
+1  A: 

Have a look at the Refreshing UpdatePanel Content section here: UpdatePanel Class.

But what I would do, personally, is just create the UserControl without the UpdatePanel, and then enclose it in one when you need to. Less confusing.

Kyralessa