[英]Multiple Selection in Dropdown and update query string
我根據公司及其客戶的選擇生成報告。 我對公司使用一個下拉菜單,並根據公司顯示客戶。 但是,我希望能夠選擇多個客戶,並且當用戶單擊視圖報告的按鈕時,我希望使用該選擇來更新查詢字符串。 如果選擇的客戶是三個,我想使用查詢字符串傳遞他們的客戶代碼。 查詢字符串按客戶選擇發送。
請幫我。
謝謝並恭祝安康。
米特什
在選擇頁面(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()" />
...
然后在您的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)));
}
...
這說明了一個無需回發頁面的示例。 如果需要發回代碼以處理后面代碼中的其他邏輯,則必須將<input>按鈕更改為ASP.NET按鈕控件,並處理其OnClick事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.