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;
int ctrlCount = 0;
for (int i = 0; i < 5; i++)
{
newButton();btnAsTrigger =
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);
}
No comments:
Post a Comment