Controlling a variable numbers of controls dynamically added to a user control

 Ok, thinking caps on people, this has been causing me grief for a couple of days now.

I have an .aspx page which contains various user controls housed in an Accordion control from the Ajax Toolkit and lots of UpdatePanels, these user controls all interact with each other through the parent page, e.g clicking buttons in one control effects visibility of other controls.

Two of these controls contain just a series of ImageButtons. In the first of these controls the ImageButtons are generated in the page_load method and added to a panel using Panel.Controls.Add(ImageButtonControl), the number of ImageButtons and their ImageUrl properties are read from a database and depend on one of the user's profile parameters. The ImageButton.Click method is assigned as follows,

//In the control creation code
imgbutton.Click += new ImageClickEventHandler(ImageButton_Click);
//later the method is declared
public ImageClickEventHandler ImageClick;

protected void ImageButton_Click(object sender, ImageClickEventArgs e)
{
if (ImageClick != null)
{
HiddenField1.Value = ((ImageButton)sender).ID;
ImageClick(this, e);

}
}

Then on the parent .aspx page

protected void Page_Load(object sender, EventArgs e)
{
...
this.Repair_main1.ImageClick += new ImageClickEventHandler(GroupImageButtonClick);
...
}

//and
public void GroupImageButtonClick(object sender, ImageClickEventArgs e)
{
HiddenField hidden1 = (HiddenField)Repair_main1.FindControl("HiddenField1");
HiddenField hidden2 = (HiddenField)RepairGroup1.FindControl("HiddenField2");
}
 

The HiddenFields are used to pass the ID of the ImageButton clicked in the first control into the second control. This ID then determines how many and which ImageButtons are displayed in the second control. This is where the problems start. Since the ImageButtons in the first control cause an autopostback event, the code that is executed by the ImageButton.Click method from the first control is executed after the Page_Load method of the second control, or so is my understanding of the .NET page life cycle.

If the ID that I need to determine how many and which ImageButtons to display in the second control is being assigned in the PostBackEvent stage then I have to put my ImageButton creation code in the Page_PreRender method (the code is almost the same structure as in the first control). But in the Page_PreRender method EventHandlers can not be assigned, so none of my ImageButtons have .Click methods. I can not created the ImageButtons in the Page_Load method because I do not have the ID of the button that was clicked in the first control yet.

I have tried giving the second control a publicly accessible property for the ID and assigning it in the click method in the parent page, but that didn't work. The big problem is that I always need to create my imagebuttons before the PreRender stage but the postback action of the imagebuttons means that any code to pass the ID through that I execute in the click method of the first control is executed just before the PreRender stage of the second control.

Is there any way to generate this second control with working imagebutton events or am I being dense and going about it all wrong? I do not want to have to put all the buttons on the page with visible properties set as I don't want to  limit the number of images that can be displayed. If there is a way of making a selection in the first control and passing the ID through to the second control before the page_load stage then that would be perfect.

Many thanks, Lex

 

0
lex_w
1/30/2008 12:13:43 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

5 Replies
1098 Views

Similar Articles

[PageSpeed] 47

Have you tried to add the ImageButtons during the Init phase instead of the page_load phase?  When they are added that early in the cycle they should be available to event handlers and the page_load of other user controls.  Often this is done in the CreateChildControls method of the initalization phase.

Hope this helps.


John Stockton
RIA Developer at Ascentium
Co-Author of Silverlight 2 in Action

blog
0
johnnystock
1/30/2008 1:12:48 PM

thats part of the problem, I don't know how many controls i need to add to the page at the Page_Init stage. The event timeline is like this

 1. ImageButton clicked on first control

2. Postback occurs

3. second control page_init and page_load

4. Now the method associated with my ImageButton.click from the first control runs and the ID is passed over to the second control

5. SQL query to database to find out how many images are associated with the ID

6. Controls are added to page

i can't do anything in the second control until the ID has been assigned (step 4), by which point its too late in the page life cycle to assign eventhandlers to my new imagebuttons
 

0
lex_w
1/30/2008 1:33:24 PM

You may want to try to hijack the life-cycle by not waiting for the page_load event and event handlers to do the processing but read the requisite values directly from the Request.Form collection during init.  I'm just guessing here but this may let you do what you are trying before the lifecycle would normally permit.


John Stockton
RIA Developer at Ascentium
Co-Author of Silverlight 2 in Action

blog
0
johnnystock
1/30/2008 2:09:11 PM

 Thanks John,

I was able to find the ID of the button being pressed inside the Request.Form collection, the code is a little messy but it works nicely. Found this link which has useful info on extracting stuff from Request.Form

http://www.ryanfarley.com/blog/archive/2005/03/11/1886.aspx 

 Many thanks, Lex
 

0
lex_w
1/30/2008 4:17:16 PM

Cool, I'm glad I was able to point you in the right direction.  You may also want to go ahead and mark this as answered so others know it has been rexolved.


John Stockton
RIA Developer at Ascentium
Co-Author of Silverlight 2 in Action

blog
0
johnnystock
1/30/2008 4:25:51 PM
Reply:

Similar Artilces:

access controls on web user control x from web user control Y
I have 2 web users controls in a page and I would like to get or set the selected values of a few dropdown boxes on web user control X from web user control Y and I also would like to know how to access functions that are on a web user control X from a web user control Y, is this possible? How can I do it? User can access and set the values of the User Control from .aspx page through properties,using javascript and in code-behind of aspx page.The details of it are shown below Using PropertiesIf the Sample.ascx control has two textboxes and submit button.You can access the valu...

Problem in accessing web form data in user controls when create user control dynamically
HI. I have a problem using web user control. I designed a user control named NewsForm. This user control contains some web form elements like <asp:textbox>. When I add it in an .ASPX page, all things may be true. I can know which user filles in textbox (for example and other controls) <%@ Register TagPrefix="uc1" TagName="NewsForm" Src="NewsForm.ascx" %> and <UC1:NewsForm id="newsForm" runat="server"></UC1:NewsForm> But when i create it in code behined dynamically, however user control has web form controls but their value are blank for example //aspx co...

User Control controls not created when dynamically creating user control
  Have created a user control for displaying and editing a dynamically number of values. The user control contains two labels and an edit box. When dynamically loading the user control I get an error when the control itself tries to change setting on the fields within the user control.   Why doesnt this work? It works okay if I dont load it dynamically but create a user-control-instance in the aspx-page but thats no solution.   Thankyou!     Here is my code:   ucDataValue.ascx   <%@ Control Language="C#" AutoEventWireup="true&...

How to access individual controls from a form containing the web user control containing those controls
 Below is a block of code in my .aspx form named default.aspx. In this form I load a web user control. I then add it to a place holder control.(Lines 3 and 4 below) So how do I access the individual controls in my web user control? I want to set the properties on those controls, but I am baffled as to how to access the control. For example I have a text label control named "label3". How do I access this control. Because for example, under certain situations I want to hide that control. ( Label3.Visible = false;)  1    if(!Page.IsPostBack) 2&nb...

Dynamically adding listbox controls in a web based user control
Hi, I am building a web control and trying to dynamically add checkboxs. I try to create the controls on the render event and i've tried these 3 methods- none of which work.   This method compiles but the checkbox is not visible on the page.  I am unsure as to where it is adding to because I can't reference the parents web page. /*CheckBox blah = new CheckBox(); blah.Text = CheckText; blah.ID = CheckValue; this.Controls.Add(blah);*/   This one compiles but the controls also do not display on the page (I knew this wouldn't work but it was worth a try) //output.W...

Web User Control to Web User Control...
Hi, I have 2 User controls on a page and want Control1 to call a method of Control2. Is this Possible? Example: Con1 = Datalist showing products, each with a Quantity Box and a Button. Con2 = Repeater showing Cart Preview (name, Price) in page.aspx. When I click on the button on the DataList in Con1 it adds the item into the Session based Cart. However it does not update the Cart Preview. I need to call BindPreview() in Con2 in order to update the Cart Preview... Does that make sense? argh! any help much appreciated! PeteWeb Design Nottingham, UK Well I can call BindPreview() from m...

Dynamically Adding Controls to User Control
 Hi to all,first, here is what I have on my .ascx code:<asp:Label ID="lblCategory" runat="server" Text="Category" /> <asp:DropDownList ID="ddlCategory" runat="server"></asp:DropDownList><table>    <tr>        <td>            <asp:Label ID="lblItem" runat="server" Text="Item Question"/>        </td>    &n...

How to do post back in a dynamically created user control [provided that update panel is in the master page of the page where user control is added] {B/C of FileUpload Control}
I have a register my scriptmanager and place the update panel around contenttempalte for my pages int the master page. Now in one of my pages i am adding some user controls dynamically. In two of my user user controls I have FileUpload control which will not work under partial post back. So I want's full postback for them How could I do that {Provided that i am on the user control,which is inside the page ,and page have the contenttemplat...

Dynamically added user controls.. Restoring user control events ...
Hello, i ve made a simple aspx page to focus on the problem..In the aspx page i have a button that when it is pressed a user control is added dynamically, So if you press the button twice , two user controls will be added..Here is how i implement it :When a user control is added ,i add it to an arraylist (which contains all the  user controls that have been inserted to the page ) and this array list is then added to the session.So when the page is loaded ,i retrieve that arraylist back from the session i iterate through it ,and i enter again to the page all the previous added user contr...

web controls and web user control
i have a problem in a web form , I have a webform that contains a web user control which in turn contains a html input form ,the web control events don't fire up ,When I click button ,no effect ,I don't know what is the effect of the html form which is in the web user control on the web controls in the web form ?? can any one help me the web control event should be handle in the code behind page, is the event handler for the button you are clicking registered? You can try double clicking the button, should give you the default btn_click event handler in code behind. Also, make ...

Adding User Controls from Sting can the User Control contain a CompileWith Control directive?
I want to store my User Controls in a DB. So I working with ParseControl and passing it a string. However when I pass as string of a control with codebehind I get this error. My Code: Dim strControl1 As String = "<%@ Control Language=""VB"" AutoEventWireup=""false"" CompileWith=""Control1.ascx.vb"" ClassName=""Control1_ascx"" %>" & vbCrLf & "<div>Control 1<br /><asp:PlaceHolder ID=""Control1PlaceHolder"" Runat=""server""...

UpdatePanel within a web user control resets data of controls outside the user control
Hello, I have a usercontrol on my page which displays a running marquee text. It makes use of an UpdatePanel (within the user control) to refresh the marquee text (within the updatepanel) at periodic intervals. For this I added a timer control and its tick event as an async postback trigger for the UpdatePanel. I have a lot of textboxes and dropdown list boxes (all with runat=server) on the page outside the above user control. These are also contained in different UpdatePanels. Problem is, when the user control does a postback to refresh the marquee text, it causes some of...

Dynamic Control problem in Web user control
hi friends, i am developing an web user control.   in that i require to add RADEditor control dynamically when the user press ADD button.   But i am unable to retain the control when the page is post back.   Can anybode send me the sample..   Note that it should be in an Web User Control  Just to make sure, Did you place the code addinthe control dynamically inside the "if (!IsPostback)" inside the page_load? santhoshonet: hi friends, i am developing an web user control.   in that i require to add RADEditor control dynamically when the...

Dynamically adding User Controls or custom controls
Hi, I would like to know how to add Web User controls dynamically in asp.net 2.0. The user control consists of several labels,image buttons and link buttons.I can't add Web user controls dynamically to an aspx page while it is easy for a builtin control to add dynamically--=A J E E S H=-- Have you try to use Page.LoadControl method ??? you can use that like: PlaceHolder1.Controls.Add(Page.LoadControl("usercontrol.ascx")) hope that helps   As Andoko said, the key is in using PlaceHolder control.  Once you put this control in your desire place, you can add your user contro...

Web resources about - Controlling a variable numbers of controls dynamically added to a user control - asp.net.web-forms

Talk:Controlling for a variable - Wikipedia, the free encyclopedia
This article is within the scope of the WikiProject Statistics , a collaborative effort to improve the coverage of statistics on Wikipedia. If ...

Controlling What You Share on Timeline - Facebook
Facebook and Privacy hat eine Notiz mit dem Titel Controlling What You Share on Timeline geschrieben. Du kannst den vollständigen Text hier lesen. ...

VidReverse-Reverse Video by controlling video speed and by adding your own music to video easily on the ...
Get VidReverse-Reverse Video by controlling video speed and by adding your own music to video easily on the App Store. See screenshots and ratings, ...

Controlling a Computer with Eyes - Flickr - Photo Sharing!
Some folks know I work with assistive technology, and this shot is one from many I took in the last few days at a conference in Phoenix. This ...

4 year old little girl feeding and controlling 6 pitbulls - YouTube
As you see this video check out this other video of 2 kids playing with 3 huge pitbulls they just met in a pet store : https://www.youtube.c ...

Controlling the minds of cockroaches using Microsoft's Xbox Kinect
Late last year, you were introduced to real, live, remote-controlled cockroaches. Well, the insect hackers at the North Carolina State University ...

CONTROLLING CHAOS: Twitter's Wild Ride From Doodle To IPO
CONTROLLING CHAOS: Twitter's Wild Ride From Doodle To IPO Business Insider Australia “We apologise if the following profile lacks our usual ...

View from the Street: Power to the people controlling the power!
Your news of the day, reduced to a snarky rant.

Dying to be clean: The new technique for controlling feral cats
Cleanliness may be next to godliness unless you're a feral cat, in which case, cleanliness may get you a place next to god. In kitty heaven, ...

Man accused of 'controlling', beating wife faces ACT court
A man who allegedly controlled his wife’s life by beating her, taking her wages, checking her phone, stopping her from driving and indecently ...

Resources last updated: 12/13/2015 3:47:05 PM