Dynamic Controls and Best Way of doing it

moc2469

Active member
Joined
Jun 21, 2007
Messages
27
Programming Experience
1-3
Hi,

I am still having issues with my dynamic controls. Which is the proper way of doing it; (1) building controls through the Response.Write object e.g.(Response.Write("<td><INPUT type='text'></td>")), OR (2) Create the control and add to a collection e.g.(Dim txt As TextBox = New TextBox, Controls.Add(txt))?

Thanks in Advance,
Michael
 
I would say option 2, work with ASP.Net instead of Html. Search internet for "asp.net dynamic controls" there is much information about this topic.
 
I figured it out and you are correct, #2 is the way to do it. I will post code for everyone to see IT IS Easy to do.

First I created a new project, in the "Web Form Designer Generated Code" region I put Protected WithEvents btn As System.Web.UI.WebControls.Button, then in the InIt() section I have a Sub call named Load_Control(), Page_Load is empty.
VB.NET:
Private Sub Load_Control()
        Dim txt As TextBox = New TextBox
        txt.ID = "txtTest"
        PlaceHolder1.Controls.Add(txt)

        btn = New Button
        btn.ID = "btnTest"
        btn.Text = "Click Me"
        PlaceHolder1.Controls.Add(btn)
    End Sub

    Private Sub ButtonClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
        Get_Values()
    End Sub

Private Sub Get_Values()
        Dim temp As String
        temp = Request.Form().Item("txtTest")
    End Sub

That's it, the textbox and button are placed into the PlaceHolder1 and when the button i clicked I can use the Request.Form() object to get the entered value. This should be as simple for a dropdown too.
 
Back
Top