I'll use OnLoad to recreate the controls, but if you're not storing the # of controls in ViewState (you could put it into Session, a Cookie, etc. ) then you can do it in OnInit Either way should still fire the postback event asp:PlaceHolder id="phDdl" runat="server"> int DdlIndex { get { object o = ViewState"_ddlIndex"; if (o == null) { ViewState"_ddlIndex" = 0; } return (int)ViewState"_ddlIndex"; } set { ViewState"_ddlIndex" = value; } } protected override void OnLoad(EventArgs e) { base. OnLoad(e); for (int I = 0; I 0) { AddDropDownList(DdlIndex++); } } void AddDropDownList(int i) { var ddl = new DropDownList(); ddl.Id = string. Format("ddl{0}", i); ddl.
SelectedIndexChanged += ddl_SelectedIndexChanged; // add items phDdls. Add(ddl); }.
I'll use OnLoad to recreate the controls, but if you're not storing the # of controls in ViewState (you could put it into Session, a Cookie, etc. ) then you can do it in OnInit. Either way should still fire the postback event.
Adding event handlers to dynamically generated objects stored in a collection is the problem... it won't fire the event! – Sebastian Aug 10 '09 at 23:03 @Sebastian - It will fire if you follow the rules. Which basically are (a) add before the event would fire, (b) name the control the same thing, and (c) put it in the same place.
– Mark Brackett Aug 10 '09 at 23:12 Oh - and I guess re-attaching the event handler would be (d).... – Mark Brackett Aug 10 '09 at 23:14 Thanks Mark, good lead, but still no luck... I think I followed the rules you mentioned... didn't I? – Sebastian Aug 10 '09 at 23:36 No - you're trying to add the same instances of the controls that you used previously (the myDdlArray stored in Session). Frankly, I don't know how/if that would work - though I'd suspect large memory leaks as you're keeping the Page alive.
You need to recreate the controls, with the same ID - you can't just reuse them. – Mark Brackett Aug 10 '097 at 1:48.
You can simply have both dropdown lists exist in your asp code with only one visible on first page load. So something like... And then when, say the "More Elements" item selected, switch the dynamicDDL's visibility to true. Then on each postback, on the Page_Load event, check what the value of mainDDL is.
If it is 0, set dynamicDDL to have visible=true Edit: Okay, I took a stab at this. There is some headway in this, however, and maybe it will lead us to some clues. To start off, we DO need an array to store this.
We will need a static array of DDLs and a static integer to count our elements. These can be defined as... Private Shared ddl_arr As DropDownList() = New DropDownList(100) {} 'max, 100 ddls. Private Shared ddl_count As Integer = 0 Now, we'll need a panel to store our DDLs in.
This is simple asp scripting, such as... So now, on our page load we will want to load any of our dropdowns that we have saved so far. This can be coded in a way such as.. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System. EventArgs) Handles Me.
Load Try If TypeOf ddl_arr(0) Is DropDownList Then For Each ddl As DropDownList In ddl_arr add_ddl(ddl) Next End If Catch ex As Exception ' this is a bad idea, but for brevity.. End Try End Sub Our add_ddl method will simply add our new drop down to the parentPanel. Protected Sub add_ddl(ByVal ddl As DropDownList) Try parentPanel.Controls. Add(ddl) 'add any formatting you would like after each ddl here.
Catch ex As Exception End Try End Sub And finally, our method when we change the ddl. This creates a brand new ddl, gives it an id (and whatever properties you may want for it), adds it to the array, increments our counter, and adds it to the page.. Protected Sub mainDDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System. EventArgs) Handles mainDDL.
SelectedIndexChanged Dim newDDL As DropDownList = New DropDownList() newDDL. ID = "ddlID" & ddl_count 'we will need to store a new ID for each one. So, dynamically generate this.'continue to add properties to your ddl in this fashion... ddl_arr(ddl_count) = newDDL ddl_count = ddl_count + 1 add_ddl(newDDL) End Sub This method should definitely check for the end of the array (among other things), but lets keep things simple.
This code will only add a new DDL whenever you change the index of the ORIGINAL DDL. You will need to set each newly created DDL to have a method called (performing the above instructions) whenever the selected index changes for all of those newly crafted DDLs. Hopefully this gets you in the right direction.
This was way less organized than I'd hoped, sorry!
Thank you Chris, but actually what I meant with "and so on" is that you can have more than two ddl, every time to select one element from the list, another ddl is created, n number of times. Hope I made it clearer. – Sebastian Aug 10 '09 at 17:25 I've added more.
Hopefully this is helpful. – Chris Aug 10 '09 at 18:07.
Dynamic controls are pretty tricky to create. I would start by reading this.
Create and add the dropdowns during the Page_Init method and generate the names based on the backing data that you're gathering. When getting input from them, either read their returned values off of the page using the FindControl() method or by getting the returned value of the control from the Request.Form. FindControl uses the id of the control, and Requst.Name uses the client id - you can get this information using the same function you use to generate the names.
Overall, ASP. NET isn't very good at working with generated controls, but by keeping the data you're working with in a model or datastructure separate from the controls that are being generated it isn't difficult to both create the controls and retrieve the results.
I'm running some tests on this and you're damn right, ASP. Net is not good with generated controls! – Sebastian Aug 10 '09 at 20:58.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.