Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic ID for DropDown Lists

Status
Not open for further replies.

Turkbear

Technical User
Mar 22, 2002
8,631
US
Hi,
Just starting to move from 'classic' ASP to .NET and have a question ( actually many, but this one is hard to find an answer to in the various .NET sample sites)..

I would like to populate several <asp:DropDownList
objects but I will not know how many I need until run-time..
Is there a way to dynamically create these so that the ID is unique and therefore I can populate them with a set of databind statements? ( I Can set up the values needed
as ArrayLists but , since I cannot seem to assign the ID to the DropDownlist in code, I can't do anything with those values)

In our .asp versions, we use Response.Write statements to dynamically build <SELECT and < OPTION statements and concatenate the name for the <Select as needed - I am trying to avoid using this method, if possible,in our .NET
stuff..





[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
You can assign the ID with unique values:
Code:
..Use the Page_Init event for this ..
Dim numOfDDLs, i as Integer

For i = 1 to numOfDDLs
   Dim DDL as New DropDownList
   DDL.ID = "MyDDL" + i.ToString
   ''Add Any Event Hanlers Here...
   Page.FindConrol("Form1").Controls.Add(DDL)
Next
Jim
 
Hi,
Thanks...I'll give it a try..




[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Hi,
OK, newness expands..

How is the Page_Init event triggered?

( I am assuming it is very different from Page_Load, and is not a Sub or Function either)

Thanks again...



[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Hi,
OK, I get that..But..How, in my aspx page do I place that code..It is using VB as its scripting language and I cannot
seem to figure out how to declare it
( Sub Page_Init fails with a 'cannot use a keyword as an identifier' error)

I think I am missing a concept here...



[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Are you using Visual Studio to create this page? If so, what version?
 
Hi,
Nope..Custom coding with Text editor



[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
uggggggg ... you are a glutton for punishment I see :)

Code:
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
......
End Sub
 
Hi,
Thanks..Yep..sometimes doing it the hard way builds character ( most times it just builds piles of .BAK files)

It now results in another error:
Code:
Compiler Error Message: BC30456: 'FindConrol' is not a member of 'System.Web.UI.Page'.

Source Error:

 

Line 9:     DDL.ID = "MyDDL" + i.ToString
Line 10:    ''Add Any Event Hanlers Here...
[COLOR=red]
Line 11:    Page.FindConrol("Form1").Controls.Add(DDL)
[/color]
Line 12: Next
Line 13: End Sub

Apparently I have missed something else..( Using .NET 1.1.432, by the way)

( Really appreciate the efforts..)





[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Hi,
OOPS, apparently on Friday, neither one of us can spell

Once
Page.FindConrol("Form1").Controls.Add(DDL)
became
Page.FindControl("Form1").Controls.Add(DDL)

the error disappeared..

Will now move on to using it..

Thanks again and have a great weekend..




[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Hi,
Seems to want to work, but executes twice, so a 'duplicate ID' error is thrown..

Why twice??



[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Hi,
The
For loop..
When I use
Code:
For i = 1 to 5
   Dim DDL as New DropDownList
   DDL.ID = "MyDDL" + i.ToString
   ''Add Any Event Hanlers Here...
   DDL.AutoPostBack="True"
   'Page.FindConrol("Form1").Controls.Add(DDL)
   Response.Write(DDL.ID + "<BR>")
   
Next
Using the Response.Write to debug, it produces:
Code:
MyDDL1
MyDDL2
MyDDL3
MyDDL4
MyDDL5
MyDDL1
MyDDL2
MyDDL3
MyDDL4
MyDDL5


Oddly, that same code, when placed in my Page_Load sub, produces just 5 DDL Ids, but, in that sub the
Page.FindConrol("Form1").Controls.Add(DDL)

fails...



[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Normally I would tell you to trace throught the code, but without VS, you can't do that. I don't know why the init event is firing twice.
The findconrol("form1").. wil only work if your form is named "Form1" . It is named that by default with VS
 
Hi,
I named it that to match your code, but the double-firing is a mystery...

If it helps, here is the full stack trace:
Code:
Multiple controls with the same ID 'MyDDL1' were found. FindControl requires that controls have unique IDs. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: Multiple controls with the same ID 'MyDDL1' were found. FindControl requires that controls have unique IDs.

Source Error: 


Line 11:    ''Add Any Event Hanlers Here...
Line 12:    DDL.AutoPostBack="True"
Line 13:    Page.FindControl("Form1").Controls.Add(DDL)
Line 14:    'Response.Write(DDL.ID + "<BR>")
Line 15:    
 

Source File: C:\inetpub\[URL unfurl="true"]wwwroot\Net\Test1\TestParamsCase.aspx[/URL]    Line: 13 

Stack Trace: 


[HttpException (0x80004005): Multiple controls with the same ID 'MyDDL1' were found. FindControl requires that controls have unique IDs.]
   System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls) +134
   System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls) +203
   System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +164
   System.Web.UI.Control.FindControl(String id) +9
   ASP.TestParamsCase_aspx.Page_Init(Object sender, EventArgs e) in C:\inetpub\[URL unfurl="true"]wwwroot\Net\Test1\TestParamsCase.aspx:13[/URL]
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.Control.OnInit(EventArgs e) +67
   System.Web.UI.Control.InitRecursive(Control namingContainer) +240
   System.Web.UI.Page.ProcessRequestMain() +174

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573




[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Hi,
Mystery of double loop solved..
From some other sample app I used as a Cut-and-Paste source
template, this Meta tag had the AutoEventWriteup set to True ( not even sure what this does, it was on the sample page I used, so I kept it)Once I changed it to this:
Code:
<%@ Page Language="VB" debug="true" AutoEventWireup="False" %>

No more duplicates and 5 lists get created ( at least their bare-bones structure does..Now, I need to find out how to assign the datasets, etc..

Thanks...







[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Since you don't have VS, you should get MSDN help if you can:
When AutoEventWireup is true, ASP.NET does not require events to specify event handlers like Page_Load or Page_Init. This means that the Handles keyword in Visual Basic is not required in the server script in the Web Form page.

By default, when the ASP.NET Web application is created in Visual Studio, the value of the AutoEventWireup attribute is set to false in the .aspx page or .ascx control, and event handlers are not automatically created. Do not set AutoEventWireup to true if performance is a key consideration.

..Now, I need to find out how to assign the datasets, etc..

In the code from before add...(after you create a dataset)
DDL.DataSoure = "myDataSet"
DDL.DataTextField = "FieldYouWantToShow"
DDL.DataValueField = "UsuallyTheIDBehindTheData"
DDL.DataBind

Jim
 
Hi,
Thanks for your patience -

( I do have MSDN Help, and should have looked that one up
once I saw it was a factor in the problem..)



[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Oh.. well that's good you have MSDN to refer too.. It will be very helpful, especially since you aren't using VS.

Glad to help...and good luck


Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top