简体   繁体   中英

Multiple Selection in Dropdown and update query string

I generate a report according to selection of company and their customer. I use one dropdown for company and according to company the customer is displayed. However, I want to make it possible to select multiple customers, and when the user clicks on the button to the view report, I want to update the query string with that selection. If the selection of customer is three, I want to pass their customer code using query string. In sort querystring is send according to the selection of customer.

Please help me.

Thanks and regards.

Mitesh

In your selection page (aspx):

...
<script type="text/javascript">
        function submitSelection() {
            var customerList = document.getElementById('<%= lbCustomers.ClientID %>');
            var companyList = document.getElementById('<%= lbCompany.ClientID %>');
            var selectedCustomerQuery = [];
            for (var i = 0; i < customerList.options.length; i++) {                
                if (customerList.options[i].selected)
                    selectedCustomerQuery.push('customer_id=' + customerList.options[i].value);
            }
            location.href = 'Report.aspx?company_id=' + companyList.value + '&' + selectedCustomerQuery.join('&');
        }
    </script>
        <asp:DropDownList ID="lbCompany" runat="server" SelectionMode="Single" >
        <asp:ListItem Value="1">Company 1</asp:ListItem>
        <asp:ListItem Value="2">Company 2</asp:ListItem>
    </asp:DropDownList><br />
    <asp:ListBox ID="lbCustomers" runat="server" SelectionMode="Multiple">
        <asp:ListItem Text="John" Value="1"></asp:ListItem>
        <asp:ListItem Text="Paul" Value="2"></asp:ListItem>
        <asp:ListItem Text="Peter" Value="3"></asp:ListItem>
    </asp:ListBox><br />
    <input id="Button1" type="button" value="View Report" onclick="submitSelection()" />
...

Then in your Report.aspx.cs:

...
protected void Page_Load(object sender, EventArgs e)
        {
            var selectedCompany = Request.QueryString["company_id"];
            //get passed selected customers, will be stored in an array
            var selectedCustomers = Request.QueryString.GetValues("customer_id");

            Response.Write(string.Format("Company ID: {0}, Customers: {1}", selectedCompany.ToString(), string.Join(",", selectedCustomers)));
        }
...

This illustrates an example without posting back to the page. If you need to post back to handle other logic in your code behind, you'll have to change the <input> button to an ASP.NET button control and handle its OnClick event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM