Adding dropdownlist to webform dynamically

tverney

Newcomer
Joined
Nov 30, 2004
Messages
6
I am trying to add a dropdownlist to a web form and failing

I have the following code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim x As New DropDownList()
With x
.Font.Bold = True
.Style.Add("position", "absolute")
.Style.Add("left", "420px")
.Style.Add("top", "50px")
.Style.Add("Width", "250px")
End With
Controls.Add(x)
End Sub

I recieve the following message:

Control '_ctl0' of type 'DropDownList' must be placed inside a form tag with runat=server.


If I change:
Dim x As New DropDownList()
To
Dim x As New Label() it runs

Any thoughts?
Thanks!
 
Is there a reason why you can't have the control already on the page and just change it's visiblity states? That would be a lot easier to wire up events to also.
 
Found the answer

bri189a said:
Is there a reason why you can't have the control already on the page and just change it's visiblity states? That would be a lot easier to wire up events to also.
Hey, thanks for the response.

Yes, I'm writing web front end to launch a Crystal Report. The Crystal report takes parameters. The front end will allow the user to input the parameters and then launch the report.

The only rub here is that there are several hundred, yes, several hundred different reports that need input screens. Didn't write them, just have to manage them.

I've written the code that will interrogate a report for it's parameters and insert records in the appropriate SQL tables.

BTW, found the answer to my question. The corrected code follows. The drParm datarow contains information about the combobox properties.

Sub AddComboBox(ByVal drParm As DataRow)
Dim dsParms As New DataSet()
dsParms = o_DataExpert.GetSPData(drParm.Item("ParmCommand"), dsParms)

Dim x As New DropDownList()

With x
'.Font.Bold = True
.Style.Add("position", "absolute")
.Style.Add("left", drParm.Item("ParmLeft"))
.Style.Add("top", drParm.Item("ParmTop"))
.Style.Add("Width", drParm.Item("ParmWidth"))
.DataSource = dsParms.Tables(drParm.Item("ParmCommand"))
.DataValueField = dsParms.Tables(drParm.Item("ParmCommand")).Columns(0).ColumnName() ' typically an ID Field
.DataTextField = dsParms.Tables(drParm.Item("ParmCommand")).Columns(1).ColumnName() ' what user will see
.DataBind()
.ID = drParm.Item("ParmName")
End With
'this is the line that was missing
Page.FindControl("Form1").Controls.Add(x)
' a couple of links that provided guidance
'http://www.pcreview.co.uk/forums/thread-1261472.php
'http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.Aspx?ArticleID=4322
End Sub
 
Back
Top