ASP.NET w/ VB.NET - 在 ListBox 或 CheckBoxList 上进行多选并启用 AutoPostback

发布于 2024-10-31 19:13:35 字数 1520 浏览 6 评论 0原文

背景:我有一个 winForm 应用程序,它根据表单中提供的用户输入在数据库中注册用户,自动为用户生成随机密码和用户名,并通过电子邮件向用户发送一个链接以根据该链接进行申请选择的营销公司。

问题: 我将要填充的捆绑列表框设置为 true,但将自动回发设置为 true,但是一旦您单击 LB 运营商,捆绑列表框就会填充,并且不允许您选择多个运营商。

您对如何在启用回发功能的情况下允许多选有什么想法吗?

这是界面截图: screenshot

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:
screenshot

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城之時有危險 2024-11-07 19:13:35

根据定义,AutoPostBack 属性将自动导致发生回发。

要启用多个选择,您需要关闭AutoPostBack并启用SelectionMode 属性

<asp:ListBox SelectionMode="Multiple" ID="lbCarriers" runat="server"
    Height="86px" Width="250px">

请注意,AutoPostBack 默认情况下为 false,因此我简单地省略了它。

用户提交后,您可以使用类似于 lbCarriers_SelectedIndexChanged 事件中的逻辑来处理选定的列表框项目。然后,您可以循环浏览项目并检查项目的 选定的属性或循环遍历GetSelectedIndices 方法 并通过索引引用项目。

如果这不是您想要采取的路线,并且您希望在没有回发的情况下即时处理它,那么您需要编写一些 JavaScript。

编辑:浏览所选项目的代码与下面的代码类似,您可能会将其放置在由提交按钮的事件处理程序调用的方法中。

bundles.Items.Clear()
For Each item As ListItem In lbCarriers.Items
    If item.Selected Then
        Dim splt() As String
        Dim ac1 As Array
        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 If
Next

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 the SelectionMode property:

<asp:ListBox SelectionMode="Multiple" ID="lbCarriers" runat="server"
    Height="86px" Width="250px">

Note that AutoPostBack is false 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's Selected property or loop through the results of the GetSelectedIndices 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.

bundles.Items.Clear()
For Each item As ListItem In lbCarriers.Items
    If item.Selected Then
        Dim splt() As String
        Dim ac1 As Array
        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 If
Next
忱杏 2024-11-07 19:13:35

最简单的修复方法是关闭 AutoPostBack 并按照建议更改 SelectionMode。然后有一个按钮,获取捆绑包。在该单击事件中,您可以添加代码以根据运营商列表框检索捆绑包。

Easiest fix would be to turn AutoPostBack off, and change the SelectionMode 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文