使用 VB 在 asp.net 中使用复选框列表控件预选复选框

发布于 2024-12-14 08:55:27 字数 1692 浏览 3 评论 0原文

如何在页面加载时自动选择复选框列表中的一项?我的复选框列表中的项目来自 SQL 数据库,我希望自动选择其中一个字段。如果我没有从数据库中检索复选框列表项,我已经找到了这个问题的简单解决方案,但我无法根据我的情况弄清楚如何执行此操作。任何帮助将不胜感激。

这是我的 2 个相关页面的代码:

ALCounties.aspx.vb

Imports System.Collections.Generic

Partial Class ALCounties
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim chkValues As Dictionary(Of String, Double) = _
        New Dictionary(Of String, Double)


    For Each Item As ListItem In CheckBoxList1.Items
        If (Item.Selected) Then
            If (Not Item.Value Is Nothing And Not String.IsNullOrEmpty(Item.Value())) Then
                chkValues(Item.Text.ToString) = Convert.ToDouble(Item.Value)
            Else
                chkValues(Item.Text.ToString) = 0
            End If

        End If
    Next Item

    Session("CheckedItems") = chkValues
    Response.Redirect("Cart.aspx")
End Sub
End Class

ALCounties.aspx

<asp:CheckBoxList ID="CheckBoxList1" runat="server" 
                DataSourceID="SqlDataSource1" DataTextField="AL_County" 
                DataValueField="AL_Fee">
            </asp:CheckBoxList>
       </div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"        ConnectionString="<%$ ConnectionStrings:advancedleadsConnectionString %>" 
                SelectCommand="SELECT [AL_Fee], [AL_County]  FROM [AL]">   </asp:SqlDataSource>

        <p>&nbsp;</p>
        <p>
        <asp:Button ID="Button1" runat="server" Text="Submit" />
        </p>

How do I make one of the items in my checkboxlist automatically selected upon page load? The items in my checkboxlist come from a SQL DB and I want one of the fields to be automatically selected. I have found simple solutions to this question if I was not retrieving my checkboxlist items from a Database, but I am having trouble figuring out how to do this based on my situation. Any help would be greatly appreciated.

Here is my code for my 2 relevant pages:

ALCounties.aspx.vb

Imports System.Collections.Generic

Partial Class ALCounties
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim chkValues As Dictionary(Of String, Double) = _
        New Dictionary(Of String, Double)


    For Each Item As ListItem In CheckBoxList1.Items
        If (Item.Selected) Then
            If (Not Item.Value Is Nothing And Not String.IsNullOrEmpty(Item.Value())) Then
                chkValues(Item.Text.ToString) = Convert.ToDouble(Item.Value)
            Else
                chkValues(Item.Text.ToString) = 0
            End If

        End If
    Next Item

    Session("CheckedItems") = chkValues
    Response.Redirect("Cart.aspx")
End Sub
End Class

ALCounties.aspx

<asp:CheckBoxList ID="CheckBoxList1" runat="server" 
                DataSourceID="SqlDataSource1" DataTextField="AL_County" 
                DataValueField="AL_Fee">
            </asp:CheckBoxList>
       </div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server"        ConnectionString="<%$ ConnectionStrings:advancedleadsConnectionString %>" 
                SelectCommand="SELECT [AL_Fee], [AL_County]  FROM [AL]">   </asp:SqlDataSource>

        <p> </p>
        <p>
        <asp:Button ID="Button1" runat="server" Text="Submit" />
        </p>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

〃安静 2024-12-21 08:55:27

我将为 CheckBoxList 的 OnDataBound 事件添加一个处理程序。在此处理程序中,您可以对控件的 .Items 属性进行 foreach 并评估选择项目的任何条件。

I would add a handler for the OnDataBound event of the CheckBoxList. In this handler you can foreach over the .Items property of the control and evaluate wahtever criteria you have for the item to be selected.

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