Showing posts with label generated. Show all posts
Showing posts with label generated. Show all posts

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);
}

Saturday, March 24, 2012

UpdatePanel and __doPostBack

My app loads one of several dynamically generated reports, implemented as user Controls, (*.ascx files) into an Update Panel. I am trying to do this without a page postback. However, client side code needs to validate several things before issuing the asynchronous request. In IE, I was using an ASP.Net custom Validator control hooked to a client side script
(GenerateReport(sender, e) {...}
to control this, (usinge.valid = true/false; to control whether the asynchronous request to update the content in the UpdatePanel was sent or not, but I have discovered that Firefox does not implement ASP.Net Validators properly.

So now I am trying to accomplish the same functionality simply using a client side script that calls __doPostBack(eventTarget, eventArgs);
But I can't seem to get the __doPostBack to call the Microsoft AJAX asynhcronous call, instead of doing a complete page postback.

How do I do that? Or is there a better way to do this.

Another option I have considered is to put the user control into a web service, that somehow just returns the innerHTML of the user5 control rendering process, but I can;t seem to figure that out either..

ideas anyone?

I've been fighting the same problem the last couple of days. Here is what I just finally came up with...

I write the __doPostBack on the server side and insert it into an onClick event on a control.

String sOnClick

="__doPostBack('" +this.UpdatePanel1.UniqueID +"','eventarg1$eventarg2');"; // the Update Panel's UniqueID will call the asynchronous request and pass the event argument string

e.Row.Attributes.Add("onClick",sOnClick); // I'm adding this to a gridview, I'm guessing this will work elsewhere too.

Then in the Page_Load I parse the event arguments and call the appropriate function

StringsEventArguments =this.Request.Params["__EVENTARGUMENT"];if(sEventArguments !=null)

{

Int32iDelimiter =sEventArguments.IndexOf('$');

StringsArgument =sEventArguments.Substring(iDelimiter + 1);

if (sEventArguments.StartsWith("eventarg1")) EventFunction(sArgument);

}

I hope this helps.

G


You canuse __doPostBack() to refresh an UpdatePanel, if you target the UpdatePanel itself.

Wednesday, March 21, 2012

UpdatePanel and Javascript

I seem to be having some trouble with some dynamically generated textboxes' onkeyup firing when they are in an UpdatePanel. Does anyone know of the UpdatePanel having some issues with Javascript?

Any suggestions are welcome. Thanks.

Hi,

could you provide a demo page that reproduces the issue?


It is a little complicated to explain, but:

I have several dynamically created textboxes in UpdatePanel1. I have them the trigger set as a invisible link (basically, a link without any text or target--which simply created the javascript generated postback). Outside of this panel, I have another textbox. The contents of this textbox consists of the sum of all numbers in the UpdatePanel1 textboxes... changing everytime onkeyup fires. Before I implemented the UpdatePanel, the page worked fine. But, since including the textboxes in the UpdatePanel1, the lower textboxes remains empty when I enter data. Basically, I am wanting the same effect.

Does this help?