Link to home
Start Free TrialLog in
Avatar of rsbadhan
rsbadhan

asked on

selectedindexChanged will not fireup, Dynamic generated Dropdown

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
Avatar of DotNetLover_Baan
DotNetLover_Baan

Did you set AutoPostBack = True for the ddl ?
-Baan
Avatar of rsbadhan

ASKER

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
Basically what i need to do is someVAr = myDDL.selecteditem.value

but outside the sub showdata it does not work.

Thnaks
RB
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
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
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
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.
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

ps above refers to dotnetlover Baan's code
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
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
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
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial