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
Solved

selectedindexChanged will not fireup, Dynamic generated Dropdown

Posted on 2004-10-12
14
1,139 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
performance issue in asp web 5 37
Modal Popup Extender control 1 38
ASP.NET MVC identity 6 30
Iteration Help (Asp.net VB) 5 24
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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