locked
Using Xpath with XmlDatasource and DropDown Lists RRS feed

  • Question

  • User-72198832 posted

    I have two Drop Down Lists - One for PROJECTS and one for ACTIVITIES

    Each PROJECT has a unique set of ACTIVITIES

    My PROJECT data  contains the following Descriptions and Values:
    Dropdown List displays Descr Items and Code items are selectedValues

    Descr, Code
    Park Maintenance, PAR
    Street Maintenance, STR
    Building Mantenance, BLD

    ACTIVITY Data Has Descr, Act and Proj fields
    Dropdown List displays Descr Items and Act items are selectedValues - Proj field is used for filtering

    Sample Data:
    Descr, Act, Proj
    Mowing, 1, PAR
    TreeTrim,2, PAR
    Sweep, 1, STR
    Repair, 2, STR
    Paint, 1, BLD
    Windows, 2, BLD

    When I select a PROJECT I want only those ACTIVITIES related to that PROJECT loaded int the ACTIVITY DropDownList.
    Im using Xpath to accomplish this.

    Here's code I'm Using.

     Private Sub lstProject_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstProject.SelectedIndexChanged
         xmlActivities.DataFile = Server.MapPath("~\data\TRIMS_Net\") & Session("Serial") & "\Activities.xml"
         xmlActivities.XPath = "/Activity_List/Activity[ @Proj='" & sender.selectedvalue & "']"
         lstActivity.DataBind()                      'Bind to Activity List
         lstActivity.Selectedindex = -1
    
        End Sub

    This works fine the 1st time I select a project - correct activities are loaded into Activity List.
    Selecting a different Project causes its Activities to be appended to Existing Activities in Activity DropDown List
    How do I get only currently selected Data via Xpath to be in Activity List?

    Any help is appreciated.

    Lee

    Friday, January 24, 2020 7:05 PM

All replies

  • User409696431 posted

    Does your dropdownlist have AppendDataBoundItems="true"?

    Sunday, January 26, 2020 6:35 AM
  • User409696431 posted

    It's common to set a dropdownlist to AppendDataBoundItems="true" if you want to hard code a first option of "Select", or something similar.  This works untll you rebind the dropdownlist again, at which point it appends the new items after the existing ones.  You either have to clear the list before you rebind it, and insert the Select again, or just let AppendDataBoundItems="false" and always insert the "Select" list item in position 0 of the DDL after databinding.

    Sunday, January 26, 2020 3:03 PM
  • User-72198832 posted

    Found Problem.  I was binding XML Control  on every postback.

    All OK Now.

    Sunday, January 26, 2020 3:24 PM