简体   繁体   English

下拉菜单中的多项选择并更新查询字符串

[英]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): 在选择页面(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: 然后在您的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. 如果需要发回代码以处理后面代码中的其他逻辑,则必须将<input>按钮更改为ASP.NET按钮控件,并处理其OnClick事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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