Showing posts with label programatically. Show all posts
Showing posts with label programatically. Show all posts

Wednesday, March 28, 2012

updatepanel - Cannot unregister UpdatePanel

Hello everybody!

I have been trying to set programatically visible and unvisible of some Controls in my WebPage. On this Controls there are UpdatePanels.
On each control - one UpdatePanel.

I have a problem. If I try to change visible of UserControl, I get the error "Cannot unregister UpdatePanel ... since was not registered with the Script Manager". OK, I found the solution.
http://msmvps.com/blogs/luisabreu/archive/2006/11/16/adding-removing-updatepanels-dynamicaly-from-a-page.aspx
I have to set visible = true/false in "Page_PreInit" method, it works on single test site, but...

In my project I have a MasterPage, and in method Page_PreInit areference to my control is null, so I can't change visible, and masterpage has no PreInit method.

How to solve it ? Maybe you know some other solution to dynamic hide controls with updatePanel in it.

regards
bakuuu

Can you have the Controls in the UpdatePanel vs the UpdatePanel in the Controls? Seems like this design would work out better...

-Damien


I did it, but these controls are tabs. These tabs look like tabs I see, when I write this replay message (Reply: Compose, Options, Related, Preview). Just imagine, I want to hide some of these tabs.
In this moment I have one big UpdatePanel and all tabs inside. But, I don't like it. There is many controls in each tab (buttons, textBoxes ...) and it weight much. I want to divide these part of page.

bakuuu


Ahh, gotcha.

Well, you could just hide them client-side using the CSSdisplay:none; on the Tab's (not the panel, but the actual tab) outermost <SPAN>. Each tab has an ID assigned to it such as ...TABPANELNAME_tab. You need to just append the "_tab" to the end of the Tab Panel's ID. I have an article ontheming the tab which talks about the tab structure that may help.

-Damien

Monday, March 26, 2012

UpdatePanel : Add TRIGGERS programatically for dynamically generated controls

I thought this would be an easy task but evidently not. Here is the situation: I have anupdatePanel that needs to be refreshed based ontriggers that are suppose to bedynamically generated controls. How do I go about doing this? I can not add Triggers to updatePanel for the controls that are not yet created.

STEPS:

    Initialized 10 controls dynamically : Lets say Button controlsupdatePanel initializedtriggers added to updatePanel for the newly generated controlsbadabing - they all TRIGGER the updatePanel behavior...

My sequence may even be wrong. Maybe the updatePanel has to be created programatically too, I don't know. First of all how to do it that it works and then whats considered the best practise? Anyone?

Cheers...

Ok, I figured it out after thinking and searching for a bit. Here is the solution:

--------

publicvoid createDynamicUpdatePanel()
{

Button btnAsTrigger;
AsyncPostBackTrigger trigForBtns;

// Create Multiple Controls
int ctrlCount = 0;
for (int i = 0; i < 5; i++)

{

btnAsTrigger =

newButton();
btnAsTrigger.ID ="btnAsTrigger" + i.ToString();
btnAsTrigger.Text ="btnAsTrigger " + i.ToString();

trigForBtns =

newAsyncPostBackTrigger();
trigForBtns.ControlID = btnAsTrigger.ID;
trigForBtns.EventName ="Click";// Add controls to the form
Form.Controls.Add(btnAsTrigger);
ctrlCount = i + 1;

}

// Create label Control : For updatePanel Content
Label myLabel =newLabel();
myLabel.Text =DateTime.Now.ToString();// Create UpdatePanel
UpdatePanel myUpdatePanel =newUpdatePanel();
myUpdatePanel.ID ="updatePanelDynamic";
myUpdatePanel.UpdateMode =UpdatePanelUpdateMode.Conditional;
myUpdatePanel.ContentTemplateContainer.Controls.Add(myLabel);// Create Triggers for all previosuly generated controls
for (int i = 0; i < ctrlCount; i++)
{
AsyncPostBackTrigger trigAsynPostback =newAsyncPostBackTrigger();
trigAsynPostback.ControlID ="btnAsTrigger" + i.ToString();
trigAsynPostback.EventName ="Click";
myUpdatePanel.Triggers.Add(trigAsynPostback);

}

// Make it happen
Form.Controls.Add(myUpdatePanel);

}

Works like a charm... Thats all there was to it. Anyway if someone has a more elegant way to do this, please do post your solution.
Thanks


Screwed up the formatting up there, here we go again:

public void createDynamicUpdatePanel()
{
Button btnAsTrigger;
AsyncPostBackTrigger trigForBtns;

// Create Multiple Controls
int ctrlCount = 0;
for (int i = 0; i < 5; i++)
{
btnAsTrigger =new Button();
btnAsTrigger.ID ="btnAsTrigger" + i.ToString();
btnAsTrigger.Text ="btnAsTrigger " + i.ToString();

trigForBtns =new AsyncPostBackTrigger();
trigForBtns.ControlID = btnAsTrigger.ID;
trigForBtns.EventName ="Click";

// Add controls to the form
Form.Controls.Add(btnAsTrigger);

ctrlCount = i + 1;
}

// Create label Control : For updatePanel Content
Label myLabel =new Label();
myLabel.Text = DateTime.Now.ToString();

// Create UpdatePanel
UpdatePanel myUpdatePanel =new UpdatePanel();
myUpdatePanel.ID ="updatePanelDynamic";
myUpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
myUpdatePanel.ContentTemplateContainer.Controls.Add(myLabel);

// Create Triggers for all previosuly generated controls
for (int i = 0; i < ctrlCount; i++)
{
AsyncPostBackTrigger trigAsynPostback =new AsyncPostBackTrigger();
trigAsynPostback.ControlID ="btnAsTrigger" + i.ToString();
trigAsynPostback.EventName ="Click";

myUpdatePanel.Triggers.Add(trigAsynPostback);
}

// Make it happen
Form.Controls.Add(myUpdatePanel);
}