2
votes

I am trying to use a RadUpload Control, however, on any button click the page postbacks. To get around this I am trying to use a updatePanel and placing the button in the updatePanel, so that only a partial postback occurs. The reason for this is that the RadUpload Control loses it's value on postback. I am dynamically creating all the controls, and being a beginner to asp.net and C# this is tricky for me.

Here is the code:

Button aButton = new Button();
aButton.ID = newControls[1].ID + "_Button";
aButton.Click += new EventHandler(aButton_Click);
aButton.UseSubmitBehavior = false;

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = aButton.ID;
trigger.EventName = "Click";

UpdatePanel container = new UpdatePanel();
container.ID = newControls[1].ID + "_Container";
container.ChildrenAsTriggers = true;
container.UpdateMode = UpdatePanelUpdateMode.Conditional;
container.Triggers.Add(trigger);
container.ContentTemplateContainer.Controls.Add(aButton);

tcControl.Controls.Add(container);

static void aButton_Click(object sender, EventArgs e)
{
    // Do Something
}

I know i'm probably doing something very wrong haha, but any advice would be great. Thanks!

2
At what point does that code run? I would guess that your having a problem with the ASP.NET Page Life Cycle. Specifically, dynamically generated controls should always being added during the "PreInit" event. - Josh Darnell
This code runs through a method which is called in the Page_Load. Should dynamically created controls be created in the PreInit always? I am dynamically creating a large number of controls. This small portion of code only occurs when a radupload is created. - Eric
I tried to place the dynamic control generating methods in the PreInit and they don't show up on the page. I am creating them all with unique ID's and placing them in an HTMLTable. This table contains anywhere from 1...n rows, and each row has two cells. The first cell contains a static label that is dynamically created. The second contains two controls, a dynamically created control of nearly any type, and a label that holds the text from the first control. The visibility of the controls in the second cell alternate, between edit mode(non-label is visible) and non-edit mode(label is visible) - Eric
Hmmm. I'm not incredibly familiar with creating controls in this fashion, I just know that's a common pitfall people run into. And yes, dynamically created controls should always be created / recreated in PreInit for the most stable performance. Sorry I wasn't a whole lot of help. Hopefully someone will come along that can help you out more. - Josh Darnell
Thanks for trying anyways (: and ill keep the advice about using the PreInit for future reference for sure! - Eric

2 Answers

0
votes

Are you creating controls again on postback? If I don't remember bad, controls should be created again on init or preinit so viewstate is loaded into them.

This video http://www.asp.net/web-forms/videos/aspnet-ajax/how-to-dynamically-add-controls-to-a-web-page shows something similar, I guess, otherwise on monday I can look for my own code at my pc at work.

Hope it helps!

0
votes

I have solved this problem by completely removing this upload control from the page and instead replacing it with a button that loads an external upload page(just a popup). I was unable to find a solution for what I originally intended, but my current solution is all I have for now.