[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with executing command when click on combobox

Posted on 2011-10-10
19
Medium Priority
?
227 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:vcharles
  • 10
  • 9
19 Comments
 
LVL 18

Expert Comment

by:ivan_vagunin
ID: 36943117
Hi! What do you mean saying Combobox? Dropdownlist or Ajaxtoolkit combobox? Can you post markup and code?
0
 

Author Comment

by:vcharles
ID: 36943240
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
 
LVL 18

Expert Comment

by:ivan_vagunin
ID: 36943309
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:vcharles
ID: 36943478
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
 
LVL 18

Expert Comment

by:ivan_vagunin
ID: 36943888
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
 

Author Comment

by:vcharles
ID: 36945426
I'm sorry, you lost me, what do you mean by page markup?  
0
 
LVL 18

Expert Comment

by:ivan_vagunin
ID: 36946892
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
 

Author Comment

by:vcharles
ID: 36947422
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
 
LVL 18

Expert Comment

by:ivan_vagunin
ID: 36953785
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
 

Author Comment

by:vcharles
ID: 36954248
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
 
LVL 18

Expert Comment

by:ivan_vagunin
ID: 36955151
The second part you should add in codebehind file (By 'DD handler" is dropdownlist handling procedure)
0
 

Author Comment

by:vcharles
ID: 36968297
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
 

Author Comment

by:vcharles
ID: 36975166
Hello,

Can you please send me more details on how to solve this issue?
0
 
LVL 18

Expert Comment

by:ivan_vagunin
ID: 36976351
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
 

Author Comment

by:vcharles
ID: 36976398
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
 
LVL 18

Accepted Solution

by:
ivan_vagunin earned 2000 total points
ID: 36984125
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
 

Author Comment

by:vcharles
ID: 36999872
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
 
LVL 18

Expert Comment

by:ivan_vagunin
ID: 37013963
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
 

Author Comment

by:vcharles
ID: 37091184
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline
Suggested Courses

830 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