Dropdown Event

Last post 08-08-2007 1:02 PM by edouard89. 5 replies.

Sort Posts:

  • Dropdown Event

    08-08-2007, 11:57 AM
    • Loading...
    • edouard89
    • Joined on 07-30-2007, 12:10 PM
    • Posts 17

    C# page. 

    I have two dropdowns. They are both independant of each other. When I select from one of the dropdowns data is displayed in a gridview. When I select from another dropdown the data is displayed based on that choice.

    My problem is that the choice from the other dropdown is still selected so I cant select it again unless I select another choice from the same drop down.

    How would I be able to change the selection in that dropdown list back to lets say "-- Select a Driver --" or "-- Select a Route --" .

    Here are my dropdowns: 

    <br />
    <asp:Label ID="Label1" runat="server" Text="Driver:" Width="88px"></asp:Label>
    <asp:DropDownList ID="DropDownList1" runat="server" Width="159px" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="employee_num" DataValueField="employee_name" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" OnDataBound="DropDownList1_DataBound">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MobileDataConnectionString %>"
       SelectCommand="SELECT employee_num,  [first_name] +' '+[last_name] as employee_name FROM [employee]"></asp:SqlDataSource>
    <br />
    <asp:Label ID="Label6" runat="server" Text="Route:" Width="88px"></asp:Label>
    <asp:DropDownList ID="DropDownList2" runat="server" Width="159px" AutoPostBack="True" DataSourceID="SqlDataSource2" DataTextField="rid" DataValueField="route_id" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged" OnDataBound="DropDownList2_DataBound">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:MobileDataConnectionString %>"
       SelectCommand="SELECT route, route_id, route + ', ' + CONVERT (char(3), route_id) AS rid FROM stop WHERE (DATEDIFF(dayofyear, date, GETDATE()) < 31) GROUP BY route, route_id">
    </asp:SqlDataSource>
     And the events I am using with them: 
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
    txtEmployeeName.Text = "Name";
    txtEmployeeNum.Text = "0";
    //Resets the fields above so the data dosnt combine with route date.
    txtRouteID.Text = DropDownList2.SelectedValue;
    }
    
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    txtRouteID.Text = "0";
    //Resets the field above so the data dosnt combine with the employee data.
    txtEmployeeNum.Text = DropDownList1.SelectedItem.Text;
    txtEmployeeName.Text = DropDownList1.SelectedValue;
    }
    
    protected void DropDownList1_DataBound(object sender, EventArgs e)
    {
    DropDownList1.Items.Insert(0, new ListItem("-- Select By Driver --"));
    }
    
    protected void DropDownList2_DataBound(object sender, EventArgs e)
    {
    DropDownList2.Items.Insert(0, new ListItem("-- Select By Route --"));
    //Problems:--> not an INT = when selected will cause an error.
    }

     

    So if any one has any input on how I can acomplish this it will be greatly apreciated.

     

     

    Filed under: , ,
  • Re: Dropdown Event

    08-08-2007, 12:31 PM
    • Loading...
    • Lee Dumond
    • Joined on 11-03-2004, 10:51 AM
    • Decatur, IL USA
    • Posts 903

     Set EnableViewState = "false" in the dropdowns.

    // ******************************
    if (this.PostHelpedYou)
    {
    MarkAsAnswer();
    }
  • Re: Dropdown Event

    08-08-2007, 12:32 PM
    • Loading...
    • crewe2007
    • Joined on 07-18-2007, 1:56 PM
    • Posts 35

    Sorry mate cant get exactly what you want. Can you be little more clear on what you want?

  • Re: Dropdown Event

    08-08-2007, 12:34 PM
    Answer
    • Loading...
    • Kelsey
    • Joined on 03-14-2003, 1:50 PM
    • BC, Canada
    • Posts 797

     Why don't you just set the opposite dropdownlists selected value to the inital value when the other is selected?

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {   
        txtRouteID.Text = "0";
        //Resets the field above so the data dosnt combine with the employee data.
        txtEmployeeNum.Text = DropDownList1.SelectedItem.Text;
        txtEmployeeName.Text = DropDownList1.SelectedValue;
        DropDownList2.SelectedIndex = 0;
    }
    Then do the same type of thing for your other DDL.
    C# / C++ Developer
  • Re: Dropdown Event

    08-08-2007, 12:41 PM
    • Loading...
    • johram
    • Joined on 06-13-2006, 6:36 AM
    • Sweden
    • Posts 3,352
    • Moderator

    You could try adding a DropDownList1.SelectedIndex = 0 in your DropDownList1_DataBound. Will it do the trick for you?

    If this post was useful to you, please mark it as answer. Thank you!
  • Re: Dropdown Event

    08-08-2007, 1:02 PM
    • Loading...
    • edouard89
    • Joined on 07-30-2007, 12:10 PM
    • Posts 17

    OK thank you every one for your input I was able to do what I wanted using Kelsey's advice.

Page 1 of 1 (6 items)
Microsoft Communities
Page view counter