Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

selectedindexChanged will not fireup, Dynamic generated Dropdown

Posted on 2004-10-12
14
Medium Priority
?
1,154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 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