Dynamic *not really dynamic* controls.

drdream

New member
Joined
Feb 13, 2006
Messages
4
Programming Experience
10+
Im slowly but surely making the transition to asp.net form ASP. And its frusturating to have to look things up for hours every time I want to do something. I could not find any info on this.

In my old asp days I had a function to create a textbox dynamically like

VB.NET:
Function TextBox(Name, Value, Class)
  ' dramatically oversimplified
   outStr = "<input type=text value=" & Value & "Class=" & Class & ">"
  TextBox = outStr
End Function

Now im trying to do the same thing with asp.net (perhaps this is real easy) .. Im trying to do something like this

VB.NET:
Public Shared Function AspNetTextBox(txName, txMessage, txRequired, txDataformat, classIn)
    Dim outStr as String
  outStr=" <asp:Textbox id=" & Functions.Quote(txName) & "runat=" & Functions.Quote("server") & "/>" & vbCrlf
  outStr = outStr & "<asp:RequiredFieldValidator runat=" & Functions.Quote("server") & vbCrlf
  outStr = outStr &  "id=" & Functions.Quote("reqName") & " ControlToValidate=" & Functions.Quote(txName)
  outStr=str & " ErrorMessage =" & Functions.Quote(txMessage)
  outStr=str & " display=" & Functions.Quote("Dynamic") & " />"
  return outStr
End function

But doing this type of stuff actually renders it on the browser instead of making the textbox and of course normal HTML ignores the asp: tags..


The purpose of these functions is to pretty much write out all that crap every time I want to add a textbox to a form.
Is this possible? or is there another way to create controls on the fly

tia
 
Here is what you can do in codebehind for example in form load event handler (doubleclick the form and you are there, at least in MS Visual Web Developer 2005 Express Edition), it should give you a start:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] t [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] TextBox
t.ID = [/SIZE][SIZE=2][COLOR=#800000]"IDtest23"
[/COLOR][/SIZE][SIZE=2]t.Text = [/SIZE][SIZE=2][COLOR=#800000]"test"
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].form1.Controls.Add(t)[/SIZE]
HOW TO: Dynamically Create Controls in ASP.NET with Visual Basic .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-US;317515
 
That looks nice but how do I reference it in the code.. I would like to have a function in the HTML... placeholders perhaps? This is what I want (or think I want)

<tr>
<td>
<%=Function.MakeControl(id, text, class, required)%>
</td>
</tr>
 
yes, you could put an asp placeholder there, and do "placeholderTxt.controls.add(t)"
 
My recommendation would be to use the placeholder from dbauer (it is free). I use this control everywhere. What I do is upfront I place this placeholder on my webpage, maybe the id is PH1 for example. Then when I want to build a web page, I do like JohnH described... for example:
VB.NET:
dim t as new System.Web.UI.WebControls.TextBox
t.id = "tb1"
me.PH1.controls.add(t)
t.text = "TEST"
the nice thing about the dbauer placeholder is that it will preserve your controls across postback. this is a must for forms that you build dynamically, but want to preserve the user typed-in values in your form across postback.
note that the asp:placeholder from microsoft will not do this. controls will be lost across postback so you would have to place them each time.

in case you're building a big page, I also will nest textbox controls inside of label controls, or nest labels inside of labels, etc.

here is an excerpt of one of my pages where we build a lengthy user-input form. as you'll notice we place a label into the base placeholder, then place 2 labels into that label, one for the left column and one for the right column, then we place labels and/or textbox controls into those sub-controls. the trick is in correctly setting the CSS classes to make it look pretty... I hope this helps. There's not a lot out there on building dynamic pages if you're not doing databinding:

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Add the title row
[/COLOR][/SIZE][SIZE=2]parLbl = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Label
parLbl.ID = "parLbl_title"
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].P0.Controls.Add(parLbl)
parLbl.CssClass = "authPL_Title"
chdLbl = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Label
chdLbl.ID = "chdLbl_title"
parLbl.Controls.Add(chdLbl)
chdLbl.CssClass = "authCL_Title"
chdLbl.Text = "Create CMP"
[/SIZE][SIZE=2][COLOR=#008000]'Add a linebreak
[/COLOR][/SIZE][SIZE=2]lineBreak = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Literal
lineBreak.ID = "lineBreak_title"
parLbl.Controls.Add(lineBreak)
lineBreak.Text = "<br/>"
[/SIZE][SIZE=2][COLOR=#008000]'Add the required fields message row
[/COLOR][/SIZE][SIZE=2]parLbl = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Label
parLbl.ID = "parLbl_req"
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].P0.Controls.Add(parLbl)
parLbl.CssClass = "authPL_Req"
chdLbl = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Label
chdLbl.ID = "chdLbl_req"
parLbl.Controls.Add(chdLbl)
chdLbl.CssClass = "authCL_Req"
chdLbl.Text = "Required fields are in <font color=""#ff3300"">red</font>"
[/SIZE][SIZE=2][COLOR=#008000]'Add a linebreak
[/COLOR][/SIZE][SIZE=2]lineBreak = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Literal
lineBreak.ID = "lineBreak_req"
parLbl.Controls.Add(lineBreak)
lineBreak.Text = "<br/>"
[/SIZE][SIZE=2][COLOR=#008000]'Add the description row
[/COLOR][/SIZE][SIZE=2]parLbl = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Label
parLbl.ID = "parLbl_desc"
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].P0.Controls.Add(parLbl)
parLbl.CssClass = "authPL_Desc"
ctlImg = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Image
ctlImg.ID = "ctlImg_desc"
parLbl.Controls.Add(ctlImg)
ctlImg.CssClass = "authCL_Desc_Image"
ctlImg.ImageUrl = "images/icons/" & gdsCom.getImage(gdsCom.translateForAppType("Folder"))
chdLbl = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Label
chdLbl.ID = "chdLbl_desc"
parLbl.Controls.Add(chdLbl)
chdLbl.CssClass = "authCL_Desc"
chdLbl.Text = "CMP - Contains information about an equipment change request"
[/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Add a linebreak
[/COLOR][/SIZE][SIZE=2]lineBreak = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Literal
lineBreak.ID = "lineBreak_desc"
parLbl.Controls.Add(lineBreak)
lineBreak.Text = "<br/>"
[/SIZE][SIZE=2][COLOR=#008000]'Add the CMPNumber row
[/COLOR][/SIZE][SIZE=2]parLbl = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Label
parLbl.ID = "CMPNumber"
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].P0.Controls.Add(parLbl)
parLbl.CssClass = "authPL_Prop"
chdLbl1 = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Label
chdLbl1.ID = "lbl1_CMPNumber"
parLbl.Controls.Add(chdLbl1)
chdLbl1.CssClass = "authCL1_Prop"
[/SIZE][SIZE=2][COLOR=#008000]'Set the text of the first property child label
[/COLOR][/SIZE][SIZE=2]chdLbl1.Text = "<font color=""red"">CMP Number*</font>"
chdLbl2 = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Label
chdLbl2.ID = "lbl2_CMPNumber"
parLbl.Controls.Add(chdLbl2)
chdLbl2.CssClass = "authCL2_Prop"
[/SIZE][SIZE=2][COLOR=#008000]'Add an input device as a control to the second property
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'child label (ID = ctl_<Name in the XML file>)
[/COLOR][/SIZE][SIZE=2]ctlTextBox = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.TextBox
ctlTextBox.ID = "ctl_CMPNumber"
chdLbl2.Controls.Add(ctlTextBox)
[/SIZE][SIZE=2][COLOR=#008000]'Set the style of the textbox
[/COLOR][/SIZE][SIZE=2]ctlTextBox.CssClass = "authCL2_Prop_TextBox"
ctlTextBox.MaxLength = 5
[/SIZE][SIZE=2][COLOR=#008000]'Set the textbox to uppercase
[/COLOR][/SIZE][SIZE=2]ctlTextBox.Style.Add("text-transform", "uppercase")
[/SIZE][SIZE=2][COLOR=#008000]'Set the default button for the textbox
[/COLOR][/SIZE][SIZE=2]gdsCom.SetDefaultButton([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Page, ctlTextBox, defaultButton)
ctlValidator = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.RequiredFieldValidator
ctlValidator.ID = "vd_CMPNumber"
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].P0.Controls.Add(ctlValidator)
ctlValidator.ControlToValidate = "ctl_CMPNumber"
ctlValidator.Text = "*"
ctlValidator.ErrorMessage = "CMP Number is required."
ctlValidator.Display = ValidatorDisplay.None
ctlValidator.EnableClientScript = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ctlValidator2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.CompareValidator
ctlValidator2.ID = "vd2_CMPNumber"
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].P0.Controls.Add(ctlValidator2)
ctlValidator2.ControlToValidate = "ctl_CMPNumber"
ctlValidator2.Text = "*"
ctlValidator2.ErrorMessage = "CMP Number must be a number."
ctlValidator2.Display = ValidatorDisplay.None
ctlValidator2.EnableClientScript = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]ctlValidator2.ValueToCompare = 1
ctlValidator2.Type = ValidationDataType.Integer
ctlValidator2.Operator = ValidationCompareOperator.GreaterThan
[/SIZE][SIZE=2][COLOR=#008000]'Add a linebreak to the property parent label
[/COLOR][/SIZE][SIZE=2]lineBreak = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Literal
lineBreak.ID = "lineBreak_CMPNumber"
parLbl.Controls.Add(lineBreak)
lineBreak.Text = "<br/>"
[/SIZE][SIZE=2][COLOR=#008000]'Add the CMPDescription row
[/COLOR][/SIZE][SIZE=2]parLbl = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Label
parLbl.ID = "CMPDescription"
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].P0.Controls.Add(parLbl)
parLbl.CssClass = "authPL_Prop"
chdLbl1 = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Label
chdLbl1.ID = "lbl1_CMPDescription"
parLbl.Controls.Add(chdLbl1)
chdLbl1.CssClass = "authCL1_Prop"
[/SIZE][SIZE=2][COLOR=#008000]'Set the text of the first property child label
[/COLOR][/SIZE][SIZE=2]chdLbl1.Text = "<font color=""red"">CMP Description*</font>"
chdLbl2 = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Label
chdLbl2.ID = "lbl2_CMPDescription"
parLbl.Controls.Add(chdLbl2)
chdLbl2.CssClass = "authCL2_Prop"
[/SIZE][SIZE=2][COLOR=#008000]'Add an input device as a control to the second property
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'child label (ID = ctl_<Name in the XML file>)
[/COLOR][/SIZE][SIZE=2]ctlTextBox = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.TextBox
ctlTextBox.ID = "ctl_CMPDescription"
chdLbl2.Controls.Add(ctlTextBox)
[/SIZE][SIZE=2][COLOR=#008000]'Set the style of the textbox
[/COLOR][/SIZE][SIZE=2]ctlTextBox.CssClass = "authCL2_Prop_TextBox"
ctlTextBox.MaxLength = 100
[/SIZE][SIZE=2][COLOR=#008000]'Set the textbox to uppercase
[/COLOR][/SIZE][SIZE=2]ctlTextBox.Style.Add("text-transform", "uppercase")
[/SIZE][SIZE=2][COLOR=#008000]'Set the default button for the textbox
[/COLOR][/SIZE][SIZE=2]gdsCom.SetDefaultButton([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Page, ctlTextBox, defaultButton)
ctlValidator = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.RequiredFieldValidator
ctlValidator.ID = "vd_CMPDescription"
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].P0.Controls.Add(ctlValidator)
ctlValidator.ControlToValidate = "ctl_CMPDescription"
ctlValidator.Text = "*"
ctlValidator.ErrorMessage = "CMP Description is required."
ctlValidator.Display = ValidatorDisplay.None
ctlValidator.EnableClientScript = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Add a linebreak to the property parent label
[/COLOR][/SIZE][SIZE=2]lineBreak = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Web.UI.WebControls.Literal
lineBreak.ID = "lineBreak_CMPDescription"
parLbl.Controls.Add(lineBreak)
lineBreak.Text = "<br/>"
[/SIZE]
 
forgot to mention... controls in the placeholder can be accessed by their ID during a postback. for example, in the Page_PreRender event, you could do something like this:
dim tb as system.web.ui.webcontrols.textbox = me.PH1.FindControl("TextBox1")
dim sUserEnteredText = tb.text
'do stuff w/the text that the user entered
 
Back
Top