asp.net vb CheckBoxList 从 CSV 中选择
我正在学习 asp.net,需要一个 CheckBoxList,如果这些项目位于数据库中的 CSV 字符串中,则最初会选择这些项目。
我已经让它工作了,尽管我只是想知道我是否已经采取了最好的方法,因为它看起来有点啰嗦?
感谢您提供的任何帮助。
ASPX
<asp:CheckBoxList ID="rh_type" runat="server" CssClass="chkbox"
RepeatLayout="Flow" CausesValidation="True">
<asp:ListItem>House</asp:ListItem>
<asp:ListItem>Flat/Apartment</asp:ListItem>
<asp:ListItem>Bungalow</asp:ListItem>
<asp:ListItem>Any</asp:ListItem>
</asp:CheckBoxList>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
代码
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim i As Integer
Dim str_rh_type As String = "House,Bungalow"
Dim split As String() = str_rh_type.Split(","c)
For Each s As String In split
'Response.Write(s & "<br />")
For i = 0 To rh_type.Items.Count - 1
If rh_type.Items(i).Text = s Then
rh_type.Items(i).Selected = True
End If
Next
Next s
End Sub
再次感谢 J。
I am learning asp.net and needed to have a CheckBoxList which items will be initially selected if the are in a CSV string from a database.
I have got it working although I just wondered if I have gone about it the best way as it seemed a little long winded?
Thanks for any help provided.
ASPX
<asp:CheckBoxList ID="rh_type" runat="server" CssClass="chkbox"
RepeatLayout="Flow" CausesValidation="True">
<asp:ListItem>House</asp:ListItem>
<asp:ListItem>Flat/Apartment</asp:ListItem>
<asp:ListItem>Bungalow</asp:ListItem>
<asp:ListItem>Any</asp:ListItem>
</asp:CheckBoxList>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
CODE
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim i As Integer
Dim str_rh_type As String = "House,Bungalow"
Dim split As String() = str_rh_type.Split(","c)
For Each s As String In split
'Response.Write(s & "<br />")
For i = 0 To rh_type.Items.Count - 1
If rh_type.Items(i).Text = s Then
rh_type.Items(i).Selected = True
End If
Next
Next s
End Sub
Thanks again
J.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码是功能性的,但也许对可维护性进行一些调整会有所帮助。也不确定您是否一定需要嵌套循环来加载下拉项。
这应该只是您自己就编码实践做出决定的参考点。当然,对某些人有效的方法对其他人无效。
以下是我的编码方式...
ASP.NET Control:
CheckBoxListHomeType
很容易记住,智能感知将帮助我完成剩下的工作。 (或者另一种常见的方法是使用cblHomeType
作为 ID)。让智能感知来帮助像rh_type
这样的名称可能同样容易,但是类似于它是什么类型的控件的 ID 在维护代码VB.NET 时确实很有帮助:
中LoadHomeTypes
函数可以使代码更具可读性。homeTypes
列表时创建新的ListItem
应该无需迭代 CheckBoxList 项目(如果您需要清除现有项目,可以添加CheckBoxListHomeType.Items.Clear()
到函数顶部)Not Page.IsPostBack
检查可防止每次回发时加载下拉值,除非您需要它们改变。Your code is functional but maybe some tweaking for maintainability would help. Also not sure you necessarily need nested loops to load your drop down items.
This should be just a reference point to make your own decisions on coding practices. Certainly what works for some doesn't work for others.
Here's how I'd code this...
ASP.NET Control:
CheckBoxListHomeType
is easy to remember and intellisense will get me the rest of the way. (or another common approach would becblHomeType
as the ID). Getting intellisense to help on a name likerh_type
may be just as easy but IDs that resemble what kind of control it is can really help when maintaining codeVB.NET:
LoadHomeTypes
function can make the code more readable.ListItem
while iterating the list ofhomeTypes
should remove the need to iterate over the CheckBoxList items, (if you need to clear out the existing ones you can addCheckBoxListHomeType.Items.Clear()
to the top of the function)Not Page.IsPostBack
check prevents the need for loading the drop down values every postback, unless you have need for them to change.这是很好的答案,试试这个
“chkqualify is checkboxlist id”
This is the good answers , try this
''chkqualify is checkboxlist id