使用 VB 在 asp.net 中使用复选框列表控件预选复选框
如何在页面加载时自动选择复选框列表中的一项?我的复选框列表中的项目来自 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> </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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将为 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.