Dynamic creation of web controls in VB.NET 
Author Message
 Dynamic creation of web controls in VB.NET

I have created a "dropdownlist" server control in a web
application and want to add codes to it after making
changes on its content. I find that I can't do that as the
control ID cannot be recognized. Could you give me some
hints? Thanks.


Mon, 31 Oct 2005 09:52:52 GMT  
 Dynamic creation of web controls in VB.NET
How are you creating the dropdownlist? Dynamically in code? If so, is the
object's scope broad enough (i.e. it should be declared on the class level
as Protected ddlWhatever as DropDownList, not on the procedure level)?

Have you actually instantiated the dropdownlist (using New)?

Sorry, I'm not sure if you have checked these things, maybe post some code
and it will be clearer what is going wrong.


Quote:
> I have created a "dropdownlist" server control in a web
> application and want to add codes to it after making
> changes on its content. I find that I can't do that as the
> control ID cannot be recognized. Could you give me some
> hints? Thanks.



Mon, 31 Oct 2005 13:05:23 GMT  
 Dynamic creation of web controls in VB.NET
Thanks for your response.

Actually, I created 2 static web controls (Panel and
button named Start) in the web form with the code below:

***** start of code *****

Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents Panel As
System.Web.UI.WebControls.Panel
    Protected WithEvents ddl1 As New DropDownList()
    Protected WithEvents ddl2 As New DropDownList()
    Protected WithEvents btnStart As
System.Web.UI.WebControls.Button
    Protected WithEvents txt As New TextBox()

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.De{*filter*}StepThrough()> Private Sub
InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web
Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Master_SelectedIndexChanged(ByVal sender
As System.Object, ByVal e As System.EventArgs)
        ddl2.Items.Clear()
        Select Case ddl1.SelectedItem.Value
            Case "A"
                ddl2.Items.Add("A1")
                ddl2.Items.Add("A2")
            Case "B"
                ddl2.Items.Add("B1")
                ddl2.Items.Add("B2")
            Case "C"
                ddl2.Items.Add("C1")
                ddl2.Items.Add("C2")
        End Select
    End Sub

    Private Sub Slave_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)
        txt.Text = ddl2.SelectedItem.Value
    End Sub

    Private Sub btnStart_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnStart.Click
        ddl1.ID = "Master"
        ddl1.AutoPostBack = True
        ddl1.Items.Add("A")
        ddl1.Items.Add("B")
        ddl1.Items.Add("C")
        AddHandler ddl1.SelectedIndexChanged, AddressOf
Me.Master_SelectedIndexChanged
        Panel.Controls.Add(ddl1)

        ddl2.ID = "Slave"
        ddl2.AutoPostBack = True
        AddHandler ddl2.SelectedIndexChanged, AddressOf
Me.Slave_SelectedIndexChanged
        Panel.Controls.Add(ddl2)

        Panel.Controls.Add(txt)
    End Sub
End Class

***** end of code *****

After pressing the "Start" button, 2 dropdownlist and 1
textbox controls are created dynamically. However, after
change the content of the dropdownlist control, all
dynamic controls disappear. Could you please tell me why?

Thanks.

Quote:
>-----Original Message-----
>How are you creating the dropdownlist? Dynamically in
code? If so, is the
>object's scope broad enough (i.e. it should be declared
on the class level
>as Protected ddlWhatever as DropDownList, not on the
procedure level)?

>Have you actually instantiated the dropdownlist (using
New)?

>Sorry, I'm not sure if you have checked these things,

maybe post some code
Quote:
>and it will be clearer what is going wrong.



>> I have created a "dropdownlist" server control in a web
>> application and want to add codes to it after making
>> changes on its content. I find that I can't do that as
the
>> control ID cannot be recognized. Could you give me some
>> hints? Thanks.

>.



Sun, 06 Nov 2005 13:21:07 GMT  
 Dynamic creation of web controls in VB.NET
Thanks for your response.

Actually, I created 2 static web controls (Panel and button named Start)
in the web form with the code below:

***** start of code *****

Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents Panel As System.Web.UI.WebControls.Panel
    Protected WithEvents ddl1 As New DropDownList()
    Protected WithEvents ddl2 As New DropDownList()
    Protected WithEvents btnStart As System.Web.UI.WebControls.Button
    Protected WithEvents txt As New TextBox()

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.De{*filter*}StepThrough()> Private Sub
InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Master_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)
        ddl2.Items.Clear()
        Select Case ddl1.SelectedItem.Value
            Case "A"
                ddl2.Items.Add("A1")
                ddl2.Items.Add("A2")
            Case "B"
                ddl2.Items.Add("B1")
                ddl2.Items.Add("B2")
            Case "C"
                ddl2.Items.Add("C1")
                ddl2.Items.Add("C2")
        End Select
    End Sub

    Private Sub Slave_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)
        txt.Text = ddl2.SelectedItem.Value
    End Sub

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStart.Click
        ddl1.ID = "Master"
        ddl1.AutoPostBack = True
        ddl1.Items.Add("A")
        ddl1.Items.Add("B")
        ddl1.Items.Add("C")
        AddHandler ddl1.SelectedIndexChanged, AddressOf
Me.Master_SelectedIndexChanged
        Panel.Controls.Add(ddl1)

        ddl2.ID = "Slave"
        ddl2.AutoPostBack = True
        AddHandler ddl2.SelectedIndexChanged, AddressOf
Me.Slave_SelectedIndexChanged
        Panel.Controls.Add(ddl2)

        Panel.Controls.Add(txt)
    End Sub
End Class

***** end of code *****

After pressing the "Start" button, 2 dropdownlist and 1 textbox controls
are created dynamically. However, after change the content of the
dropdownlist control, all dynamic controls disappear. Could you please
tell me why? Do you have any solution for me?

Thanks.

*** Sent via Developersdex http://www.*-*-*.com/ ***
Don't just participate in USENET...get rewarded for it!



Sun, 06 Nov 2005 13:22:04 GMT  
 Dynamic creation of web controls in VB.NET
There is no state between page requests - every page gets recreated from scratch again.  Your code that creates the controls should be created in
Page_Load.  It might be easier for you to create the controls you need at design time, and set their Visible property to False if you don't need them.

There may be more clever ways to do this - I'd check in the dotnet.framework.aspnet forum or go directly to http://www.*-*-*.com/ .

--
Adam Braden, VB Team
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

Quote:
>Content-Class: urn:content-classes:message



>Subject: Re: Dynamic creation of web controls in VB.NET
>Date: Tue, 20 May 2003 22:21:07 -0700
>Lines: 122

>MIME-Version: 1.0
>Content-Type: text/plain;
>    charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>Thread-Index: AcMfWMg5RuwVFjVrRoqmXPd5Tx60kA==
>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
>Newsgroups: microsoft.public.dotnet.languages.vb
>Path: cpmsftngxa06.phx.gbl
>Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:109097
>NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
>X-Tomcat-NG: microsoft.public.dotnet.languages.vb

>Thanks for your response.

>Actually, I created 2 static web controls (Panel and
>button named Start) in the web form with the code below:

>***** start of code *****

>Public Class WebForm1
>    Inherits System.Web.UI.Page
>    Protected WithEvents Panel As
>System.Web.UI.WebControls.Panel
>    Protected WithEvents ddl1 As New DropDownList()
>    Protected WithEvents ddl2 As New DropDownList()
>    Protected WithEvents btnStart As
>System.Web.UI.WebControls.Button
>    Protected WithEvents txt As New TextBox()

>#Region " Web Form Designer Generated Code "

>    'This call is required by the Web Form Designer.
>    <System.Diagnostics.De{*filter*}StepThrough()> Private Sub
>InitializeComponent()

>    End Sub

>    Private Sub Page_Init(ByVal sender As System.Object,
>ByVal e As System.EventArgs) Handles MyBase.Init
>        'CODEGEN: This method call is required by the Web
>Form Designer
>        'Do not modify it using the code editor.
>        InitializeComponent()
>    End Sub

>#End Region

>    Private Sub Page_Load(ByVal sender As System.Object,
>ByVal e As System.EventArgs) Handles MyBase.Load

>    End Sub

>    Private Sub Master_SelectedIndexChanged(ByVal sender
>As System.Object, ByVal e As System.EventArgs)
>        ddl2.Items.Clear()
>        Select Case ddl1.SelectedItem.Value
>            Case "A"
>                ddl2.Items.Add("A1")
>                ddl2.Items.Add("A2")
>            Case "B"
>                ddl2.Items.Add("B1")
>                ddl2.Items.Add("B2")
>            Case "C"
>                ddl2.Items.Add("C1")
>                ddl2.Items.Add("C2")
>        End Select
>    End Sub

>    Private Sub Slave_SelectedIndexChanged(ByVal sender As
>System.Object, ByVal e As System.EventArgs)
>        txt.Text = ddl2.SelectedItem.Value
>    End Sub

>    Private Sub btnStart_Click(ByVal sender As
>System.Object, ByVal e As System.EventArgs) Handles
>btnStart.Click
>        ddl1.ID = "Master"
>        ddl1.AutoPostBack = True
>        ddl1.Items.Add("A")
>        ddl1.Items.Add("B")
>        ddl1.Items.Add("C")
>        AddHandler ddl1.SelectedIndexChanged, AddressOf
>Me.Master_SelectedIndexChanged
>        Panel.Controls.Add(ddl1)

>        ddl2.ID = "Slave"
>        ddl2.AutoPostBack = True
>        AddHandler ddl2.SelectedIndexChanged, AddressOf
>Me.Slave_SelectedIndexChanged
>        Panel.Controls.Add(ddl2)

>        Panel.Controls.Add(txt)
>    End Sub
>End Class

>***** end of code *****

>After pressing the "Start" button, 2 dropdownlist and 1
>textbox controls are created dynamically. However, after
>change the content of the dropdownlist control, all
>dynamic controls disappear. Could you please tell me why?

>Thanks.

>>-----Original Message-----
>>How are you creating the dropdownlist? Dynamically in
>code? If so, is the
>>object's scope broad enough (i.e. it should be declared
>on the class level
>>as Protected ddlWhatever as DropDownList, not on the
>procedure level)?

>>Have you actually instantiated the dropdownlist (using
>New)?

>>Sorry, I'm not sure if you have checked these things,
>maybe post some code
>>and it will be clearer what is going wrong.



>>> I have created a "dropdownlist" server control in a web
>>> application and want to add codes to it after making
>>> changes on its content. I find that I can't do that as
>the
>>> control ID cannot be recognized. Could you give me some
>>> hints? Thanks.

>>.



Mon, 07 Nov 2005 05:10:12 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Dynamic control creation and calling DOS programs from VB.

2. Licensing VB Controls for Dynamic creation at runtime

3. Dynamic Web Page Creation

4. Dynamic Controls Creation and Referencing the new Control.

5. Dynamic Control Creation, Controls object has no .Add ???

6. VB.NET: Steps for Converting a Windows .NET Application to a Web .NET Application

7. Dynamic Control Creation - Expand Form?

8. Dynamic Creation of Controls

9. Dynamic control creation

10. dynamic creation of controls?

11. VB6-Dynamic Control Creation

12. Dynamic control creation-HOW???

 

 
Powered by phpBB® Forum Software