ASP.NET w/ VB.NET - 在 ListBox 或 CheckBoxList 上进行多选并启用 AutoPostback
背景:我有一个 winForm 应用程序,它根据表单中提供的用户输入在数据库中注册用户,自动为用户生成随机密码和用户名,并通过电子邮件向用户发送一个链接以根据该链接进行申请选择的营销公司。
问题: 我将要填充的捆绑列表框设置为 true,但将自动回发设置为 true,但是一旦您单击 LB 运营商,捆绑列表框就会填充,并且不允许您选择多个运营商。
您对如何在启用回发功能的情况下允许多选有什么想法吗?
这是界面截图:
default.aspx 上的代码:
<td class="style1">
Carriers:</td>
<td bgcolor="#ffffff" class="style2">
<asp:ListBox AutoPostback="true" ID="lbCarriers" runat="server" Height="86px" Width="250px">
</asp:ListBox>
</td>
</tr>
<td class="style1">
Bundles:</td>
<td bgcolor="#ffffff" class="style2">
<asp:ListBox ID="bundles" runat="server" Height="86px" Width="250px">
</asp:ListBox>
</td>
</tr>
default.aspx.vb 上的代码:
Protected Sub lbCarriers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lbCarriers.SelectedIndexChanged
Dim splt() As String
Dim ac1 As Array
bundles.Items.Clear()
Dim item As ListItem = lbCarriers.SelectedItem
splt = item.ToString().Split("|")
ac1 = proxy.GetContractingBundles("test", "test", Trim(splt(0)))
For Each Pitem In ac1
bundles.Items.Add(Trim(splt(2)) & " | " & Pitem.FormBundleName)
Next
End Sub
感谢您的查看!
Background: I have a winForm app that registers a user in the database based on the user input provided in the form, auto-generates a random password and username for the user, and e-mails the user a link to take an application based on the marketing company selected.
Problem:
I got the bundles listbox to populate w/ autopostback set to true but the bundles listbox populates as soon as you click on an lbcarrier and it doesn't allow you select more than one carrier.
Do you have any ideas on how to allow multiselect with the postback feature on?
Here's a screenshot of the interface:
code on default.aspx:
<td class="style1">
Carriers:</td>
<td bgcolor="#ffffff" class="style2">
<asp:ListBox AutoPostback="true" ID="lbCarriers" runat="server" Height="86px" Width="250px">
</asp:ListBox>
</td>
</tr>
<td class="style1">
Bundles:</td>
<td bgcolor="#ffffff" class="style2">
<asp:ListBox ID="bundles" runat="server" Height="86px" Width="250px">
</asp:ListBox>
</td>
</tr>
code on default.aspx.vb:
Protected Sub lbCarriers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lbCarriers.SelectedIndexChanged
Dim splt() As String
Dim ac1 As Array
bundles.Items.Clear()
Dim item As ListItem = lbCarriers.SelectedItem
splt = item.ToString().Split("|")
ac1 = proxy.GetContractingBundles("test", "test", Trim(splt(0)))
For Each Pitem In ac1
bundles.Items.Add(Trim(splt(2)) & " | " & Pitem.FormBundleName)
Next
End Sub
Thanks for looking!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据定义,
AutoPostBack 属性
将自动导致发生回发。
要启用多个选择,您需要关闭
AutoPostBack
并启用SelectionMode
属性:请注意,
AutoPostBack
默认情况下为false
,因此我简单地省略了它。用户提交后,您可以使用类似于 lbCarriers_SelectedIndexChanged 事件中的逻辑来处理选定的列表框项目。然后,您可以循环浏览项目并检查项目的
选定的
属性或循环遍历GetSelectedIndices
方法 并通过索引引用项目。如果这不是您想要采取的路线,并且您希望在没有回发的情况下即时处理它,那么您需要编写一些 JavaScript。
编辑:浏览所选项目的代码与下面的代码类似,您可能会将其放置在由提交按钮的事件处理程序调用的方法中。
By definition the
AutoPostBack
property will automatically cause a postback to occur when the user changes the list selection.To enable multiple selections you'll need to turn
AutoPostBack
off and enable theSelectionMode
property:Note that
AutoPostBack
isfalse
by default so I simply omitted it.Once the user submits you can process the selected list box items with logic similar to what you have in the
lbCarriers_SelectedIndexChanged
event. You can then loop through the items and check the item'sSelected
property or loop through the results of theGetSelectedIndices
method and reference the items by their indices.If that's not the route you want to take, and you want it to be handled on the fly without a postback, then you'll need to write some JavaScript.
EDIT: the code to go through your selected items would be similar to the code below, and you would probably place it in a method that is called by the submitted button's event handler.
最简单的修复方法是关闭
AutoPostBack
并按照建议更改SelectionMode
。然后有一个按钮,获取捆绑包
。在该单击事件中,您可以添加代码以根据运营商列表框检索捆绑包。Easiest fix would be to turn
AutoPostBack
off, and change theSelectionMode
as suggested. Then have a button,Get Bundles
. In that click event you can add your code to retrieve the bundles based off of the Carrier listbox.