Last post Mar 27, 2009 10:55 AM by urenjoy
Jun 05, 2008 04:17 AM|antonyvijayan|LINK
This might be useful for someone who strucks with create dynamic dropdownlist/ controls. In ASP.Net is page is stateless. when you are creating the dynamic dropdownlist control following issues you might be facing,
1. After loaded the control, on selected index changed event the control will be disappear.
2. The selectedIndexChanged raised event might not be fired when selection of Dropdownlist.
3. Locate the exact dropdownlist which you created dynamically.
4.Placing this dynamic controls in exact location in the page.
Solution: You have to re-create the control and load the data on each postback. Also maintain the numbr of dropdownlist count in viewstate. Go throught the code.
NOTE: Dont forget to mark as ANSWER and just do reply if it is helpful for you.
I gave you the source code below. The Scenario is first loading the product list. According the selection of product subsequent (
"n" Number ) classification will be loaded in the dropdownlist.
File Name : default2.aspx
File Name : default2.aspx.vb
_ddlCounter = 0
_ddlCounter = _ViewState()
_ddlCounter = _ddlCounter + 1
lblError.Text = ex.Message
createDropdownList(i - 1)
CreateSubClassification(i - 1)
clsSearch.t_UserID = PersonnelID
PermActionCategory = clsSearch.t_ACLCategoryIMS
PermAction = clsSearch.t_ACLIMSViewIncidents
SQLProduct = "Select * from productTable"
dsProduct = SqlHelper.ExecuteDataset(ConnectionString, CommandType.Text, SQLProduct)
ddlProduct.DataSource = dsProduct
'you load the first child level data
dsParentClassification =clsClassification.ParentClassification(ddlProduct.SelectedItem.Value) ' Load data for the first level of Child
_ddlSearchByArray(iNdex).DataSource = dsParentClassification
_ViewStateCounter = _ddlCounter
_ddlValue = _ddlTblTd.SelectedItem.Value
dsSubClassification = clsClassification.SubClassification(_ddlValue)
_ddlSearchByArray(Index).DataSource = dsSubClassification
asp .net ... c# ... SQL
dynamic template in datalist
Dynamic Method Name
ASP net 2.0
ASP NET2 .0
dynamic web survey
dynamic loading controls
Add control dynamically .NET
ASP 2.0 Validation Controls
dynamically created usercontrol issues
dynamic control creation
Adding controls dynamically
controls html dynamically li ul
Dynamic Object Instantiation Runtime
Create a Folder Dynamically in ASP.NET
dynamic code generation
add link button dynamically
ASP .NET Dynamic Controls ViewState
asp.net 2.0 controls
Dynamically created controls
aspx dynamically created controls
Jun 05, 2008 12:23 PM|pavankumarkavety|LINK
hi antony can u please help me how to add selected index changed event to dynamic dropdownlists.i ve added dynamic dropdownlists to a place holder.the code i ve written to create dynamic dropdownlists is below.
noofcolulmns = dsExcelColumns.Tables.Rows.Count;
ddl.DataSource = dsExcelColumns;
//slection changed event which is not firing.
so can u tell me wats wrong in my code.
and can u give me da code in c#
Jun 05, 2008 01:06 PM|antonyvijayan|LINK
Change AutoPostBack to true
ddl.AutoPostBack = true;
Let me know if you have queries.
Jun 05, 2008 01:46 PM|pavankumarkavety|LINK
ya already have done that ..i kept it false as i was loosing the viewstate and let i finish the other work apart from this in this one....:)
Jun 05, 2008 02:40 PM|antonyvijayan|LINK
I m not clear about your reply.
Jun 05, 2008 02:41 PM|antonyvijayan|LINK
i dont understand your reply. still you have the same issue ?
Jun 05, 2008 02:49 PM|pavankumarkavety|LINK
ya still im facing with the same pb...the thing is when im setting autopostback is equal to true im loosing the state of the controls and also it is not at all going to the breakpoint of the selectionchanged event.
Jun 05, 2008 03:06 PM|antonyvijayan|LINK
Yup. You have to note down the points,
1. create the controls in Postback method.
2. go through the code which i posted.
3.everytime you have create the controls in the postback.( This depends on your scanrio)
4. Keep the number of dropdownlist in Viewstate.
Like , Viewstate.Add(Key, value)
5.Read the value in next postback( viceversa)
If you want to run the posted code, remove the database part in the function(createdropdownlist) and add the item manually(_ddlsearchArray(idex).items.insert(0,"test") and do debug. hope you understand clearly.
Mar 27, 2009 10:55 AM|urenjoy|LINK
Here is another good example:
ASP net 2.0
dynamic control creation