Solved

selectedindexChanged will not fireup, Dynamic generated Dropdown

Posted on 2004-10-12
14
1,136 Views
Last Modified: 2008-01-09
hi i have dynmically added a number of controls dynamically . The count is dependant on the no of values in a dataview.

the code behind generates all the controls including populating the dropdown boxes, but the vaues of the selecteditemindex are not reterived.

Sub showData(ByVal newcounter2 As Integer)
        Dim x As Integer
        Try
            For x = 0 To newcounter2 - 1
                Dim mylbl As New Label()
                mylbl.CssClass = "lblblue2"
                PlaceHolder2.Controls.Add(New LiteralControl("<p>"))
                PlaceHolder2.Controls.Add(mylbl)
                mylbl.Text = fReadName2(x)
                PlaceHolder2.Controls.Add(New LiteralControl("<br>"))
                Dim objDataview As New DataView(DSGeneric.Tables("gproducts"))
                objDataview.RowFilter = "meds = '" & fReadName2(x) & "'"

                PlaceHolder2.Controls.Add(New LiteralControl("<table>"))
                Dim myDDL As New DropDownList()
                myDDL.CssClass = "sWidth"

                Dim btnBuy As New ImageButton()
                btnBuy.ImageUrl = ("./images/add2cart.gif")

                PlaceHolder2.Controls.Add(New LiteralControl("<tr>"))

                PlaceHolder2.Controls.Add(New LiteralControl("<td width=400>"))
                PlaceHolder2.Controls.Add(myDDL)
                myDDL.DataSource = objDataview
                myDDL.DataTextField = "desc"
                myDDL.DataValueField = "ProductId"
                myDDL.DataBind()
                myDDL.AutoPostBack = True
                Session("selection") = myDDL.SelectedItem.Value
                AddHandler myDDL.SelectedIndexChanged, AddressOf Me.Ddl_SelectedIndexChanged
                PlaceHolder2.Controls.Add(New LiteralControl("</td>"))

                PlaceHolder2.Controls.Add(New LiteralControl("<td width=100>"))
                PlaceHolder2.Controls.Add(btnBuy)
                AddHandler btnBuy.Click, AddressOf Button_Click
                PlaceHolder2.Controls.Add(New LiteralControl("</td>"))

                PlaceHolder2.Controls.Add(New LiteralControl("</p>"))
                PlaceHolder2.Controls.Add(New LiteralControl("</tr>"))
                PlaceHolder2.Controls.Add(New LiteralControl("</table>"))
            Next

        Catch
        End Try
        Dim objDataview2 As New DataView(DSGeneric.Tables("gproducts"))
    End Sub

    Private Sub Ddl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        showData(Session("noitems"))        '************gets no of items form another sub

                                                  '********** what do i do here?
    End Sub
0
Comment
Question by:rsbadhan
  • 7
  • 3
  • 2
  • +1
14 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12291719
Did you set AutoPostBack = True for the ddl ?
-Baan
0
 

Author Comment

by:rsbadhan
ID: 12292556
yes... i have tried with and without
what i really need to do is refer to the session variable where i have marked here, but how do i refer to myDDL?

Private Sub Ddl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        showData(Session("noitems"))          
********** HERE *********
End Sub
0
 

Author Comment

by:rsbadhan
ID: 12292597
Basically what i need to do is someVAr = myDDL.selecteditem.value

but outside the sub showdata it does not work.

Thnaks
RB
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12292696
ok, when are you populating the DDL, in Page_Load ? If yes, make sure that you check for Post back..

If Not Page.IsPostBack Then
   'Populate the ddl here
End If

Then, set AutoPostBack = True for the ddl

Use "Handles" to set the event.
    Private Sub Ddl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddl1.SelectedIndexChanged
           'showData(Session("noitems"))
            Dim NewCounter2 As Integer
            NewCounter2 = Ddl.SelectedItem.Value
            showData(NewCounter2)
    End Sub

-Baan
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12292743
Ok, since you know that you are going to have a myDDL dropdownlist in advance. I would do this. Define Dim myDDL in the class variable section. Remember you are just defining. But you are isntantiating the myDDL in your showData method, where it gets equalled to New DropDownList().

 This way, you are doing the actual object instantiation in the method dynamically, but defining in advance in the class variables section so everybody else in the class can access this myDDL.

Remember showData should be called before you call the Ddl_SelectedIndexChanged event handler, since if you dont, myDDL wouldnt have an object to refer to, just a definition which goes no where. So it should be the following

public class
{
Dim myDDL
Sub showData(ByVal newcounter2 As Integer)
        ..............................
        myDDL as New DropDownList()
        .....................
End Sub

 Private Sub Ddl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
             someVAr = myDDL.selecteditem.value  // Remember here you can access myDDL since its defined outside the method showData
  End Sub

}

HTH
0
 

Author Comment

by:rsbadhan
ID: 12292934
i am using vb... but i have tried ryerras  code... getting problem .. object reference not set to instance of an object.
Altough in visual studio it gave me the options for mydll. *****

trying baan's code
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rsbadhan
ID: 12292967
ryerras actually when i just use dim myddl in the
public class
Dim myDDL
....
end class

it is not recgonised by the intellensense of the VB studio, bur if i make it a Dim myDDl as new Dropdownlist then it shows up in the intellesene hints.
0
 

Author Comment

by:rsbadhan
ID: 12293130
if i do this

If Not Page.IsPostBack Then
   'Populate the ddl here
End If

on postback the cotrols disapear since they are dynamically generated and are part of the populate the data sub..

Sub showData(ByVal newcounter2 As Integer)
....

end sub

0
 

Author Comment

by:rsbadhan
ID: 12293243
ps above refers to dotnetlover Baan's code
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12318821
Ok, I do not know VB.NET that much, I am a c# programmer. Anyway, If you want to use myDDL dropdownlist in various methods, it has to be a class variable. You can define in the class variable section and populate in your showData method, and then retrieve the value in the  Ddl_SelectedIndexChanged event handler. I never coded in vb.net, so not even sure how the variable accessing works, but in any object oriented approach, if you are using a variable in multiple places in your class, atleast its definition should be in the class variables section. Remember, the entire class should own it to be able to access in multiple methods in the class.

So, define myDDL as  private myDDL as new DropDownList in the class variables section and populate in the showData method and then use it in the  Ddl_SelectedIndexChanged event handler.

Good luck
0
 

Author Comment

by:rsbadhan
ID: 12322364
thanks.. it has to be solved as follows :

    Private Sub Ddl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim targetUniqueId = Page.Request.Params("__EVENTTARGET")
        Dim targetControl As Control
        If Not IsNothing(targetUniqueId) Then
            targetControl = Page.FindControl(targetUniqueId)
            If Not IsNothing(targetControl) And (TypeOf (targetControl) Is IPostBackDataHandler) Then
                Dim ddl As DropDownList = CType(targetControl, DropDownList)
                Session("selection") = ddl.SelectedItem.Value
                Session("Desc") = ddl.SelectedItem.Text
            End If
        End If
    End Sub
0
 
LVL 6

Expert Comment

by:ryerras
ID: 12534353
I think the author has allready fixed his problem, with the suggestions from the experts over here. I am not sure, if this question should be deleted
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12572760
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to go to Web page 2 93
How to LOAD up a .NET Framework website with data? 2 39
Adjust the position 3 47
How to access a DataBinder.Eval variable using C# ? 4 27
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now