Help with executing command when click on combobox

Hello,

I need to trigger a command when I click on my combobox, but since Ispostback does not equal to false, the command that I have in my Form Load event which has the If Ispostback = false doesn't execute, Is there a way to execute the command when I click on the Combobox?

Thanks,

Victor
vcharlesAsked:
Who is Participating?
 
ivan_vaguninCommented:
Hi!
Then you need to add javascript postback function for ddl onclick event. You can do it like this:
CmbRec.Attributes("onclick") = ClientScript.GetPostBackEventReference(this, "CmbRecClick")
Then you need to implement IPostbackEventHandler interface for your page (http://msdn.microsoft.com/en-us/library/system.web.ui.ipostbackeventhandler.aspx) like this:

Public Sub RaisePostBackEvent(ByVal eventArgument As String)
        If (eventArgument = "CmbRecClick") Then
            'Execute Code B
        End If
    End Sub

If you attach your page and code, I will let you know where exactly you should put this code
0
 
ivan_vaguninCommented:
Hi! What do you mean saying Combobox? Dropdownlist or Ajaxtoolkit combobox? Can you post markup and code?
0
 
vcharlesAuthor Commented:
I meant the Dropdown List, when I click on it, I need to execute the method below:

Public Sub FillCombos()
        If IsPostBack = False Then

            '*******************
            Dim CountryOrigin As New DataSet
            CountryOrigin.ReadXml(Server.MapPath("~/App_Data/Receiver.xml"))
            C1Country.DataSource = CountryOrigin.Tables(0)
            C1Country.DataMember = "Name"
            C1Country.DataTextField = "Name"
            C1Country.DataValueField = "ReceiverID"
            C1Country.DataBind()
            '******************

            '*******************
            Dim CountryReplace As New DataSet
            CountryReplace.ReadXml(Server.MapPath("~/App_Data/Receiver.xml"))
            C1Receiver.DataSource = CountryReplace.Tables(0)
            C1Receiver.DataMember = "Name"
            C1Receiver.DataTextField = "Name"
            C1Receiver.DataValueField = "ReceiverID"
            C1Receiver.DataBind()
            '******************

            Dim RecName As XmlNodeList = RecStream.SelectNodes("Root/Receiver")
            Dim DonorName As XmlNodeList = DonorStream.SelectNodes("Root/Donor")

            Dim donorlst As New DataTable()
            donorlst.Columns.Add("DonorID")
            donorlst.Columns.Add("Name")
            Dim reclst As New DataTable
            reclst.Columns.Add("ReceiverID")
            reclst.Columns.Add("Name")

            For Each node As XmlNode In RecName
                reclst.Rows.Add(New Object() {node.ChildNodes(0).InnerText, node.ChildNodes(1).InnerText})
            Next

            For Each node As XmlNode In DonorName
                donorlst.Rows.Add(New Object() {node.ChildNodes(0).InnerText, node.ChildNodes(1).InnerText})
            Next

            donorlst.DefaultView.Sort = "Name ASC"
            CmbDonor.DataSource = donorlst
            CmbDonor.DataTextField = "Name"
            CmbDonor.DataValueField = "DonorID"

            reclst.DefaultView.Sort = "Name ASC"
            CmbRec.DataSource = reclst
            CmbRec.DataTextField = "Name"
            CmbRec.DataValueField = "ReceiverID"
             End If
    End Sub
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
ivan_vaguninCommented:
Does you markup for ddl looks like following:
<asp:dropdownlist ID="Dropdownlist1" OnSelectedIndexChanged="Dropdownlist1_SelectedIndexChanged" AutoPostBack="true" runat="server">?
Pay attention to AutoPostBack="true" and OnSelectedIndexChanged="Dropdownlist1_SelectedIndexChanged" and ID should not be empty.
Indeed DropDownList does not have event handler for click event, it has handler only for selected index changed event wich is different from click. However if really want to fire event on every click there is a workaround for this
0
 
vcharlesAuthor Commented:
What is the workaround? Initially the Dropdownlist withh display data from a filtered search, but I need users to be able to load it with data from an xml file incase they want to make a change to the data filtered.
0
 
ivan_vaguninCommented:
The workaround is to generate a client postback function (using ClientScript.GetPostBackEventReference), then you can set this function as client onclick handler. But I guess you does not need it - handling OnSelectedIndexChanged should be enough for you scenario. So I would advise to figure out what's wrong with that. Can you post your page markup?
0
 
vcharlesAuthor Commented:
I'm sorry, you lost me, what do you mean by page markup?  
0
 
ivan_vaguninCommented:
I mean content of your aspx page. If you create DropDownBox dynamically (in code), please, post the code for that. I need to see how dropdownlist is created
0
 
vcharlesAuthor Commented:
Hi,

Below is the code in my aspx page for the Dropdownlist (CmbRec) control. Initially I am loading the control by code with the code from my first post. I will then need to load it when users click on it, with Receiver.xml to display all available Receivers to give them the option to change the existing receiver using the following code:
            Dim CountryOrigin As New DataSet
            CountryOrigin.ReadXml(Server.MapPath("~/App_Data/Receiver.xml"))
            CmbRec.DataSource = CountryOrigin.Tables(0)
            CmbRec.DataMember = "Name"
            CmbRec.DataTextField = "Name"
            CmbRec.DataValueField = "ReceiverID"
            CmbRec.DataBind().

aspx page code:
<asp:DropDownList ID="CmbRec" runat="server" Height="16px">
        </asp:DropDownList>
0
 
ivan_vaguninCommented:
Hi! Try to add selected index handler:
<asp:DropDownList ID="CmbRec" runat="server" Height="16px" AutoPostback="True" OnSelectedIndexChanged="CmbRec_SelectedIndexChanged">
        </asp:DropDownList>

And dd handler procedure in your code:
Protected Sub Sel(ByVal sender As Object, ByVal e As EventArgs)
        ...
Dim CountryOrigin As New DataSet
            CountryOrigin.ReadXml(Server.MapPath("~/App_Data/Receiver.xml"))
            CmbRec.DataSource = CountryOrigin.Tables(0)
            CmbRec.DataMember = "Name"
            CmbRec.DataTextField = "Name"
            CmbRec.DataValueField = "ReceiverID"
            CmbRec.DataBind()
...
    End Sub
0
 
vcharlesAuthor Commented:
Hi,

I added:

<asp:DropDownList ID="CmbRec" runat="server" Height="16px" AutoPostback="True" OnSelectedIndexChanged="CmbRec_SelectedIndexChanged">
        </asp:DropDownList>
in my aspx page but can't figure out what to do with the second part of you code, what do you mean by adding a DD handler?

Thanks,

Victor
0
 
ivan_vaguninCommented:
The second part you should add in codebehind file (By 'DD handler" is dropdownlist handling procedure)
0
 
vcharlesAuthor Commented:
Sorry I still don't undersatand, add in codebehind file? I included it in my web form, but nothing happens when I click on the control. Can you please send me an example of what part of the project to include this code.
0
 
vcharlesAuthor Commented:
Hello,

Can you please send me more details on how to solve this issue?
0
 
ivan_vaguninCommented:
Hi! Sorry for late response..
The code should be placed where the code is (codebehind file or markup). Where is your FillCombos() declared? Does the page refreshes when you change selection in dropdownlist?
0
 
vcharlesAuthor Commented:
Hi,

I am no longer using the FillCombobox method, I am populating the Dropdownlist from a Search button click event using CodeA. Once I click on the dropdownList, I want to execute the codeB to load the Dropdownlist with Receiver.xml and Donor.xml.

CodeA:

 Protected Sub Search_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Search.Click

        '  If IsPostBack = True Then
        ' create a new XML Document object for each XML file
        Dim xdDonor As New Xml.XmlDocument
        Dim xdLink As New Xml.XmlDocument
        Dim xdReceiver As New Xml.XmlDocument

        ' load all of the XML documents
        xdDonor.Load(Server.MapPath("~/App_Data/Donor.xml"))
        xdLink.Load(Server.MapPath("~/App_Data/Link.xml"))
        xdReceiver.Load(Server.MapPath("~/App_Data/Receiver.xml"))

        ' This outer loop will iterate over the Link file where the ReceiverID node is equal to our search criteria
        For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/Link[ReceiverID='" & C1Country.SelectedItem.Value & "']")

            ' Extract the ReceiverID and DonorID values from the current node
            Dim ReceiverID As String
            Dim DonorID As String

            ReceiverID = xnLink.SelectSingleNode("ReceiverID").InnerText
            DonorID = xnLink.SelectSingleNode("DonorID").InnerText

            ' Perform another similar search based upon the Donor XML file
            For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root/Donor[DonorID='" & DonorID & "']")

                ' Extract the DonorID and Name values from the current node
                Dim DonorID2 As String
                Dim Name As String

                DonorID2 = xnDonor.SelectSingleNode("DonorID").InnerText
                Name = xnDonor.SelectSingleNode("Name").InnerText
                dtDonor.Rows.Add({DonorID2, Name})
            Next

            ' Perform yet another practically identical search on the Receiver XML file
            For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/Root/Receiver[ReceiverID='" & ReceiverID & "']")
                ' Extract the ReceiverID and Name values from the current node
                Dim ReceiverID2 As String
                Dim Name As String

                ReceiverID2 = xnReceiver.SelectSingleNode("ReceiverID").InnerText
                Name = xnReceiver.SelectSingleNode("Name").InnerText
                dtReceiver.Rows.Add({ReceiverID2, Name})

            Next
        Next
       

        CmbRec.DataSource = dtReceiver
        CmbRec.DataTextField = "Name"
        CmbRec.DataValueField = "ReceiverID"
        CmbRec.DataBind()

        CmbDonor.DataSource = dtDonor
        CmbDonor.DataTextField = "Name"
        CmbDonor.DataValueField = "DonorID"
        CmbDonor.DataBind()

CodeB:
Dim CountryOrigin As New DataSet
            CountryOrigin.ReadXml(Server.MapPath("~/App_Data/Receiver.xml"))
            CmbRec.DataSource = CountryOrigin.Tables(0)
            CmbRec.DataMember = "Name"
            CmbRec.DataTextField = "Name"
            CmbRec.DataValueField = "ReceiverID"
            CmbRec.DataBind()
0
 
vcharlesAuthor Commented:
Hello,

This is where I'm confused, the Dropdownlist  doesn't have a click event. Do you mean the Javascript will force a click?

I am populating the Dropdownlist with Code A from a button's click event, where od I add the Javascript to execute code B to populate both controls with the xml files' data when I click on the control?

 Dim RecName As XmlNodeList = RecStream.SelectNodes("Root/Receiver")
        Dim DonorName As XmlNodeList = DonorStream.SelectNodes("Root/Donor")

        'If IsPostBack = True Then
        ' create a new XML Document object for each XML file
        Dim xdDonor As New Xml.XmlDocument
        Dim xdLink As New Xml.XmlDocument
        Dim xdReceiver As New Xml.XmlDocument

        ' load all of the XML documents
        xdDonor.Load(Server.MapPath("~/App_Data/Donor.xml"))
        xdLink.Load(Server.MapPath("~/App_Data/Link.xml"))
        xdReceiver.Load(Server.MapPath("~/App_Data/Receiver.xml"))

        ' Extract the ReceiverID and DonorID values from the current node
        Dim ReceiverID As String
        Dim DonorID As String
        Dim Link_ID As String
        Dim DonorID2 As String
        Dim Name As String
        Dim ReceiverID2 As String

        dtReceiver = New DataTable
        dtReceiver.Columns.Add("ReceiverID", GetType(String))
        dtReceiver.Columns.Add("Name", GetType(String))
        dtDonor = New DataTable
        dtDonor.Columns.Add("DonorID", GetType(String))
        dtDonor.Columns.Add("Name", GetType(String))

        Session("CurrentIndex") = 0
        Session("dtDonor") = dtDonor
        Session("dtReceiver") = dtReceiver

        MsgBox(C1Country.SelectedItem.Value)
        ' This outer loop will iterate over the Link file where the ReceiverID node is equal to our search criteria
        For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/Link[ReceiverID='" & C1Country.SelectedItem.Value & "']")
            Link_ID = xnLink.SelectSingleNode("Link_ID").InnerText
            ReceiverID = xnLink.SelectSingleNode("ReceiverID").InnerText
            DonorID = xnLink.SelectSingleNode("DonorID").InnerText

            ' Perform another similar search based upon the Donor XML file
            For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root/Donor[DonorID='" & DonorID & "']")

                '' Extract the DonorID and Name values from the current node


                DonorID2 = xnDonor.SelectSingleNode("DonorID").InnerText
                Name = xnDonor.SelectSingleNode("Name").InnerText
                dtDonor.Rows.Add({DonorID2, Name})
            Next
            dtLinkID = New DataTable
            dtLinkID.Columns.Add("Link_ID", GetType(String))
            ' Perform yet another practically identical search on the Receiver XML file
            For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/Root/Receiver[ReceiverID='" & ReceiverID & "']")
                ' Extract the ReceiverID and Name values from the current node
                ReceiverID2 = xnReceiver.SelectSingleNode("ReceiverID").InnerText
                Name = xnReceiver.SelectSingleNode("Name").InnerText
                dtReceiver.Rows.Add({ReceiverID2, Name})
            Next
            ' Extract the Link_ID avalues from the current node
            LinkID2 = xnLink.SelectSingleNode("Link_ID").InnerText
            dtLinkID.Rows.Add({LinkID2})
        Next

        CmbRec.DataSource = dtReceiver
        CmbRec.DataTextField = "Name"
        CmbRec.DataValueField = "ReceiverID"
        CmbRec.DataBind()

        CmbDonor.DataSource = dtDonor
        CmbDonor.DataMember = "Name"
        CmbDonor.DataTextField = "Name"
        CmbDonor.DataValueField = "DonorID"
        CmbDonor.DataBind()

Code B:

Dim CountryOrigin As New DataSet
            CountryOrigin.ReadXml(Server.MapPath("~/App_Data/Receiver.xml"))
            CmbRec.DataSource = CountryOrigin.Tables(0)
            CmbRec.DataMember = "Name"
            CmbRec.DataTextField = "Name"
            CmbRec.DataValueField = "ReceiverID"
            CmbRec.DataBind()

Dim User As New DataSet
            User.ReadXml(Server.MapPath("~/App_Data/Donor.xml"))
            CmbDonor.DataSource = User.Tables(0)
            CmbDonor.DataMember = "Name"
            CmbDonor.DataTextField = "Name"
            CmbDonor.DataValueField = "DonorID"
            CmbDonor.DataBind()
Thanks,

Victor


0
 
ivan_vaguninCommented:
Dropdownlist does not have a server-side Click event, but you can use client onclick to run a javascript postback function and then handle postback in server code (see my prev comment 36984125)
0
 
vcharlesAuthor Commented:
Hello,

I understand adding the code below in my form

Public Sub RaisePostBackEvent(ByVal eventArgument As String)
        If (eventArgument = "CmbRecClick") Then
            'Execute Code B
        End If
    End Sub


But I still don't understand how do I add that part of the code in the project

CmbRec.Attributes("onclick") = ClientScript.GetPostBackEventReference(this, "CmbRecClick")


Can you please send me more information on how to solde this issue.

Thanks,

Victor
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.