Solved

selectedindexChanged will not fireup, Dynamic generated Dropdown

Posted on 2004-10-12
14
1,134 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 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

16 Experts available now in Live!

Get 1:1 Help Now