Creating dynamic controls and getting their values

 Hi everyone,

i have to create a number of dynamic controls (dropdownlist and textboxes) acording to a number inputed by the user.

i've read some articles, posted some posts, but aynone has truly help me, so i'm asking if someone can hepl me beeing objective.

i've put the code in the page_load, and in the the page_init (not on both simultaneous), but i don't know in which one is right.

The code i putted on the Page_Load was this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        If (Page.IsPostBack) Then


            Dim sConnStr As String = "Data Source=VSQL1;Initial Catalog=VarelaBusLog;Integrated Security=True"
            Dim cnBKTest As New Data.SqlClient.SqlConnection(sConnStr)
            Dim cmdTest As New Data.SqlClient.SqlCommand("tipos_percurso", cnBKTest)


            Dim leitor As Data.SqlClient.SqlDataReader

            Dim i As Integer

            Dim item As New ListItem()


            cnBKTest.Open()


            For i = 1 To txtNumber.Text
                Dim MyDDL2 = New DropDownList
                Dim tb = New TextBox
                tb.ID = "nada" & i
                MyDDL2.ID = "nuno" & i
                leitor = cmdTest.ExecuteReader()
                Do While leitor.Read()
                    item = New ListItem()
                    item.Text = leitor("descricao")
                    item.Value = leitor("id")
                    MyDDL2.Items.Add(item)
                Loop
                leitor.Close()
                Panel1.Controls.Add(MyDDL2)
                Panel2.Controls.Add(tb)
                Dim MyLiteral = New LiteralControl
                MyLiteral.Text = "<BR><BR>"
                Panel1.Controls.Add(MyLiteral)

            Next

            cnBKTest.Close()

        End If

    End Sub

 

Then when i read and other article i puted on page_init with some minor changes (in bold), because i was getting this error:  Conversion from string "" to type 'Integer' is not valid,  for this line of the code "For i = 1 To txtNumber.Text". So i changed and got this code for Page_init

 

If (Page.IsPostBack) Then

 
            Dim sConnStr As String = "Data Source=VSQL1;Initial Catalog=VarelaBusLog;Integrated Security=True"
            Dim cnBKTest As New Data.SqlClient.SqlConnection(sConnStr)
            Dim cmdTest As New Data.SqlClient.SqlCommand("tipos_percurso", cnBKTest)

            Dim n_linhas As Integer

           n_linhas = Convert.ToInt32(txtNumber.TextMode)

            Dim leitor As Data.SqlClient.SqlDataReader

            Dim i As Integer

            Dim item As New ListItem()


            cnBKTest.Open()


            For i = 1 To n_linhas

                Dim MyDDL2 = New DropDownList
                Dim tb = New TextBox
                tb.ID = "nada" & i
                MyDDL2.ID = "nuno" & i
                leitor = cmdTest.ExecuteReader()
                Do While leitor.Read()
                    item = New ListItem()
                    item.Text = leitor("descricao")
                    item.Value = leitor("id")
                    MyDDL2.Items.Add(item)
                Loop
                leitor.Close()
                Panel1.Controls.Add(MyDDL2)
                Panel2.Controls.Add(tb)
                Dim MyLiteral = New LiteralControl
                MyLiteral.Text = "<BR><BR>"
                Panel1.Controls.Add(MyLiteral)

            Next


            cnBKTest.Close()


        End If


 

 In both cases i'm able to create the dll's and even an Postback the controls saty on the page, that was exactly what i wanted.

Then the other problem is to get the values of the dropdownlist and the textboxes. Someone told me it could be with something like this Panel1.FindControl("myTextBox"), but then someone told me it should be with a recursion, something like this

 
For Each child as Control in PlaceHolder.Controls
    Dim foundDropDownList as DropDownList = CType(child, DropDownList)
  ' Put code to send you data's to the database here but not that this code is not tested
  Next 

 So i don't know what do have to do.

Can somenone please HELP ME??? PLEASE, i'm desperate

 

I know that i have to create the controls in Page_load or Page_init (i don't know wich), then use something like a view state .

Please can anyone help me?

I would really appreciate  

0
wop_nuno
10/24/2007 9:47:52 AM
asp.net.web-forms 93655 articles. 5 followers. Follow

39 Replies
817 Views

Similar Articles

[PageSpeed] 47

Hi wop_nuno,

Have you come across the infiniteLoop page?

They have some sweet TRULY UNDERSTANDING articles.

 

I hope you haven't read them, and I hope that if you do .... all will become clear Smile


If they don't answer your questions, please reply!

Kind regards,
Wim


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/24/2007 3:03:09 PM

 Hi deblendewim,

 Thank you for responding to my post.

The 1st one i already knew, but the second one i didn't.

But the honest truth is that i found both of them truly confusing.

Can you still help me?

 

Thanks.
 

0
wop_nuno
10/24/2007 3:23:52 PM

Hi wop_nuno,

Yes the articles can be confusing ... I think it's because he mixes his articles with a lot of humor AND he has funny examples
Nevertheless ... they gradually improved my understandings on the topics.

 

Ok ... so you have 2 problems:

  1. Where to add the controls to the control-tree? (oninit/onload)
  2. How to access the properties of the dynamic controls?

1. -----> I would add them to the tree in the OnInit event of the page. I think that this way viewstate gets less dirty. And viewstate still tracks the modifications to the controls by the user.

2. -----> I would go for the FindControl method of the container that holds the controls. I think it's better to fetch the control directly out of the collection instead of looping through the collection.

 

If you have specific questions/remarks please do so!

 

=====> EDIT <=====
Did you do the following explicitly: Putting the code in a IF ISPOSTBACK THEN? You did it because you don't want to create the controls on an initial load? right?

And euhm, you can also use the Page_Init event, you don't need to override the OnInit of the page.

Kind regards,
wim.


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/25/2007 8:34:38 AM

 Hi deblendewim,


First of all i want star by thanking you for your help. I really appreciate.

1-------

for the first part of the problem, i 've decided to create my controls on Page_init (using VB code). So my code to create them is:

 Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init


        If (Page.IsPostBack) Then

            Dim sConnStr As String = "Data Source=VSQL1;Initial Catalog=VarelaBusLog;Integrated Security=True"
            Dim cnBKTest As New Data.SqlClient.SqlConnection(sConnStr)
            Dim cmdTest As New Data.SqlClient.SqlCommand("tipos_percurso", cnBKTest)


            Dim leitor As Data.SqlClient.SqlDataReader

            Dim i As Integer

            Dim item As New ListItem()


            cnBKTest.Open()


            For i = 1 To Request.Form("txtNumber")

                Dim MyDDL2 = New DropDownList
                Dim tb = New TextBox
                tb.ID = "nada" & i
                MyDDL2.ID = "nuno" & i
                leitor = cmdTest.ExecuteReader()
                Do While leitor.Read()
                    item = New ListItem()
                    item.Text = leitor("descricao")
                    item.Value = leitor("id")
                    MyDDL2.Items.Add(item)
                Loop
                leitor.Close()
                Panel1.Controls.Add(MyDDL2)
                Panel2.Controls.Add(tb)
                Dim MyLiteral = New LiteralControl
                MyLiteral.Text = "<BR><BR>"
                Panel1.Controls.Add(MyLiteral)

            Next


            cnBKTest.Close()


        End If
 

2----
 

For the second part of the problem, i have a button that when i click it it will insert the values into the data base.

I was thinking of doing something like this to my code. Can you see if it's right or not, please?

Protected Sub Enviar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Enviar.Click

        Dim sConnStr As String = "Data Source=VSQL1;Initial Catalog=VarelaBusLog;Integrated Security=True"
        Dim cnBKTest As New Data.SqlClient.SqlConnection(sConnStr)
        Dim cmdTest As New Data.SqlClient.SqlCommand("INS_lines_bord", cnBKTest)

        cmdTest.CommandType = Data.CommandType.StoredProcedure

 
        Dim trans As System.Data.SqlClient.SqlTransaction = Nothing
        Dim i As Integer


        Try

            cnBKTest.Open()

           'trans = cnBKTest.BeginTransaction()


           
            cmdTest.Parameters.Add("@n_livro", Data.SqlDbType.Int)
            cmdTest.Parameters.Add("@drop", Data.SqlDbType.Int)
            cmdTest.Parameters.Add("@hora", Data.SqlDbType.VarChar)
            cmdTest.Parameters.Add("@tbox", Data.SqlDbType.VarChar)

            For i = 1 To txtNumber.Text

                cmdTest.Parameters("@n_livro").Value = n_livro.text
                cmdTest.Parameters("@drop").Value = Request.Form("nuno" & i)
                cmdTest.Parameters("@hora").Value = hora.text
                cmdTest.Parameters("@tbox").Value = Request.Form("nada" & i)
                cmdTest.ExecuteNonQuery()

            Next

            trans.Commit()
            cmdTest.ExecuteNonQuery()
            cnBKTest.Close()



        Catch ex As Exception

            lblerro.Text = ex.Message

            If trans IsNot Nothing Then trans.Rollback()

        End Try


    End Sub

 

To get the values and insert into the data base i was thinking of doing the code in bold.

Can you continue to help me?

 Again, thanks a lot!

Nuno
 

0
wop_nuno
10/25/2007 10:43:57 AM

Hi Nuno,

About the code in bold, is it working?

  1. I see that you put the controls in a panel. Isn't this better code:
    cmdTest.Parameters("@drop").Value = Panel1.FindControl("nuno" & i)

  2. After your For-Next loop you have another ExecuteNonQuery. Is this one nececarry?


  3. I haven't worked with transactions like this before .... I suggest that you test this thoroughly. (check if it rolls back etc)

Kind regards,
Wim


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/25/2007 11:12:02 AM

 Hi Wim,

It's with some happiness  that i must say that my code is working.

But before i say to you a new problem that i have, let's go by your steps.

1 - If i change my code in bold to the one you gave me i get this error :"Failed to convert parameter value from a TextBox to a String"  (when i use this  Panel1.FindControl("nuno" & i)".  But if i don't change it works just fine!

2 - You're right, i don't need an other ExecuteNonQuery. Thank you for that.

3 - I only tried my code without the transactions because if i use them i gt this error : "ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized."

If i don't use them it works fine. Do you think i have to use it or not?

 ---------------------------------------------------

Now for my new problem:

I now see that i have to create not one, but two dropdownlist, but the problem is that the second dropdownlist will be bind depending on the value of the first one?

Because the value i have to insert into the Data base is the value of the second dropdownlist.

Can i do this with dynamic fields, or i'm i crazy?

 
Thank you.

I'm sorry if my English is sometimes understandable, but it's because i'm portuguese.....

But did you understand my new problem?

 
Again, thanks a lot.
 

 

0
wop_nuno
10/25/2007 12:11:51 PM

Hi Nuno,

wop_nuno:
:"Failed to convert parameter value from a TextBox to a String"

Ok this is my bad. When you use the findcontrol you just retrieve the object. That object needs to be casted to the right type, and then you can access the right property of it.

So instead of using Panel1.FindControl("myControlId") you should do this:

for a textbox: ctype(Panel1.FindControl("myTextBoxControlId") , TextBox).Text

for a dropDownList: ctype(Panel1.FindControl("myDDLControlId") , DropDownList).SelectedValue

 

wop_nuno:
I only tried my code without the transactions because if i use them i gt this error : "ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized."

I think this could be caused by the fact that you didn't assign your SqlTransaction to the SqlCommand.Transaction. More info on how to work with transactions here: http://www.sql-server-performance.com/articles/asp_ado/ado_transactions_p1.aspx . Please compare the examles with your code.

 

wop_nuno:
If i don't use them it works fine. Do you think i have to use it or not?

I think that working with them is better! Let's say that something goes wrong in the middle of the query execution. When using transactions, you can rollback the half-processed query.

 

 

wop_nuno:

Now for my new problem:

I now see that i have to create not one, but two dropdownlist, but the problem is that the second dropdownlist will be bind depending on the value of the first one?

Because the value i have to insert into the Data base is the value of the second dropdownlist.

Can i do this with dynamic fields, or i'm i crazy?

Offcourse you can do this ... it's just a question of finding the right control and selecting the right value.

Make sure you have some good naming conventions for the ID's of the dynamic controls. Because those names you'll be using to find the controls.

 

Kind regards,
Wim


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/25/2007 12:56:04 PM

 Hi Wim,


"Offcourse you can do this ... it's just a question of finding the right control and selecting the right value.

Make sure you have some good naming conventions for the ID's of the dynamic controls. Because those names you'll be using to find the controls."

Ok. I already know how to create the controls in the Page_Init, but i do i get this going?

With this line i can get the first dropdown to postback : " MyDDL2.AutoPostBack = "True" ".

My though is to create an other panel where the second dropdown will be located (easy to do).

Can i obtain in Page_Init using the command "Request.Form("nuno" & i)" and get the value of the dropdown and then bind the second one?

 Thanks.
 

 

0
wop_nuno
10/25/2007 2:12:43 PM

Hi Nuno,

I realy want to help further but then I need to know what the actual functionality will be.

Can you give me a functional example of what should happen?

 

Ok a small example of what you should do to make me understand your setup. Do something like:

  • A user load a page MyPage
  • Based on some parameters, some controls are created dynamically on the page
  • 2 Textboxes and 4 DDL are created.
  • The user fills in: "blabla" in textbox1; "boeboe" in textbox2; "value1" in DDL1
  • When the value of DDL1 changes, the value of DDL2 is automatically updated.
  • The user can't change the value of DDL2 anymore
  • .......
  • The user presses the button "Submit"
  • .....

You see, this is some sort of storyboard of what is happening, and/or what should happen.
And it's no technical talk, just what needs to happen functional.

Can you give me a small example like that?
Maybe then I can understand your situation entirely.

Kind regards,
Wim
 

 


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/25/2007 3:01:30 PM

 Hi Wim,

Of course i can give an example although you got most of the part.

  • A user load a page MyPage
  • The user must input into a textbox the number of controls tha must be dynamically created. It should create 2 dropdownlist and 1 text box.
  • The user fills in: "blabla" in textbox1;  "value1" in DDL1
  • When the value of DDL1 changes, the value of DDL2 is automatically updated.
  • Then the user must choose a value from DDL2
  • The user presses the button "Submit"

Did i explain it right?

 

Thanks.

Nuno 

0
wop_nuno
10/25/2007 3:32:12 PM

Hi Nuno,

We are almost there ....

wop_nuno:
The user must input into a textbox the number of controls tha must be dynamically created. It should create 2 dropdownlist and 1 text box.

Ok so when a user inputs: 1 ---> 1 textbox and 2 ddl's are created. When he inputs: 2 ---> 2 txtboxes, 4 ddl's; right?

Another thing, some of the controls will cause postbacks. Especially if you want the 2 DDL's to be dependent of each other. Because of that, you can't always use the TextBox.Text as the number in your loops in your code. Does that make sence?

 

wop_nuno:
When the value of DDL1 changes, the value of DDL2 is automatically updated.

 

Here I have a question. Ok lets say the user changes the DDL1 value to "value2". What changes in the DDL2 now? The selectedItem of the current itemCollection OR a whole new collection gets attached?

The deal is: You have to build your logic on the fact that a control triggers a postback. Then you handle the way they interact in the way you build them in the init event. Hmmmm .... an example here would be: The value of DDL1 changes. DDL1 has an AutoPostBack property that is set to True. Let's say that the selectedItem of DDL2 is dependent on the one that is selected in DDL1. Then in the Page_Init event, you just need to look up the selectedItem in DDL1 and based on that you set the selectedItem of DDL2.  

(I know your english is not so well .... well, mine isn't either so if some of the things above are not that clear to you, please tell me ...... I'm just thinking out loud here and typing it down Wink )

wop_nuno:
The user presses the button "Submit"

 

When this action occurs you want to store the currently selected values of the dynamic controls, right?

 

Please comment on my observations ....
I'll be waiting for the reply ;)

Kind regards,
Wim


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/25/2007 11:04:31 PM

Hi Wim,


When the user Inputs 1 it will created 1 textbox and 2 ddl's., but in one line, like this

 textbox     DDL1    DDL2

when he inputs 2: -> 2 textbox and 4 ddl's, but in two lines. One line line will always have 1 textbox and1 DDL, like this

 textbox1     DDL1    DDL2

 textbox2     DDL3    DDL4

3: -> 3 textbox and 6 ddl's   (3 lines)

 textbox1     DDL1    DDL2

 textbox2     DDL3    DDL4

 textbox3      DDL5    DDL6

 4: -> 4 textbox and 8 DDL's

 

 textbox1     DDL1    DDL2

 textbox2     DDL3    DDL4

 textbox3      DDL5    DDL6

 textbox4      DDL7    DDL8



etc.....

Meaning that whatever the number the user inputs he always creates  1 textbox and 2 DDL per line!!!


When ever  the DDL1 makes selects a value a whole new collection gets attached to the DLL2, depending on the value of the DLL1

For instance, let's assume that we have this table called "ice creams", that has 2 tipes of ice creams:

id    Description

1     Sunday

2     Mccflurry

Then whe have and other table with the flavors of the ice creams:

 
id  ice_cream_id     description

1       1                   chcolate

2       1                   strawberry

3       1                    caramel

4       2                    kit-kat

5       2                    M&M's

For example the DLL1 will be bind with the values of the first table (ice creams). The second DLL2 will be bind with the values of second table (flavours).

If the selectvalue of the DLL1 is 1, then in the DLL2 there only appear the values "chocolate, strawberry and caramel", but if the value of DDL1 is 2 then on DLL2 can only appear "kit - kat and M&M's".

 

When this action occurs you want to store the currently selected values of the dynamic controls, right?

 

Yes when i click on the submit button i want to store in to the Data base the selected valuesof the dymanic controls. 

 

Did i explain it right now?

Your English is just perfect, and feel free to think out loud and type.

 
Thanks.
0
wop_nuno
10/26/2007 10:00:50 AM

Hi nuno,

I made a small demo page called DynamicControls

This should be kind of a kickstart for you!

DynamicControls.aspx:

 

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DynamicControls.aspx.vb" Inherits="DynamicControls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
	  <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
	</div>
    </form>
</body>
</html>

 

DynamicControls.aspx.vb:

Partial Class DynamicControls
  Inherits System.Web.UI.Page

    'the amount of control sets (1 control-set = 1 textbox, 2 dropdownlists)
    Private controlsAmount As Integer = 2

    'here you always have to create your controls and add them to a container
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        '2 counters to keep track of the number of controls created
        Dim txtCount As Integer = 0
        Dim ddlCount As Integer = 0

        'make a control set
        For a As Integer = 1 To controlsAmount
            txtCount += 1
            'create a textbox
            Dim txtBox As New TextBox
            txtBox.ID = "TextBox" + txtCount.ToString

            Me.PlaceHolder1.Controls.Add(txtBox)

            'create 2 dropdownlists per textbox
            For b As Integer = 1 To 2
                ddlCount += 1
                Dim ddl As New DropDownList
                ddl.ID = "Ddl" + ddlCount.ToString

                'only applies on the 1st ddl of a control-set
                If b = 1 Then
                    ddl.AutoPostBack = True
                    AddHandler ddl.SelectedIndexChanged, AddressOf ddl1_SelectedIndexChanged

                    'add items to dropdownlist1
                    Dim ddlItem1 As New ListItem("a", "a")
                    Dim ddlItem2 As New ListItem("b", "b")
                    ddl.Items.Add(ddlItem1)
                    ddl.Items.Add(ddlItem2)
                End If

                Me.PlaceHolder1.Controls.Add(ddl)

            Next

            'add a linebreak
            Dim break As New Literal
            break.Text = "<br />"
            Me.PlaceHolder1.Controls.Add(break)

        Next
    End Sub

    'this method is called when the 1st DDL changes it's index
    'it gets triggered because the DDL1 autopostback property was set to true
    'here you can change the item's collection of DDL2
    Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myDdl As DropDownList
        myDdl = sender

        'I extract the number from the dropdownlist's ID. 
        'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher
        Dim ddlNumber As Integer = CType(Right(myDdl.ID, 1), Integer)
        ddlNumber += 1

        'this is just a small check to see what value is selected
        Dim script As String = "alert('" & myDdl.SelectedItem.Value & "');"
        Page.ClientScript.RegisterStartupScript(Me.GetType, "ddlTest", script, True)

        'here is some logic. 
        'based on the selectedValue of DDL1 I'm gonna set the collection of DDL2
        If myDdl.SelectedValue = "a" Then
            Dim a As New ListItem("c", "c")
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("Ddl" + ddlNumber.ToString), DropDownList)
            dummy.Items.Clear()
            dummy.Items.Add(a)
        ElseIf myDdl.SelectedValue = "b" Then
            Dim a As New ListItem("d", "d")
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("Ddl" + ddlNumber.ToString), DropDownList)
            dummy.Items.Clear()
            dummy.Items.Add(a)
        End If

    End Sub

End Class

 

Some REMARKS ------>

  • I commented the code, but if something still isn't clear, please let me know.
  • As you may have noticed, there is no code to save it to a database. Well, this is actually not that hard and I'm sure you can do that by yourself!!
    On a button click (the submit) you just have to go through the container that holds all of the dynamic controls.

    On small thing here: There are 2 ways you can handle this:
    1. Or you use the same numbering technique as the way you named your dyn. controls when you created them. Then you call the FindControl method on the container to find each control and retrieve it's value.

    2. Or there is a more performant way (I guess). If you are 100% sure that there are only TextBoxes and DropDownLists in your collection (ok, also some literals for the line breaks). And you also know that the sequense is always good. (I mean: txt1 ddl1 ddl2, txt2 ddl3 ddl4, txt3 ddl5 ddl6, ...) Then you don't even need a numbering technique to get all the controls and their values, because the sequence in the container is the same sequence as you want them in the database.
    You could do something like (just some logical code):
    FOR EACH CONTROL IN CONTAINER
       IF CONTROL IS TEXTBOX
          store CAST(CONTROL, TextBox).Text
       ELSE IF CONTROL IS DROPDOWNLIST
          store CAST(CONTROL, DropDownList).SelectedValue
       ELSE
          nothing --> other controls in the collection get ignored
       END
    END
  • Euhm, something else I needed to say ........ hmmm can't remember :)

Please give the code a try!
If you have questions let me know ....

Good luck,
Wim


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/27/2007 7:04:16 PM

Hi Wim, Thank you very much for this code. It must gave you some hard work to do it, so thanks a lot. I haven't tried the code yet, because there are some aspects that i still haven't quiet understood them.  

Don't you have to have a button on the form to cause a Postback so the controls can be created? And on page_Init don't you have to have something like this : "If (Page.IsPostaback) 

1) In this example it only creates 1 line: 1 text box and 2 DDL,right?  It doens't receive any input number?'the amount of control sets (1 control-set = 1 textbox, 2 dropdownlists)
    Private controlsAmount As Integer = 2

 

2)  'make a control set
        For a As Integer = 1 To controlsAmount
            txtCount += 1
            'create a textbox

            Dim txtBox As New TextBox
            txtBox.ID = "TextBox" + txtCount.ToString

            Me.PlaceHolder1.Controls.Add(txtBox)

            'create 2 dropdownlists per textbox

            For b As Integer = 1 To 2
                ddlCount += 1
                Dim ddl As New DropDownList
                ddl.ID = "Ddl" + ddlCount.ToString

                'only applies on the 1st ddl of a control-set

                If b = 1 Then
                    ddl.AutoPostBack = True

                    AddHandler
ddl.SelectedIndexChanged, AddressOf ddl1_SelectedIndexChanged

                    'add items to dropdownlist1

                    Dim ddlItem1 As New ListItem("a", "a")
                    Dim ddlItem2 As New ListItem("b", "b")
                    ddl.Items.Add(ddlItem1)
                    ddl.Items.Add(ddlItem2)
                End If


                Me
.PlaceHolder1.Controls.Add(ddl)

            Next

Does this creates 1 or two DDL? I think it only creates 1 DDL.

 

3)

Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myDdl As DropDownList
        myDdl = sender

        'I extract the number from the dropdownlist's ID.

        'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher
        Dim ddlNumber As Integer = CType(Right(myDdl.ID, 1), Integer)
        ddlNumber += 1

        'this is just a small check to see what value is selected

        Dim script As String = "alert('" & myDdl.SelectedItem.Value & "');"
        Page.ClientScript.RegisterStartupScript(Me.GetType, "ddlTest", script, True)

       
'here is some logic.
        'based on the selectedValue of DDL1 I'm gonna set the collection of DDL2
        If myDdl.SelectedValue = "a" Then
            Dim a As New ListItem("
c", "c")
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("
Ddl" + ddlNumber.ToString), DropDownList)
            dummy.Items.Clear()
            dummy.Items.Add(a)
        ElseIf myDdl.SelectedValue = "
b" Then
            Dim a As New ListItem("
d", "d")
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("
Ddl" + ddlNumber.ToString), DropDownList)
            dummy.Items.Clear()
            dummy.Items.Add(a)
        End If


    End Sub
 

Here i don't understand much. I know that this is the method to populate the seconde DDL, but here is what i don't get:   

 

3.1)  I don't understand this thing:         

  Dim myDdl As DropDownList
        myDdl = sender

        'I extract the number from the dropdownlist's ID.

        'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher
        Dim ddlNumber As Integer = CType(Right(myDdl.ID, 1), Integer)
        ddlNumber += 1

 

when i read this : "   'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher",  i knew i had misslead you in a mistake. 

When in a previous post i posted this:

When the user Inputs 1 it will created 1 textbox and 2 ddl's., but in one line, like this 

textbox     DDL1    DDL2

 

when he inputs 2: -> 2 textbox and 4 ddl's, but in two lines. One line line will always have 1 textbox and1 DDL, like this 

textbox1     DDL1    DDL2 

textbox2     DDL3    DDL4

3: -> 3 textbox and 6 ddl's   (3 lines)

 textbox1 DDL1    DDL2 

textbox2     DDL3    DDL4 

textbox3      DDL5    DDL6

 4: -> 4 textbox and 8 DDL's 

textbox1    DDL1    DDL2

 textbox2     DDL3    DDL4 

textbox3      DDL5    DDL6 

textbox4      DDL7    DDL8 

what i was really trying to say is that the 2 DDL's will have different names but their "id's" per line can be equal, meaning that instead of what i gave you it can be something like this: 

textbox     DDL1.i    DDL2.i

when he inputs 2: -> 2 textbox and 4 ddl's, but in two lines
One line line will always have 1 textbox and1 DDL, like this

 textbox1   DDL1.1    DDL2.1

 textbox2     DDL1.2    DDL2.2

3: -> 3 textbox and 6 ddl's   (3 lines)

 textbox1     DDL1.1    DDL2.1

 textbox2     DDL1.2    DDL2.3 

textbox3      DDL1.3    DDL2.3  

4: -> 4 textbox and 8 DDL's

textbox1     DDL1.1    DDL2.1 

textbox2     DDL1.2    DDL2.2 

textbox3      DDL1.3    DDL2.3

 textbox4      DDL1.4    DDL2.4

I don't know if this complicates or not.....but I think it’s easier 

3.2) In this code is where i really lost myself:

 'based on the selectedValue of DDL1 I'm gonna set the collection of DDL2
        If myDdl.SelectedValue = "a" Then
            Dim a As New ListItem("
c", "c")
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("
Ddl" + ddlNumber.ToString), DropDownList)
            dummy.Items.Clear()
            dummy.Items.Add(a)
        ElseIf myDdl.SelectedValue = "
b" Then
            Dim a As New ListItem("
d", "d")
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("
Ddl" + ddlNumber.ToString), DropDownList)
            dummy.Items.Clear()
            dummy.Items.Add(a)
        End If

With the selectedValue of the DDL1 we're gona set the collection of DDL2, but you do it with an " if " ? That's ok if you have 2 or 3 types (in my case i have 4, so it's fine i only have to do 4 if's), but Imagine if we had 10 or 20 types. We had to do all 30 "if's"?what does this code do???           

 

             If myDdl.SelectedValue = "a" Then  //this I understand, LOL
            Dim a As New ListItem("
c", "c") // if it’s option “a” on DDL1 you create a new item “c” with the value “c”.
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("
Ddl" + ddlNumber.ToString), DropDownList) //this I  don’t understand
            dummy.Items.Clear()      
//you clear the list of items that the DDL2 has??
            dummy.Items.Add(a)     
//you had the item you created to the DDL2, right?

My last question is, and i'm asking this assuming on what i said before (when i said that i think that you only created 1 DDL and not 2): You only created one DDL and add two items “a” and “b”. Then do you create the second DDL (DDL2), only when you check  what value was postback by the first DDL (DDL1), is that right?  Was I clear in my questions?

Thank's a lot again.

 

Nuno

0
wop_nuno
10/28/2007 2:11:16 PM

Hi nuno, 

wop_nuno:

1) In this example it only creates 1 line: 1 text box and 2 DDL,right?  It doens't receive any input number?'the amount of control sets (1 control-set = 1 textbox, 2 dropdownlists)
    Private controlsAmount As Integer = 2

Here, the private controlsAmount is the input-number. I just made it a fixed value, up to you to bind it to an input control. ....

You say it only creates one line?

But the controlsAmount variable has a value of 2. Meaning that 2 control sets will be created.

 

wop_nuno:
Does this creates 1 or two DDL? I think it only creates 1 DDL.

Are you talking about the code in the following method???
Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myDdl As DropDownList
        myDdl = sender

Ok, in this method I only define one dropdownlist. This is just a reference to the dropdownlist which triggered an event. The SelectedIndexChanged event. That event calls this method!

You have to understand the page life-cycle here a bit. When we enter this method, the Page_Init method is already executed. This means that all the controls are now created already. I am just in this method (the ddl1_SelectedIndexChanged method) to make some changes to the controls. (Example: This method is called by the 1st DDL of the control-set, and when it is called, I make changes to the 2nd DDL in the control-set. Are you following here? This is what I do in my example, but you can build up whatever logic you want!)

Summerized: I only instanciate one DDL here, just to work easily with it in the code. But did you noticed this:

Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("Ddl" + ddlNumber.ToString), DropDownList)

A bit further down in that same method (the ddl1_SelectedIndexChanged method) I make a reference to the 2nd DDL of the control-set. I do this to clear it's items, and add a new item. (This is just some of my example-logic. You can do whatever you want here. For example: make a new ItemCollection and add it to the 2nd DDL of the control-set)

wop_nuno:

Here i don't understand much. I know that this is the method to populate the seconde DDL, but here is what i don't get:   

This is just some of my example-logic, please run the code once. YOU just need to paste it in a page. It can't get any easier than that.
If you run the page, I hope it will get a bit more clear to you.

Play around a bit with this variable: Private controlsAmount As Integer = 2 (Set it to 5 for example)

 

wop_nuno:

3.1)  I don't understand this thing:         

  Dim myDdl As DropDownList
        myDdl = sender

        'I extract the number from the dropdownlist's ID.

        'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher
        Dim ddlNumber As Integer = CType(Right(myDdl.ID, 1), Integer)
        ddlNumber += 1

I hope I explained this a bit above.

About the code: "Dim ddlNumber As Integer = CType(Right(myDdl.ID, 1), Integer)":
I did this just to know which DLL is next to the one that triggered this code. Maybe an example:

When controlsAmount is 2, the following will be created (i named them by ID):

TextBox1     Ddl1     Ddl2
TextBox2     Ddl3     Ddl4

In my example-code, I use this logic: WHEN DDL1 of the control-set changes index THEN do something to collection of DDL2 of control-set.

So when Ddl3 of the example triggered the code (I will reference Ddl3 with the variable myDdl in the code), I must get a reference to Ddl4. So the code above extracts the number of the ID of Ddl3 (so ddlNumber will be 3) and then I add 1 to it. (Then I get 4 ---> Yes, I got the right DDL Smile)  Please tell me you are following here?

Note: There are probably more efficient ways to get the id's of the 2nd DDL of the control-set, that's up to you ...  my code was quick and dirty :p

 

wop_nuno:

when i read this : "   'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher",  i knew i had misslead you in a mistake. 

When in a previous post i posted this:

When the user Inputs 1 it will created 1 textbox and 2 ddl's., but in one line, like this 

textbox     DDL1    DDL2

 

when he inputs 2: -> 2 textbox and 4 ddl's, but in two lines. One line line will always have 1 textbox and1 DDL, like this 

textbox1     DDL1    DDL2 

textbox2     DDL3    DDL4

3: -> 3 textbox and 6 ddl's   (3 lines)

 textbox1 DDL1    DDL2 

textbox2     DDL3    DDL4 

textbox3      DDL5    DDL6

 4: -> 4 textbox and 8 DDL's 

textbox1    DDL1    DDL2

 textbox2     DDL3    DDL4 

textbox3      DDL5    DDL6 

textbox4      DDL7    DDL8 

what i was really trying to say is that the 2 DDL's will have different names but their "id's" per line can be equal, meaning that instead of what i gave you it can be something like this: 

textbox     DDL1.i    DDL2.i

when he inputs 2: -> 2 textbox and 4 ddl's, but in two lines
One line line will always have 1 textbox and1 DDL, like this

 textbox1   DDL1.1    DDL2.1

 textbox2     DDL1.2    DDL2.2

3: -> 3 textbox and 6 ddl's   (3 lines)

 textbox1     DDL1.1    DDL2.1

 textbox2     DDL1.2    DDL2.3 

textbox3      DDL1.3    DDL2.3  

4: -> 4 textbox and 8 DDL's

textbox1     DDL1.1    DDL2.1 

textbox2     DDL1.2    DDL2.2 

textbox3      DDL1.3    DDL2.3

 textbox4      DDL1.4    DDL2.4

I don't know if this complicates or not.....but I think it’s easier 

I don't know what the deal is here ... look at my response above: There you see how the controls get created in my example. If you have another naming convention then just apply it to the code. Just make sure that the ID's are unique. Also make sure that you can easily relocate them. (Example: Lets say you create a DDL with ID: "ThisIsJustACrazyRandomID". How are you ever gonna find it again? :) )

 

wop_nuno:
With the selectedValue of the DDL1 we're gona set the collection of DDL2, but you do it with an " if " ? That's ok if you have 2 or 3 types (in my case i have 4, so it's fine i only have to do 4 if's), but Imagine if we had 10 or 20 types. We had to do all 30 "if's"?what does this code do???  

Here again, this is just the way the code of my demo/example is set up!

Read my logic above agian: It was somthing like: WHEN DDL1 index changes THEN change collectoin on DDL2. If your logic is diferent, or needs better code because of scaling reasons, then that's up to you .... (If I wrote the page ... you would have to pay me :p)

wop_nuno:
             If myDdl.SelectedValue = "a" Then  //this I understand, LOL
            Dim a As New ListItem("
c", "c") // if it’s option “a” on DDL1 you create a new item “c” with the value “c”.
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("
Ddl" + ddlNumber.ToString), DropDownList) //this I  don’t understand
            dummy.Items.Clear()      
//you clear the list of items that the DDL2 has??
            dummy.Items.Add(a)     
//you had the item you created to the DDL2, right?

 

Where you say this I don't understand ... I hope you understand it now after what I wrote above ...

What I do is just making a dropdownlist (just a local one to the method / it acts as a reference to the DDL2 of the control-set, the one next to DDL1 of the control-set which index changed.)

Ok, I clear the Items. Why: When I don't do it, all the ohter items added before will still be in there because viewstate recovers them. Just somehting of my example-logic, don't pull to hard on it.

Yep, then I just add an item to the Collection of DDL2 of the control-set.

 

wop_nuno:
My last question is, and i'm asking this assuming on what i said before (when i said that i think that you only created 1 DDL and not 2): You only created one DDL and add two items “a” and “b”. Then do you create the second DDL (DDL2), only when you check  what value was postback by the first DDL (DDL1), is that right?  Was I clear in my questions?

Please run the code, do you see only 1 ddl, or do you see 2?

I am 100% sure that there are 2 DDL's per control-set.

Ok, during the creation of the controls (in the Page_Init) I do perform some actions who only apply to the 1st DDL of the control set (see code: If b = 1 Then)

And look at the code: b goes from 1 to 2, so 2 DDL's per control-set are always created.

Only when b = 1 I apply some actions to DDL1.

These actions are just a part of my example logic. I add a handler to DDL1 (the selectedIndexChanged event) and then I set its autopostback to true so it triggers a postback and calls its ddl1_SelectedIndexChanged method.

 

Kind regards,
Wim


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/28/2007 5:04:17 PM

 Hi Wim,

Thanks again for answering me so fast and above all in a Sunday.

I will try now be even more clear on my questions.

I've already runned you're code and it does works perfectly, has i was expecting. I did change the value of the variable "controlsAmount" and it did work all the time.

1)  when i said :"Does this creates 1 or two DDL? I think it only creates 1 DDL" i was referring   to the code where you create the DDL.

'make a control set

        For a As Integer = 1 To controlsAmount
            txtCount += 1
            'create a textbox
            Dim txtBox As New TextBox
            txtBox.ID = "TextBox" + txtCount.ToString
 
            Me.PlaceHolder1.Controls.Add(txtBox)
 
            'create 2 dropdownlists per textbox
            For b As Integer = 1 To 2
                ddlCount += 1
                Dim ddl As New DropDownList
                ddl.ID = "Ddl" + ddlCount.ToString
 
                'only applies on the 1st ddl of a control-set
                If b = 1 Then
                    ddl.AutoPostBack = True
                    AddHandler ddl.SelectedIndexChanged, AddressOf ddl1_SelectedIndexChanged
 
                    'add items to dropdownlist1
                    Dim ddlItem1 As New ListItem("a", "a")
                    Dim ddlItem2 As New ListItem("b", "b")
                    ddl.Items.Add(ddlItem1)
                    ddl.Items.Add(ddlItem2)
                End If
 
                Me.PlaceHolder1.Controls.Add(ddl)
 

            Next

 

when i read this code,  i didn't realize it was creating two DDL. I only saw it and understood, when i runned the code.

Why did i say that it was only creating 1 DDL? Very simple: the code that i'm using to create my controls is:
 

For i = 1 To Reques.Form ("txt_number")

                Dim MyDDL1 = New DropDownList
                Dim MyDDL2 = New DropDownList

                Dim tb = New TextBox
                tb.ID = "txtBox" & i
                MyDDL1.ID = "DDL1" & i
                MyDDL2.ID = "DDL2" & i
                leitor = cmdTest.ExecuteReader()
                Do While leitor.Read()
                    item = New ListItem()
                    item.Text = leitor("descricao")
                    item.Value = leitor("id")
                    MyDDL2.Items.Add(item)
                Loop
                leitor.Close()
                Panel1.Controls.Add(MyDDL2)
                Panel2.Controls.Add(tb)
                Dim MyLiteral = New LiteralControl
                MyLiteral.Text = "<BR><BR>"
                Panel1.Controls.Add(MyLiteral)

            Next

which by my code i would get this if the user entered the number 3:

txtBox1  DDL1.1   DDL2.1 

txtBox2  DDL1.2   DDL2.2

txtBox3  DDL1.3   DDL2.3

Right? which means that the two DDL have different names DDL1 to the first and DDL2 to the second DDL, but perline the number would be the same. If you see the last line the only difference in the ID is in the name. Did i explain this subject right?

But by your code when the variable "controlsAmount" is 3 what we get is this:

txtBox1  Ddl.1   Ddl.2 

txtBox2  Ddl3    Ddl4

txtBox3  Ddl5    Ddl6

right??  So i wasn't understanding why you where adding 1, in your code below (but now i understand).

'I extract the number from the dropdownlist's ID.

'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher

Dim ddlNumber As Integer = CType(Right(myDdl.ID, 1), Integer)

ddlNumber += 1 


So far i've i explained myself correctly? 

About when to populate the second DDL, i now understand where i have to do it. I will have to do it when i check which value was selected in the first DDL, right?

 

Just one last question (for now Big Smile  ).

Has i explained before in my code i as using two different names (id's) to the dropdownlist (DDL1and DDL2), than when they would be created they're names would be something like DDL1.i and DDL2.i, according to the value "i".

You instead created only one droplist with the id  "Ddl" + ddlCount.ToString .

 When you want to know which dropdown to populate you get the ID of the first DDL and than add one . 

'I extract the number from the dropdownlist's ID.

'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher

Dim ddlNumber As Integer = CType(Right(myDdl.ID, 1), Integer) //here you get the id of the first DDL right?

ddlNumber += 1 // you had 1 to get the correct number of the Ddl to populate

You add 1 so that in the line of code (below):

 

Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("Ddl" + ddlNumber.ToString), DropDownList) 

he can populate the right DDL, i'm i right? Because here the value of the variable "ddlNumber" already has the plus 1!!!!

But, seeing my code (a few lines above) i would have ony this (or something like this) 

 

Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myDdl As DropDownList
        myDdl = sender
 
        'I extract the number from the dropdownlist's ID. 
        'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher
        Dim ddlNumber As Integer = CType(Right(myDdl.ID, 1), Integer)
 
        'this is just a small check to see what value is selected
        Dim script As String = "alert('" & myDdl.SelectedItem.Value & "');"
        Page.ClientScript.RegisterStartupScript(Me.GetType, "ddlTest", script, True)
 
        'here is some logic. 
        'based on the selectedValue of DDL1 I'm gonna set the collection of DDL2
 
For i =1 to Reques.Form ("txt_number") 
        If myDDL1& i.SelectedValue = "a" Then
            Dim a As New ListItem("c", "c")
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL2" & i ), DropDownList)
            dummy.Items.Clear()
            dummy.Items.Add(a)
        ElseIf myDDL1 & i.SelectedValue = "b" Then
            Dim a As New ListItem("d", "d")
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL2" & i), DropDownList)
            dummy.Items.Clear()
            dummy.Items.Add(a)
        End If
 
    End Sub
 
Isn't it? 

 

Did i understand this right? I'm i right?

Did i explain myself enough to you?


You've been a great help. I was really desperate with this, but with your help... i've been able to do somethings Stick out tongue

 

Thanks again.

Nuno
 

 

 

 

 

 

0
wop_nuno
10/28/2007 7:00:40 PM

Hi nuno, 

wop_nuno:

But by your code when the variable "controlsAmount" is 3 what we get is this:

txtBox1  Ddl.1   Ddl.2 

txtBox2  Ddl3    Ddl4

txtBox3  Ddl5    Ddl6

right??  So i wasn't understanding why you where adding 1, in your code below (but now i understand).

'I extract the number from the dropdownlist's ID.

'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher

Dim ddlNumber As Integer = CType(Right(myDdl.ID, 1), Integer)

ddlNumber += 1 

Yep this is just my logic. In your example: If DDL1.2 gets changed you should look for DDL2.2. Well, that is if you want to change something in that dropdownlist ;)

 

wop_nuno:

About when to populate the second DDL, i now understand where i have to do it. I will have to do it when i check which value was selected in the first DDL, right?

Yes, that's when you should do it!!

Remember the Icecreams example? Lets say that DDL1 holds the flavours. And DDL2 holds the ingredients per flavour. Then if the flavour changes, you should change it's ingredients.

NOTE: Take the same example with the icecreams. You should always have an initial state. That what the end-user will see for the first time when the controls are created. You could add a dummy value to the dropdownlists saying <<Please select a value>>. Or you could select the first flavour in DDL1, and its ingredients in DDL2.

 

wop_nuno:

Just one last question (for now Big Smile  ).

Has i explained before in my code i as using two different names (id's) to the dropdownlist (DDL1and DDL2), than when they would be created they're names would be something like DDL1.i and DDL2.i, according to the value "i".

You instead created only one droplist with the id  "Ddl" + ddlCount.ToString .

 When you want to know which dropdown to populate you get the ID of the first DDL and than add one . 

'I extract the number from the dropdownlist's ID.

'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher

Dim ddlNumber As Integer = CType(Right(myDdl.ID, 1), Integer) //here you get the id of the first DDL right?

ddlNumber += 1 // you had 1 to get the correct number of the Ddl to populate

You add 1 so that in the line of code (below):

 

Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("Ddl" + ddlNumber.ToString), DropDownList) 

he can populate the right DDL, i'm i right? Because here the value of the variable "ddlNumber" already has the plus 1!!!!

But, seeing my code (a few lines above) i would have ony this (or something like this) 

 

Hey you're all right here!

For example: When Ddl235 triggered this event, I want to change the collection of that dropdownlist next to it. So in my example that would be Ddl236.

 

 

wop_nuno:
For i =1 to Reques.Form ("txt_number") 
        If myDDL1& i.SelectedValue = "a" Then
            Dim a As New ListItem("c", "c")
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL2" & i ), DropDownList)
            dummy.Items.Clear()

But this .... hmm, if I was a teacher and you were my student and it was the 50's I aught to BitchSlap you!!!!

(sorry for the offencive language)

But myDDL1 ------> That variable isn't even instanciated.

Read the following very well: The ddl1_SelectedIndexChanged method is a method that gets hooked up to the first dropdownlist of the controlset( txt1, ddl1, dll2). Too bad that ddl1 is in the name, but the name of this method isn't relevant .... it is added to all first dropdowns of a serie of 3 controls (txt ddl ddl). The parameter = sender of that method is the dropdownlist that triggers the method !!!!!!!! I have put that object in my own variable called myDdl.

In my example, if Ddl4897 triggered this method, then I know the Dropdownlist next to it is Ddl4898   !!

In your example, if DDL455.1 triggerd this method, then you probably want to change the collection of the dropdownlist next to it called DDL455.2 !!! Right?

SO ---> Back to the code you wrote:

you should do a IF myDdl.selectedValue: ------> myDdl here is the ddl that has its index changed !!!!!!!!!!!!!!!!!!!! myDdl here in your example could be a ddl with id = DDL26.1

What do you need to do to get the ID of the ddl next to it! C'mon you should know this, just strip of the right character and add a "2" to it!!!!!!!!!!!!! Because if the index of DDL26.1 changes you want to change the collection of DDL26.2 right?

 

In your code example, lets say you have 26 controlsets. Then you have a 1st for loop from 1 to 26, Why???? this is a method that gets called because an event of a ddl triggered it remember?

Let's say in loop 14 of the for-loop: --------> You would get some code like this: Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL214), DropDownList) 

Because: Me.PlaceHolder1.FindControl("DDL2" & i) becomes Me.PlaceHolder1.FindControl("DDL214) right??

 

Or I'm totally not following what you want to achieve, or you have some weird coding skills :p

You are trying to achieve the following right? --> Create multiple controlsets of 3. Let the collection of DDL2 of the controlset be dependent on DDL1 of the controlset?

 

Kind regards,
Wim


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/28/2007 9:07:30 PM
]Hi Wim,

I´m glad we are getting along... and i don't mind being BitchSlap, altough i don't understand quiet why i'am being.

 Until the bitchslap, we agree in everything. So let's take a look from there.
 

deblendewim:

But this .... hmm, if I was a teacher and you were my student and it was the 50's I aught to BitchSlap you!!!!

(sorry for the offencive language)

But myDDL1 ------> That variable isn't even instanciated.

When you first gave me you're code, you create the DDL like this (below):

Dim ddl As New DropDownList , right?

That's why that in the method  that trigers the selectevalue you have Dim myDdl As DropDownList , because "myDdl was how  you created the DDL, right ?

If you had this line to create the DDL  Dim BitchSlap As New DropDownList, then in the trigger method you should have something like

Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim BitchSlap As DropDownList
        BitchSlap = sender
 
        'I extract the number from the dropdownlist's ID. 
        'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher
        Dim ddlNumber As Integer = CType(Right(BitchSlap.ID, 1), Integer)
        ddlNumber += 1
 
        'this is just a small check to see what value is selected
        Dim script As String = "alert('" & BitchSlap.SelectedItem.Value & "');"
        Page.ClientScript.RegisterStartupScript(Me.GetType, "ddlTest", script, True)
 
        'here is some logic. 
        'based on the selectedValue of DDL1 I'm gonna set the collection of DDL2
        If BitchSlap.SelectedValue = "a" Then
            Dim a As New ListItem("c", "c")
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("BitchSlap" + ddlNumber.ToString), DropDownList)
            dummy.Items.Clear()
            dummy.Items.Add(a)
        ElseIf BitchSlap.SelectedValue = "b" Then
            Dim a As New ListItem("d", "d")
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("BitchSlap" + ddlNumber.ToString), DropDownList)
            dummy.Items.Clear()
            dummy.Items.Add(a)
        End If
 
I'm i right? So why did i got bitchedslaped for? Because i created my DDL with  
Dim MyDDL1 = New DropDownList 
Dim MyDDL2 = New DropDownList
 and then on the triggered method i didn't change the name? is it?
 (i know it's important to keep track of this, but has you problably noticed i'm a newbie in this) .
 
So according to what i've just said, my method would be something like this, or ate least i hope it is 
(according to the code i posted in my last post, showing how was i creating the controls):
 Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)       
 Dim MyDDL1 As DropDownList         
 MyDDL1 = sender     
'I extract the number from the dropdownlist's ID.        
 'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher      
  Dim ddlNumber As Integer = CType(Right(MyDDL1.ID, 1), Integer)        
'this is just a small check to see what value is selected        
Dim script As String = "alert('" & MyDDL1.SelectedItem.Value & "');"        
Page.ClientScript.RegisterStartupScript(Me.GetType, "ddlTest", script, True)      
  'here is some logic.        
 'based on the selectedValue of DDL1  I'm gonna set the collection of DDL2 
For i =1 to Reques.Form ("txt_number")            
 If MyDDL1& i.SelectedValue = "a" Then                
Dim a As New ListItem("c", "c")                
Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL2." & i ), DropDownList)                
dummy.Items.Clear()                 
dummy.Items.Add(a)            
 ElseIf MyDDL1 & i.SelectedValue = "b" Then                 
Dim a As New ListItem("d", "d")            
 Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL2." & i), DropDownList)              
dummy.Items.Clear()             
 dummy.Items.Add(a)       
 End If    
 End Sub
 Now is it right? All i had to do was change the name of the object according to the name i gave?
 
deblendewim:

Read the following very well: The ddl1_SelectedIndexChanged method is a method that gets hooked up to the first dropdownlist of the controlset( txt1, ddl1, dll2). Too bad that ddl1 is in the name, but the name of this method isn't relevant ..... it is added to all first dropdowns of a serie of 3 controls (txt ddl ddl). The parameter = sender of that method is the dropdownlist that triggers the method !!!!!!!! I have put that object in my own variable called myDdl.

In my example, if Ddl4897 triggered this method, then I know the Dropdownlist next to it is Ddl4898   !!

In your example, if DDL455.1 triggerd this method, then you probably want to change the collection of the dropdownlist next to it called DDL455.2 !!! Right?

That's absolutely right. 

deblendewim:

you should do a IF myDdl.selectedValue: ------> myDdl here is the ddl that has its index changed !!!!!!!!!!!!!!!!!!!! myDdl here in your example could be a ddl with id = DDL26.1

What do you need to do to get the ID of the ddl next to it! C'mon you should know this, just strip of the right character and add a "2" to it!!!!!!!!!!!!! Because if the index of DDL26.1 changes you want to change the collection of DDL26.2 right?

Again, right! Jackpot.

And that's what i did, or tried to do. I think that in the code that i posted right above this situation is correct or isn't?

This is the code that is above, regarding i think you're comment. 

     If MyDDL1& i.SelectedValue = "a" Then           
     Dim a As New ListItem("c", "c")           
     Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL2." & i ), DropDownList)

 

 MyDDL1 is related to the first the DDL the and then the DDL2. is the one who is gone be bind. 
deblendewim:

In your code example, lets say you have 26 controlsets. Then you have a 1st for loop from 1 to 26, Why???? this is a method that gets called because an event of a ddl triggered it remember?

Let's say in loop 14 of the for-loop: --------> You would get some code like this: Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL214), DropDownList) 

Because: Me.PlaceHolder1.FindControl("DDL2" & i) becomes Me.PlaceHolder1.FindControl("DDL214) right??

Also right. My code in the Loop 14 would be something like this (according to the one i posted in this same post):

     If MyDDL1.14.SelectedValue = "a" Then           
     Dim a As New ListItem("c", "c")           
     Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL2.14 ), DropDownList)
Right? Wat's wrong with this code? 

 


deblendewim:

Or I'm totally not following what you want to achieve, or you have some weird coding skills :p

You are trying to achieve the following right? --> Create multiple controlsets of 3. Let the collection of DDL2 of the controlset be dependent on DDL1 of the controlset?

Yes, i'm trying to achieve multiple controlsets of 3. Let the collection of DDL2 of the controlset be dependent on DDL1 of the controlset.

I already now that all of your code works.

My creation of the controls works. But i want to now if the method (the trigger) that i posted now is right?

Why do you say that you're not following me? You are understanding me now, aren't you?

 

when i was starting this project, i thought of him in one way, and now you're showing me an other way that i can do the same.

According to your way i save some lines of code, because in one line you create both of the DDL, and i was creating them in two lines, which implicates that i will be using more lines to add them to the control panel, etc... In other way i don't have to add numbers to know which DDL was triggered.

 I'm sorry if sometimes you fell frustrated with my explanations or my code, but i  really appreciate what you're doing for me.

Thanks a lot.

 

Nuno

 

PS-

I was looking again to your code and i saw something that i don't what it does:

deblendewim:

If b = 1 Then ddl.AutoPostBack = True

AddHandler ddl.SelectedIndexChanged, AddressOf ddl1_SelectedIndexChanged

 'add items to dropdownlist1 Dim ddlItem1 As New ListItem("a""a")

Dim ddlItem2 As New ListItem("b", "b")

ddl.Items.Add(ddlItem1)

ddl.Items.Add(ddlItem2)

End If Me.PlaceHolder1.Controls.Add(ddl)

It's on this line AddHandler ddl.SelectedIndexChanged, AddressOf ddl1_SelectedIndexChanged.

Can you tell me what it does? 


 





0
wop_nuno
10/29/2007 12:14:21 AM

Hi nuno,

sorry for my frustrations, but some of the things I say or do are very basic ....

 

Ok, let's go true it ... 

wop_nuno:

So according to what i've just said, my method would be something like this, or ate least i hope it is 
(according to the code i posted in my last post, showing how was i creating the controls):
 Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)       
 Dim MyDDL1 As DropDownList         
 MyDDL1 = sender     
'I extract the number from the dropdownlist's ID.        
 'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher      
  Dim ddlNumber As Integer = CType(Right(MyDDL1.ID, 1), Integer)        
'this is just a small check to see what value is selected        
Dim script As String = "alert('" & MyDDL1.SelectedItem.Value & "');"        
Page.ClientScript.RegisterStartupScript(Me.GetType, "ddlTest", script, True)      
  'here is some logic.        
 'based on the selectedValue of DDL1  I'm gonna set the collection of DDL2 
For i =1 to Reques.Form ("txt_number")            
 If MyDDL1& i.SelectedValue = "a" Then                
Dim a As New ListItem("c", "c")                
Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL2." & i ), DropDownList)                
dummy.Items.Clear()                 
dummy.Items.Add(a)            
 ElseIf MyDDL1 & i.SelectedValue = "b" Then                 
Dim a As New ListItem("d", "d")            
 Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL2." & i), DropDownList)              
dummy.Items.Clear()             
 dummy.Items.Add(a)       
 End If    
 End Sub

 

Let's hook this code up to a real life example:

  • A user has entered a txt_number of 4, so 4 controls sets are created by your code right??
  • So the dynamic controls that would be created would look like this: (I am naming them by ID)

    txtBox1  DDL1.1   DDL2.1
    txtBox2  DDL1.2   DDL2.2
    txtBox3  DDL1.3   DDL2.3
    txtBox4  DDL1.4   DDL2.4

  • Now lets say the index of DDL1.3 changes. Then I want the collection of DDL2.3 to change its collection.
  • I hooked a method to the selectedIndexChanged event of all first DDL's of a controlset remember? (this code does that:AddHandler ddl.SelectedIndexChanged, AddressOf ddl1_SelectedIndexChanged) ---> I am adding a method to the ddl that will handle its SelectedIndexChanged-event !!!!

     
  • Ok back to the example: So DDL1.3 changed it's index. This triggers the method ddl1_SelectedIndexChanged with the following parameters: sender = DDL1.3 and "e" are its event-arguments.
     
  • Note that when I enter this method, all the dynamic controls are ALREADY CREATED !!!!! This happens in the Page_Init stage of the page. And that stage is already executed !!!!
     
  • So in the method ddl1_SelectedIndexChanged you are just making changes to the collection of the 2nd DDL of the controlset. More specific in this example: You want to change the collection of DDL2.3, because it's the one next to DDL1.3. (The one who's index changed)
     
  • Back to your code that I quoted here above:

    You say:
    "Dim MyDDL1 As DropDownList
    MyDDL1 = sender


    So, MyDDL1 will be DDL1.3 in the code which is quoted above.


    Then you say:
    For i =1 to Reques.Form ("txt_number")      
     If MyDDL1& i.SelectedValue = "a" Then      
     
    Dim a As New ListItem("c", "c")      
    Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL2." & i ), DropDownList)


    So, AGAIN, remember THIS ===> you are in the method ddl1_SelectedIndexChanged because DDL1.3 triggered it. Because its index changed. So now you just want to make modificantions to the collection of DDL2.3 right??


    So, your code is wrong!!
    --> Why are you going from 1 to 4 in your FOR Loop? Please tell me this, because all that needs to change now is the collection of DDL2.3 right???

    --> 
    i.SelectedValue? C'mon man!, A selectedValue is from a DropDownList, "i" in the code quoted above is just an integer !!!!! You can only take a selectedValue from a DDL, for example MyDDL1.SelectedValue.

    If you do MyDDL1.SelectedValue in the code quoted above, you'd be saying: "DDL1.3".SelectedValue. Get it? 
    Now you just need to get a reference to the DDL next to the sender (this will be DDL2.3 !!!!!!).

    So you should have something like:
    Dim theIdNumberOfTheDropDownNextToDDL1 as String = Right(MyDDL1.ID, 1)  -----> In this example, i would get a 3 here, because I get the right most character of DDL1.3.
    Dim MyDDLNextToDDL1 as DropDownList =
    CType(Me.PlaceHolder1.FindControl("DDL2." & theIdNumberOfTheDropDownNextToDDL1 ), DropDownList)

    Now
    MyDDLNextToDDL1 is a reference to DDL2.3. Now I can make modifications to that DDL !!!!

wop_nuno:
deblendewim:

In your code example, lets say you have 26 controlsets. Then you have a 1st for loop from 1 to 26, Why???? this is a method that gets called because an event of a ddl triggered it remember?

Let's say in loop 14 of the for-loop: --------> You would get some code like this: Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL214), DropDownList) 

Because: Me.PlaceHolder1.FindControl("DDL2" & i) becomes Me.PlaceHolder1.FindControl("DDL214) right??

Also right. My code in the Loop 14 would be something like this (according to the one i posted in this same post):

     If MyDDL1.14.SelectedValue = "a" Then           
     Dim a As New ListItem("c", "c")           
     Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL2.14 ), DropDownList)
Right? Wat's wrong with this code? 



Ok sorry I didn't saw the dot, but still, if DDL1.26 triggered this method, why would you loop from 1 to 26, when you only need to change the collection of DDL2.26?????

 

wop_nuno:
My creation of the controls works. But i want to now if the method (the trigger) that i posted now is right?
 

No

 

 

wop_nuno:
Why do you say that you're not following me? You are understanding me now, aren't you?
 

Please tell me that you are understanding me .... : You don't need to loop through all of your DDL's  in the ddl1_SelectedIndexChanged method. Only if you want to make modifications to all the DDL's. But that's not what you want right?

Again: For example you have 50 controlsets. And the 1st DDL of controlset 35 triggers the method ddl1_SelectedIndexChanged. Then you just need to get the dropdownlist with id = DDL2.35 right??
Why loop from 1 to 50 here ????????

Are you with me?

 

wop_nuno:

I was looking again to your code and i saw something that i don't what it does:

deblendewim:

If b = 1 Then ddl.AutoPostBack = True

AddHandler ddl.SelectedIndexChanged, AddressOf ddl1_SelectedIndexChanged

 'add items to dropdownlist1 Dim ddlItem1 As New ListItem("a""a")

Dim ddlItem2 As New ListItem("b", "b")

ddl.Items.Add(ddlItem1)

ddl.Items.Add(ddlItem2)

End If Me.PlaceHolder1.Controls.Add(ddl)

It's on this line AddHandler ddl.SelectedIndexChanged, AddressOf ddl1_SelectedIndexChanged.

Can you tell me what it does?

 

This is the KEY to the whole logic !!!!!!!!!!!!

Don't you see in my example that when a 1st DDL of a controlset has an index-change, that  "ddl1_SelectedIndexChanged" gets executed??

Thats because I hook up that method to the SelectedIndexChanged event of all 1st DDL's of a controlset !!

 

That what the line does:
Hook-up a method to the SelectedIndexChanged-event of ddl. The method has an address of dd1_SelectedIndexChanged. (ddl in this case is the 1st DDL of the controlset that I'm creating.)

 

Man I can't explain this any better than I did now ....
Please tell me you know now that creating DDL's in the ddl1_SelectedIndexChanged-method is not necessary?

 

Kind regards,
Wim


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/29/2007 9:50:58 AM

 Hi Wim,

I don't know what to say, except i'm dumb. Really dumb, and that's to you i realize that now.

I can now clearly see what you were trying to explain me. The method that you created gives me the exact number of the first DDL that made the postback.

You created that method exactly to avoid what i was doing, right? What i was doing, was looping thru the all the DDL's to find out wich one triggered. Right?

 My bad. I'm really sorry. Now i understand you're frustation.

Having said that, and after all your explanations, and you beeing with a nervous breakdown due to your frustration about me (i'm just kidding), i think i'm in conditions

to present what should me my code:

 

Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)       
 Dim MyDDL1 As DropDownList         
 MyDDL1 = sender     
'I extract the number from the dropdownlist's ID.        
 'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher      
  Dim ddlNumber As Integer = CType(Right(MyDDL1.ID, 1), Integer)        
'this is just a small check to see what value is selected        
Dim script As String = "alert('" & MyDDL1.SelectedItem.Value & "');"        
Page.ClientScript.RegisterStartupScript(Me.GetType, "ddlTest", script, True)      
  'here is some logic.        
 'based on the selectedValue of DDL1  I'm gonna set the collection of DDL2 

 If MyDDL1.SelectedValue = "a" Then                
Dim a As New ListItem("c", "c")                
Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL2." & ddlNumber ), DropDownList)                
dummy.Items.Clear()                 
dummy.Items.Add(a)            
 ElseIf MyDDL1.SelectedValue = "b" Then                 
Dim a As New ListItem("d", "d")            
 Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("DDL2." & ddlNumber), DropDownList)              
dummy.Items.Clear()             
 dummy.Items.Add(a)       
 End If    
 End Sub

 

Is it right?

 

Thanks.

Nuno 

 

0
wop_nuno
10/29/2007 10:40:39 AM

 Hi Nuno,

wop_nuno:

I can now clearly see what you were trying to explain me. The method that you created gives me the exact number of the first DDL that made the postback.

You created that method exactly to avoid what i was doing, right? What i was doing, was looping thru the all the DDL's to find out wich one triggered. Right?

 My bad. I'm really sorry. Now i understand you're frustation.

 

Yeah, it was a bit frustrating ... but you have to see things for yourself you know! If I would just give you all the answers and you wouldn't think about what the code actually does, ... that's not good!

About being dumb ... everyone has to start somewhere. This is just a learning curve you're going through. BUT you should know that the page life-cycle is very important to understand!
EDIT: A tip here: Put a breakpoint at every method of your page and debug the page. Then you can follow the life-cycle of your page.

 

About the code example:

It all right!! Big Smile you did it by yourself you see!

 

Kind regards,
Wim



Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/29/2007 11:24:11 AM

 Hi Wim,

 I tried my best. I, like most of the people never programmed in VB and .NET is a new thing for me. It has only been a couple of months since i started working with VB.

I read a lot of articles about the life-cycle, but sometimes that isn't enough.

Well i'm having a problem in my code. In page_init:

 

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init


        If (Page.IsPostBack) Then


            Dim sConnStr As String = "Data Source=VSQL1;Initial Catalog=VarelaBusLog;Integrated Security=True"
            Dim cnBKTest As New Data.SqlClient.SqlConnection(sConnStr)
            Dim cmdTest As New Data.SqlClient.SqlCommand("tipos_percurso", cnBKTest)

            Dim leitor As Data.SqlClient.SqlDataReader

            Dim i As Integer

            Dim item As New ListItem()


            cnBKTest.Open()


            For i = 1 To Request.Form("txtNumber")

                Dim MyDDL1 = New DropDownList
                Dim MyDDL2 = New DropDownList
                Dim tb = New TextBox
                tb.ID = "txt" & i
                MyDDL2.ID = "DRP1" & i
                MyDDL1.ID = "DRP2" & i

                leitor = cmdTest.ExecuteReader()
                Do While leitor.Read()

                    item = New ListItem()
                    item.Text = leitor("descricao")
                    item.Value = leitor("id")
                    MyDDL2.Items.Add(item)
                    MyDDL2.AutoPostBack = "True"
                    AddHandler MyDDL2.SelectedIndexChanged, AddressOf ddl1_SelectedIndexChanged


                Loop
                leitor.Close()
                Panel1.Controls.Add(MyDDL2)
                Panel3.Controls.Add(MyDDL1)
                Panel2.Controls.Add(tb)
                Dim MyLiteral = New LiteralControl
                MyLiteral.Text = "<BR><BR>"
                Panel1.Controls.Add(MyLiteral)

            Next


            cnBKTest.Close()


        End If

      

    End Sub

 

It's in this line :  AddHandler MyDDL2.SelectedIndexChanged, AddressOf ddl1_SelectedIndexChanged

Where i have MyDDL2.SelectedIndexChanged it says that "SelectedIndexChanged" is not an event of object. Do you know why it gives me this?


 

0
wop_nuno
10/29/2007 12:41:41 PM

Hi Nuno,

This is caused by the following:

In my example I write: Dim ddl As New DropDownList  --------> GOOD

In your example you write: Dim ddl = New DropDownList ------> NOT GOOD

Smile

 

After the changes is should work!

Kind regards,
Wim

 


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/29/2007 1:43:57 PM

 Hi Wim,

I made the changes you told me and it works fine. YOU ROCK.

But now i have an other error on my "trigger" method.

My method is:

Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim MyDDL1 As DropDownList

        MyDDL1 = sender

        'I extract the number from the dropdownlist's ID.       

        'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher     

        Dim ddlNumber As Integer = CType(Right(MyDDL1.ID, 1), Integer)

        'this is just a small check to see what value is selected       

        Dim script As String = "alert('" & MyDDL1.SelectedItem.Value & "');"

        Page.ClientScript.RegisterStartupScript(Me.GetType, "ddlTest", script, True)

        'here is some logic.       

        'based on the selectedValue of DDL1  I'm gonna set the collection of DDL2



        If MyDDL1.SelectedValue = "1" Then

            Dim a As New ListItem("c", "c")

            Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DDL2." & ddlNumber), DropDownList)

            dummy.Items.Clear()

            dummy.Items.Add(a)

        ElseIf MyDDL1.SelectedValue = "2" Then

            Dim a As New ListItem("d", "d")

            Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DDL2." & ddlNumber), DropDownList)

            dummy.Items.Clear()

            dummy.Items.Add(a)

        End If

    End Sub

 My DDL1 is the one to the first populated. With a query i made it populates with 4 values (1 to 4). If i select the value number 3 or 4 it gives me the alert, but if i select the value 1 or 2, on line "dummy.Items.Clear()" gives me the following error: "Object reference not set to an instance of an object." Do you know i'm getting this error?

 It must be so obvious, but i can't get there.

Thanks a lot.

Nuno

 

 

 

 

 


0
wop_nuno
10/29/2007 3:51:05 PM

 Hi Wim,

My real code is going to be something like this:

    Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim sConnStr As String = "Data Source=VSQL1;Initial Catalog=VarelaBusLog;Integrated Security=True"
        Dim cnBKTest As New Data.SqlClient.SqlConnection(sConnStr)
        Dim cmdTest As New Data.SqlClient.SqlCommand("percursos2", cnBKTest)

        ' with the store procedure percuros2, i receive a "id", and with that "id" i will select the correspondent values
        cmdTest.CommandType = Data.CommandType.StoredProcedure

        Dim reader As Data.SqlClient.SqlDataReader


        Dim MyDDL1 As DropDownList

        MyDDL1 = sender


        'I extract the number from the dropdownlist's ID.       

        'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher     

        Dim ddlNumber As Integer = CType(Right(MyDDL1.ID, 1), Integer)

        'this is just a small check to see what value is selected       

        Dim script As String = "alert('" & MyDDL1.SelectedItem.Value & "');"

        Page.ClientScript.RegisterStartupScript(Me.GetType, "ddlTest", script, True)

      
        Try

            cnBKTest.Open()

            cmdTest.Parameters.Add("@id", Data.SqlDbType.Int)
            ' the id is equal to the selected value of the DDL that triggered this method
            cmdTest.Parameters("@id").Value = ddlNumber
            cmdTest.ExecuteNonQuery()


            cnBKTest.Close()


        Catch ex As Exception

            lblerro.Text = ex.Message

        End Try


        Dim ddl_items As New ListItem()


        'here is some logic.       

        'based on the selectedValue of DDL1  I'm gonna set the collection of DDL2

        If MyDDL1.SelectedValue = "1" Then

            Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DRP2." & ddlNumber), DropDownList)

            cnBKTest.Open()

            reader = cmdTest.ExecuteReader()

            Do While reader.Read()

                ddl_items = New ListItem()
                ddl_items.Text = reader("descricao")
                ddl_items.Value = reader("id")

            Loop

            reader.Close()

            dummy.Items.Clear()

            dummy.Items.Add(ddl_items)

            cnBKTest.Close()



        ElseIf MyDDL1.SelectedValue = "2" Then

            Dim a As New ListItem("d", "d")

            Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DRP2." & ddlNumber), DropDownList)

            dummy.Items.Clear()

            dummy.Items.Add(a)

        End If

    End Sub

 

And if i run it and select  value 1 or 2 (from the first DDL) it gives me the mistake that i posted on my previous post.

On line "dummy.Items.Clear()" gives me this error: " Object reference not set to an instance of an object." 

Do you know why? (of course you know, i know you know).

 Thanks.

Nuno

 

PS - my store procedure is this

 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go





ALTER  PROCEDURE [dbo].[percursos2]
(
    @id int 
)

 
AS


    SET NOCOUNT OFF;


SELECT     dbo.T_PERCURSOS.descricao, dbo.T_PERCURSOS.id
FROM         dbo.T_PERCURSOS INNER JOIN
                      dbo.T_TIPOSPERCURSO ON dbo.T_PERCURSOS.id_tipo = dbo.T_TIPOSPERCURSO.id
WHERE     (dbo.T_PERCURSOS.id_tipo = @id)

 

0
wop_nuno
10/29/2007 6:03:32 PM

Hi nuno,

wop_nuno:

        If MyDDL1.SelectedValue = "1" Then

            Dim a As New ListItem("c", "c")

            Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DDL2." & ddlNumber), DropDownList)

            dummy.Items.Clear()

            dummy.Items.Add(a)

        ElseIf MyDDL1.SelectedValue = "2" Then

            Dim a As New ListItem("d", "d")

            Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DDL2." & ddlNumber), DropDownList)

            dummy.Items.Clear()

            dummy.Items.Add(a)

        End If

    End Sub

 My DDL1 is the one to the first populated. With a query i made it populates with 4 values (1 to 4). If i select the value number 3 or 4 it gives me the alert, but if i select the value 1 or 2, on line "dummy.Items.Clear()" gives me the following error: "Object reference not set to an instance of an object." Do you know i'm getting this error?

 It must be so obvious, but i can't get there.

The error you get tells you something very significant !!

Read the error again: It tells you that you are referencing an object that is not yet instantiated. Mening that you are referencing to Nothing!!

Ok I see what is going on:

In the code, you extract the ddlNumber right? ddlNumber is an integer.

Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DDL2." & ddlNumber), DropDownList)   ------------->

In this line of code you instantiate dummy. Before this line of code, dummy was just Nothing!

Now the question: Why is dummy nothing then?? Is the FindControl not working??

Well the answer is: You do FindControl(String & Integer) because ddlNumber is an integer right??

 

Solution: Do ddlNumber.ToString
OR
Dim ddlNumber as String = ctype(right(firstDdl.id,1), string) ------> then ddlNumber is a string and "DDL2." & ddlNumber would work!

Please let me know how that worked out

 

Ah, an additional note: you only had it with value 1 and 2 because those are the only ones in your if then conditions ...

Kind regards,
wim


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/29/2007 8:05:05 PM

 Hi Wim,

well i knew this was significant, but not even in my wildest dreams i would get there.

I had put breakpoints on several places, and with the Autos window (debugging) i saw that the variable "ddlNumber" had the correct value, so i didn't suspect from him.

But why does it fails on me, and with your code works just fine?

By the way do you think my code is correct?

 I'm gone try your suggestion, and will get back to you, ok?

Again thanks a lot.

 

Nuno

PS-  even this i knew, LOLLLL

deblendewim:
Ah, an additional note: you only had it with value 1 and 2 because those are the only ones in your if then conditions ...
 

0
wop_nuno
10/29/2007 8:43:04 PM

Hi Nuno,

Yep ... your getting there ....

wop_nuno:

I had put breakpoints on several places, and with the Autos window (debugging) i saw that the variable "ddlNumber" had the correct value, so i didn't suspect from him.

Try debugging on code that triggers the error. In your case it was dummy.Items.Clear right? So debug on code-lines where dummy gets instantiated etc. Then you would have seen that after the line "Dim dummy as Dropdownlist = ...." the variable dummy was still nothing. But is should have been the 2nd dropdownlist of a particular controlset.

 

 

wop_nuno:
But why does it fails on me, and with your code works just fine?

Here is code of my example: (i copied it from one of my replies before)

         If myDdl.SelectedValue = "a" Then
            Dim a As New ListItem("
c", "c")
            Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl("
Ddl" + ddlNumber.ToString), DropDownList)

Do you see that I use ddlNumber.ToString? Altough i think it is better to directly take a string of the ID of the 1st DDL of the controlset. You should only make it integers if you need to calculate with the value or anything. You just need the value!

This makes me think of something very important!!!  You should not strip of 1 character to the right of the ID. Example: DDL1.345 made an index change. So he will be the sender. When you are recreating the ID of DDL2 of the controlset the way you do in the example, you would get this:
ddlNumber = right(DDL1.345, 1) = 5
dummy = placeholder.findcontrol("DDL2." & ddlNumber) = placeholder.findcontrol(DDL2.5)
But you want DDL2.345 right??

So at the left you should strip of DDL1 and replace it with DDL2 / or replace the 4rd character from the left with a 2. As you see, there are many ways to achieve this but the way it happens in the code now is not save!

 

wop_nuno:

By the way do you think my code is correct?

 I'm gone try your suggestion, and will get back to you, ok?

Again ... you're getting there ;)

Kind regards,
Wim

 


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/29/2007 11:01:47 PM

 Hi Wim,

Well each time i talk to you i learn a new thing, and new doubts appear... whenever i think i'm getting there, something else shows up, that messes my mind up.

deblendewim:

Try debugging on code that triggers the error. In your case it was dummy.Items.Clear right? So debug on code-lines where dummy gets instantiated etc. Then you would have seen that after the line "Dim dummy as Dropdownlist = ...." the variable dummy was still nothing. But is should have been the 2nd dropdownlist of a particular controlset.

 

I did that, and has you well say the variable Dummy has the value "nothing".

deblendewim:

This makes me think of something very important!!!  You should not strip of 1 character to the right of the ID. Example: DDL1.345 made an index change. So he will be the sender. When you are recreating the ID of DDL2 of the controlset the way you do in the example, you would get this:
ddlNumber = right(DDL1.345, 1) = 5
dummy = placeholder.findcontrol("DDL2." & ddlNumber) = placeholder.findcontrol(DDL2.5)
But you want DDL2.345 right??

what is this? where did this came from?

You're talking about this line of code " Dim ddlNumber As Integer = CType(Right(MyDDL1.ID, 1), Integer) ", right?

Are you saying that if the user inputs a number higher than 9 (10, 11,.... etc) i'm gona have troubles!!?!!!

 

deblendewim:

So at the left you should strip of DDL1 and replace it with DDL2 / or replace the 4rd character from the left with a 2. As you see, there are many ways to achieve this but the way it happens in the code now is not save!

 

 How do i do this? I know what you mean, but i don't know the code to do it (has i said before, it's few a very few months since i started working with VB).

Let's look at the options:

1)  

deblendewim:

So at the left you should strip of DDL1 and replace it with DDL2

 you're saying to lose all of the DLL1. ??? and change it to DDL2, right?

 

 2)

deblendewim:

or replace the 4rd character from the left with a 2.

what you are saying is that if i have DDL1.345, all i should do is change the number "1" to the "2" so i'll have DDL2.345, right ? But how????

 

 I have to make this change because i will definitely have times that the user will input higher numbers that "9". So when i make this change i will also have to change this line of code (my code)

Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DRP2." & ddlNumber), DropDownList)  ain't i right?

Because when ever i make this change i won't have ddlNumber anymore, right?

 

Please tell me that i'm right, that i'm getting the logic of this.

Thank you very much Wim.

Nuno


 



0
wop_nuno
10/30/2007 12:56:29 AM

Hi nuno,

You're right, when the count of the controlset gets higher then 9, you run into trouble.
Because you get the right character of the DDL1 id and increase it to find the DDL2 id. ---> But this doesn't always work out!

 

Ok so what you should do is the following:
Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl(MyDDL1.ID.Replace("1.", "2.")), DropDownList)

==> What I do is replace the string-combination "1." of the ID with "2.". This way I get the ID of DDL2 of the controlset. ==> This always works out!

And you don't even need ddlNumber anymore now.

 

I would also suggest this:

Please read alot about the framework (for example of the msdn library): This tells you for example how to work with string objects and how to modify the string: http://msdn2.microsoft.com/en-us/library/system.string_methods.aspx

Kind regards,
Wim


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/30/2007 9:26:42 AM

 Hi Wim,

let me start by saying that i did what you told me to do to get pass by that error with the "empty  dummy", but it didn't work. It still gives me the same error. Has you can see by my code:

    Protected Sub ddl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim sConnStr As String = "Data Source=VSQL1;Initial Catalog=VarelaBusLog;Integrated Security=True"
        Dim cnBKTest As New Data.SqlClient.SqlConnection(sConnStr)
        Dim cmdTest As New Data.SqlClient.SqlCommand("percursos2", cnBKTest)

        ' with the store procedure percuros2, i receive a "id", and with that "id" i will select the correspondent values
        cmdTest.CommandType = Data.CommandType.StoredProcedure

        Dim reader As Data.SqlClient.SqlDataReader


        Dim MyDDL1 As DropDownList

        MyDDL1 = sender


        'I extract the number from the dropdownlist's ID.       

        'Because of the way I created my controls, I know that the DDL next to it has an ID which has an ID on 1 higher     

        Dim ddlNumber As Integer = CType(Right(MyDDL1.ID, 1), Integer)

        'this is just a small check to see what value is selected       

        Dim script As String = "alert('" & MyDDL1.SelectedItem.Value & "');"

        Page.ClientScript.RegisterStartupScript(Me.GetType, "ddlTest", script, True)

      
        Try

            cnBKTest.Open()

            cmdTest.Parameters.Add("@id", Data.SqlDbType.Int)
            ' the id is equal to the selected value of the DDL that triggered this method
            cmdTest.Parameters("@id").Value = ddlNumber
            cmdTest.ExecuteNonQuery()


            cnBKTest.Close()


        Catch ex As Exception

            lblerro.Text = ex.Message

        End Try


        Dim ddl_items As New ListItem()


        'here is some logic.       

        'based on the selectedValue of DDL1  I'm gonna set the collection of DDL2

        If MyDDL1.SelectedValue = "2" Then

            Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DRP2." & ddlNumber.ToString), DropDownList)

            cnBKTest.Open()

            reader = cmdTest.ExecuteReader()

            Do While reader.Read()

                ddl_items = New ListItem()
                ddl_items.Text = reader("descricao")
                ddl_items.Value = reader("id")

            Loop

            reader.Close()

            dummy.Items.Clear()

            dummy.Items.Add(ddl_items)

            cnBKTest.Close()



        ElseIf MyDDL1.SelectedValue = "1" Then

            Dim a As New ListItem("d", "d")

            Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DRP2." & ddlNumber.ToString), DropDownList)

            dummy.Items.Clear()

            dummy.Items.Add(a)

        End If

    End Sub

 

deblendewim:

Ok so what you should do is the following:
Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl(MyDDL1.ID.Replace("1.", "2.")), DropDownList)

==> What I do is replace the string-combination "1." of the ID with "2.". This way I get the ID of DDL2 of the controlset. ==> This always works out!

And you don't even need ddlNumber anymore now.

 

If i do this all i have to do is delete the following line  " Dim ddlNumber As Integer = CType(Right(MyDDL1.ID, 1), Integer)" , but as you can see i need the ddlNumber, to my store procedure. So what i was thinking of doing was  Dim ddlNumber = MyDDL1.SelectedItem.Value , so i can use the ddlNumber in my store procedure. What do you think?

Now about your suggestion, this leaves me an other problem. My code generates DDL with the name DRP1  and DRP2, that's why when i was looking in the panel for the DDL i was looking for her name, right?

Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DRP2." & ddlNumber.ToString), DropDownList)

If i do what you are sayimg me to do  "Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl(MyDDL1.ID.Replace("1.", "2.")), DropDownList) " will he find the DRP2.????, i don't thnik so. Do you?

I'm i right or wrong?

 

Thanks.

Nuno 

0
wop_nuno
10/30/2007 10:29:39 AM

 Nuno you are driving me *NuTS*

 

I order you to read a  VB.NET coding basics book before you work any further on the program!!!!!!
Sometimes it's better to take one step back!

 

FIRST POINT:

Your code doesn't work ....

So after this line: "Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DRP2." & ddlNumber.ToString), DropDownList)" --> dummy is still Nothing. right??

I can't check this because I don't know if there is a DDL with ID = DRP2.x (that's the one you are searching for with the FindControl)    ----> Did you create ddls with an ID like that in the Page_Init?

 

wop_nuno:

If i do this all i have to do is delete the following line  " Dim ddlNumber As Integer = CType(Right(MyDDL1.ID, 1), Integer)" , but as you can see i need the ddlNumber, to my store procedure. So what i was thinking of doing was  Dim ddlNumber = MyDDL1.SelectedItem.Value , so i can use the ddlNumber in my store procedure. What do you think?

Now about your suggestion, this leaves me an other problem. My code generates DDL with the name DRP1  and DRP2, that's why when i was looking in the panel for the DDL i was looking for her name, right?

Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DRP2." & ddlNumber.ToString), DropDownList)

You can keep on working with ddlNumber if you want:
I never said you couldn't
DO THIS

  • in visual studio type: me.panel2.FindControl(
  • you see the intellisence right?
  • telling you a bit more about the findcontrol method?
  • look at the parameters the findcontrol takes!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  • ITS A STRING
  • so ddlNumber needs to be a string, or casted to one (.ToString)

 

 

wop_nuno:

If i do what you are sayimg me to do  "Dim dummy As DropDownList = CType(Me.PlaceHolder1.FindControl(MyDDL1.ID.Replace("1.", "2.")), DropDownList) " will he find the DRP2.????, i don't thnik so. Do you?

I'm i right or wrong?

 

GO TO THAT LINK ABOVE AND SEE WHAT THE REPLACE METHOD DOES!!!!!!!

Example:

  • the ddl1_selectedIndexChanged method is triggered.
  • this is done by a DDL with ID = DRP1.456498465 (yeah, lots of controlsets Smile )
  • LOOK WHAT I TYPE:
  • FindControl(MyDDL1.ID.Replace("1.", "2."))
  • That is equal to: FindControl("DRP1.456498465".Replace("1.", "2."))
  • I hope you have read what the replace method does? So, any string combination "1." will be replaced by "2."
  • That means what I type is equal to: FindControl("DRP2.456498465")

So my code seems ok!

 

Kind regards,
Wim


Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
0
deblendewim
10/30/2007 10:55:26 AM

 Hi Wim,

i'm sorry about driving you nuts, you must be a saint, to take this stupidity from me. 

 

deblendewim:

FIRST POINT:

Your code doesn't work ....

So after this line: "Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DRP2." & ddlNumber.ToString), DropDownList)" --> dummy is still Nothing. right??

I can't check this because I don't know if there is a DDL with ID = DRP2.x (that's the one you are searching for with the FindControl)    ----> Did you create ddls with an ID like that in the Page_Init?

 

Yes i do create my DDL's in Page_Init with this id 

MyDDL1.ID = "DRP1" & i
MyDDL2.ID = "DRP2" & i

and that's why i had this : "Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DRP2." & ddlNumber.ToString), DropDownList)" , because "DRP2." is the name of my  second DDL.

deblendewim:

the ddl1_selectedIndexChanged method is triggered.

No problem here. 

deblendewim:

this is done by a DDL with ID = DRP1.456498465 (yeah, lots of controlsets Smile )

 

No problem here too. 

deblendewim:

  • FindControl(MyDDL1.ID.Replace("1.", "2."))
  • That is equal to: FindControl("DRP1.456498465".Replace("1.", "2."))
  • I hope you have read what the replace method does? So, any string combination "1." will be replaced by "2."
  • That means what I type is equal to: FindControl("DRP2.456498465")
  •  

     

    I've already read the linked you gave and i understand the replace now.

    Has the word say "replace", what he's  gone do  is find the DRP1.456498465   and the replace for DRP2.456498465 , right? I think we both agree on that.

    But i've tested this code, and has i suspect, he doesn't populate the DRP2.456498465, what he does is that instead of having DRP1.456498465 and then DRP2.456498465, i only have DRP2.456498465.

    I don't think i explained my self well, so i'll try a litlle bit more.

    Imagine that the inputed inputed the number 3. Then i have:

    txtBox1   DRP1.1    DRP2.1

    txtBox2   DRP1.2    DRP2.2

    txtBox3   DRP1.3    DRP2.3

    I think we can all agree on that, right?

    imagine that DRP1.1 is the one the triggers the method, instead of in the DRP2.1 having the values depending on the DRP1.1 what he does, is in the place where DRP1.1 is he "replaces" for DRP2.1, so now i don't have in my screen the DRP1.1 dropdownlist, but i have DRP2.1 dropdownlist on the screen but empty.

    Did i explain my self clear now?

     

    Can i send you 3 print screens so you can see what i'm talking about? Please? 

     

    I'm really sorry about driving you NUTS. Embarrassed


     

    Here is the problem. 

     

    0
    wop_nuno
    10/30/2007 11:51:26 AM
    Hi nuno,

     

    wop_nuno:

    MyDDL1.ID = "DRP1" & i
    MyDDL2.ID = "DRP2" & i

    and that's why i had this : "Dim dummy As DropDownList = CType(Me.Panel2.FindControl("DRP2." & ddlNumber.ToString), DropDownList)" , because "DRP2." is the name of my  second DDL.

     

    Did you make a typo here in "myddl1.id = "DRP1" & i" ??

    Because this will make ID's like this: DRP11, DRP12, DRP13 and not DRP1.1, DRP1.2, ... (you forgot a dot)

     

    wop_nuno:
    Has the word say "replace", what he's  gone do  is find the DRP1.456498465   and the replace for DRP2.456498465 , right? I think we both agree on that.
     

    Not exactly right: Replace is a function you apply on a STRING, not a dropdownlist. So I replace any stringcombination "1." in the STRING "DRP1.456498465" with the string-combination "2.". This occurs only once. So the string ddl1.blablabla becomes ddl2.blablabla.

     

    wop_nuno:

    imagine that DRP1.1 is the one the triggers the method, instead of in the DRP2.1 having the values depending on the DRP1.1 what he does, is in the place where DRP1.1 is he "replaces" for DRP2.1, so now i don't have in my screen the DRP1.1 dropdownlist, but i have DRP2.1 dropdownlist on the screen but empty.

    Did i explain my self clear now?

     

    Not quite following here ....

     

    wop_nuno:
    Can i send you 3 print screens so you can see what i'm talking about? Please?
     

    You can always mail. Press the contact button and mail me .... Please tell me this in a PM too or in a reply! (I don't check that mailbox very often)

     

    Kind regards,
    Wim            


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    0
    deblendewim
    10/30/2007 12:46:29 PM

     Hi wim,

    no need to mail, or drive you nuts.... at least for now!!! Big Smile

     It works just fine, and that's because of you.....

    you found my mistake and i must say it was pretty stupid (coming from me, nothing else should be expected).

    deblendewim:

    MyDDL1.ID = "DRP1" & i
    MyDDL2.ID = "DRP2" & i

    my error was here!!! i forgot the dot (.)

    It should be

    MyDLL1.ID = "DRP1." & i

    MyDLL2.ID = "DRP2." & i

    and not like i had.

     
    I'm really sorry for all the problems i gave you in this last few days. I'm glad that they are people like you, to help people like me.

    I now i can't express for words, my feelings now, but i'm so happy, and i don't know how to thank you.

    In this last days i learned more Asp.net and VB than in all the months before!

    Thank you.

    Nuno

     

    PS - by the way i did you know i was a man?
     

    0
    wop_nuno
    10/30/2007 1:09:23 PM

    wop_nuno:

    I'm really sorry for all the problems i gave you in this last few days. I'm glad that they are people like you, to help people like me.

    I now i can't express for words, my feelings now, but i'm so happy, and i don't know how to thank you.

    In this last days i learned more Asp.net and VB than in all the months before!

    Thank you.

    Nuno

     

    Hehe, when you start to develop asp.net skills ... also help out beginners Wink 

     

     

    wop_nuno:
    PS - by the way i did you know i was a man?
     

    I think the majority of programmers is Big Smile
    I'm in the wrong business :p

     

    Kind regards,
    Wim


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    0
    deblendewim
    10/30/2007 4:01:43 PM

     Hy Wim,

    Why do you think the majority of programmers are men? 

    You're a women, right?

    You're not in the wrong business, you are exactly where you are suppose to be, because you're an excellent programmer. Beside being and awesome programmer you are also an excellent teacher, so you might think a career in the teaching business Big Smile

    Again Wim, thank you very much!!!! and don't be a stranger, show up, because you'll see many posts of me, LOL

    I now this is kinda strange, but whenever i have a doubt (big doubt) can i contact you?

    Nuno
     

    0
    wop_nuno
    10/30/2007 4:42:19 PM

    wop_nuno:
    You're a women, right?

     

    Nope Cool

    It is the avatar no? Crap .... :)

    And I never met a girl called Wim before :)

     

     

    wop_nuno:
    I now this is kinda strange, but whenever i have a doubt (big doubt) can i contact you?

    I personally think that the forum is still the best place to ask a question when you try to solve a problem.

    Because I only reply to threads I know something of ...

    Good luck with your project ;)
    Kind regards,
    Wim.

     


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    0
    deblendewim
    10/30/2007 8:46:31 PM

    i have the same problem, i have one static DDL and i have to generate at least 6 dependent DDLs, and i show the first dynamic ddl ,  i cant get the second one

     

    do you know how i can manage this

     

    thankx

    0
    zakandzak
    7/7/2008 11:10:46 PM
    Reply:

    Similar Artilces:

    How to get the value of dynamically created text box which is created dynamically on click of button control.
    <p>Dear AllI am new in asp.net.I am doing one project there I have one doubt.what I want is I have taken two  button controls in my page, in click of first button1 I have  dynamically created one textbox and I added this textbox to my page and also set the id for this textbox,.after that in click of second button2 I want that the value of the textbox will be print in my page by getting the value of the textbbox. some codes are given below. <form id="form1" runat="server">    <div>      ...

    How get value from a dynamically created control in a user control
    HI, I am working with a version of IBuySpy. On one page is a form that contains a user control that lists the items the user purchased, the subtotal, shipping, etc. I wanted to add a DropDownList from which the user could select the shipper. The dropdown list works fine as long as it has .AutoPostBack = false. However, I cannot figure out how to get the value that the user selected when they finally press the submit button on the form. All attempts to locate the control and it's value have failed. I tried drillingdown from the form to the child controls, and sub child controls. N...

    Get user input from dynamic created control, why i cant get the value???
    Hi!, i was tried to dynamically generate some textboxes according the selected number in a dropdownlist, but when i need to get the user input from those textboxes.. i cant get at all!!, Can any1 guide me on tis? Below is my code.Basically, i have 1 dropdownlist (ddltbCount), 1 placeHolder(Pholder1), 1 button(btGetValue), i dont know y i cant post the html here... Private count As Integer = 1 Sub IterateThroughChildren(ByVal parent As Control) For Each c As Control In parent.Controls If c.GetType.ToString.Equals("System.Web.UI.WebControls.TextBox") AndAlso c.ID Is Nothing Then ' CType(c, T...

    Reading form values in dynamically created child controls in a user control
    I have a common problem for which I've searched the web for answers for hours to no avail. I have some radiobuttonlists ,dropdownlists,checkboxlists and text controls in a user control. The lists are created statically, however the items in the lists are populated at run time. The lists are displayed in default.aspx (the containing page) and populated on the Page_Load event of the user control .ascx file based on an external data source containing arrays of data. I want to be able to read the values selected by the user from deafult.aspx, the parent page. details: The .ascx...

    Problem in accessing web form data in user controls when create user control dynamically
    HI. I have a problem using web user control. I designed a user control named NewsForm. This user control contains some web form elements like <asp:textbox>. When I add it in an .ASPX page, all things may be true. I can know which user filles in textbox (for example and other controls) <%@ Register TagPrefix="uc1" TagName="NewsForm" Src="NewsForm.ascx" %> and <UC1:NewsForm id="newsForm" runat="server"></UC1:NewsForm> But when i create it in code behined dynamically, however user control has web form controls but their value are blank for example //aspx co...

    How to get the HREF value of the Anchor tag in a web page using the web browser control in vb.net
    Hi Friends,               i have a webserver control in my application.i load the web browser control on a button click using the below code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click                AxWebBrowser1.Navigate("http://www.rateitall.com/i-322335-.aspx", False) End sub once the page is loaded in the browser i want the <a> HREF value in that page.For example if that page has ...

    when loading a previously created record on to form, some controls are missing values(controls are depended on values selected in other controls)
    Hello - I have created a page for Editing records.  this page will open up a chosen record and allow user to edit the data.On the form, I have 3 drop down lists.   The second dropdown list should fill values depending on value chosen in first dropdown list.  The third dropdown list should show/fill values depending on value chosen in second dropdown list.So taking this into count, i have several records with various data.  When I open-up/load a record in Edit form, only the first dropdown list has a value selected that was the pre-existing va...

    Dynamically creating controls in Web forms
    I am able to create a web control programmatically and display on the form. In the click event of the button then I am trying to capture the value entered in the above created textbox in another click of button but I am unable to obtain the reference of the control that has been created dynamically. Can anyone help me please? protected System.Web.UI.WebControls.Panel myPanel; private void btnMake_Click(object sender, System.EventArgs e) { TextBox txtBox = new TextBox(); myPanel.Controls.Add(txtBox); } private void btnGotValues_Click(object sender, System...

    Get values of dynamic created controls?
    Hello there,I'm creating some dynamic controls, where i'm getting some info from a db. - Thats working, but I dont know how to get the values from the dynamic created controls again, so i could save them in my database? This code am i using to create the controls (This get looped for each record in the db) 1 ' Connection 2 Dim str As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString 3 Dim conn As SqlConnection = New SqlConnection(str) 4 conn.Open(...

    Get the value of dynamically created controls
    I have created the following function to get the text out of dynamically created text boxes that I have created from the code behind page.When call the function I get a message saying Object Reference not set to an instance of an object on this line -For Each c In Me.Parent.ControlsThe ID of the textboxes have the following format - Quantity_1111 Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnUpdate.Click Dim c As Control Dim strID As String Dim arrProducts(2) As String Dim dtProducts As New DataTable 'Dim parent As C...

    Dynamically created controls in web form
    Hi there. This is bit of an open ASP.Net (C#) question with regards to dynamically created controls and in particular tabular or panel controls. What I currently have is a web page that creates between 1 and 6  tables, each of which holds specific data about a set of products, each table (or panel, if you want) has a couple of multi-line TextBoxes and a few other controls, all lined up in a set format. The number is determined by the data records returned from a database table. My question is what is a best way to create these tables of data at run time?  Has anybody else had e...

    Get the value or Text of dynamically created controls?
    Hello, I have a page with a placeHolder, in wich I create several controls (of textBox, checkBox and dropdownList type) I found several articles on the web about dynamic controls, but I couldn't find how to access each controls properties. I would like to be able to check if the user has checked the checkBox or not, and read the text inside the textBox I created Is that not possible? Thank you An important thing to keep in mind is that you need to recreate the dynamic controls you add to your PlaceHolder control each time a postback occurs.  After that, you can use FindContr...

    How to get the value fo dynamically created control?
    <p>Dear AllI am new in asp.net.I am doing one project there I have one doubt.what I want is I have taken two  button controls in my page, on click of first button1 I have  dynamically created one textbox and I added this textbox to my page and also set the id for this textbox,.after that on click of second button2 I want that the value of the textbox will be print in my page by getting the value of the textbbox. some codes are given below. <form id="form1" runat="server">    <div>      ...

    Retreaving Values from dynamically created Web Controls
    I have a tricky problem that has been stumping me. I have a multiview page.  One the 2nd view I need to dynamically add  web controls for the user to enter in data for a search.  I am unable to get the data from the web controls before they are wiped out in the page load.  When a page button is pushed the page reloads and my data is gone ( because it was created dynamically ). I can not add the web controls in the On_Init method because 1) it is a multiview and 2) the users must choose which ones they want at run time on view 2. Currently, I'm trying to out...

    Web resources about - Creating dynamic controls and getting their values - asp.net.web-forms

    Wikipedia:Creating a bot - Wikipedia, the free encyclopedia
    It is often far simpler to request a bot job from an existing bot. If you have only periodic requests or are uncomfortable with programming languages, ...

    U.K. Woman Faces 20 Months For Creating Fake Facebook Accounts, Sending Herself Abusive Messages
    A woman in Cornwall in the U.K. came up with a creative way to use Facebook to accelerate a family spat, and it earned her 20 months in prison. ...

    Sandi Krakowski’s Tips for Creating Great Content
    Quick tips to help you create great content for your business.

    Bug - Creating a Video.Watches Action stopped working - Facebook-Entwickler
    Since today executing a POST on a me/video.wacthes endpoints no longer works, it was working around 10 hours ago since then the call always returns ...

    Facebook simplifies process for creating repeat events
    Facebook introduced a new feature for events this week that allows users to quickly create a similar event as one they previously hosted or attended. ...

    The Obama campaign won by creating their own Facebook, with unique features for political campaigns.
    ... seen as too dangerous to nominate someone who is so thoroughly narcissistic, arrogant and dishonest. The Obama campaign won basically by creating ...

    Creating A Road Map To Market Your Startup
    Spreading the word can be challenging when you’re new to an industry, don’t have a big budget, and have little helpbut it’s possible.You’ll need ...

    Dance Machine - The Fun Video Editor for Creating Music Videos to share on Instagram, Facebook, Vine ...
    Get Dance Machine - The Fun Video Editor for Creating Music Videos to share on Instagram, Facebook, Vine, and Twitter on the App Store. See screenshots ...

    Creating Economic Dynamism: Joseph E. Stiglitz
    DAVOS/SWITZERLAND, 25JAN13 - Joseph E. Stiglitz, Professor, Columbia University, USA talks during the session 'Creating Economic Dynamism' at ...

    YouTube - Super Human Abilities: Tila Tequila Creating Energy Balls & Electricity Out of Thin Air!
    Veröffentlicht am 24.04.2013 Hey Guys! For a long time now I have been able to do some pretty supernatural things that most would consider "SUPER ...

    Resources last updated: 11/22/2015 6:47:35 AM