asp.net vb CheckBoxList 从 CSV 中选择

发布于 2024-10-31 23:59:56 字数 1200 浏览 0 评论 0原文

我正在学习 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 技术交流群。

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

发布评论

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

评论(2

所有深爱都是秘密 2024-11-07 23:59:56

您的代码是功能性的,但也许对可维护性进行一些调整会有所帮助。也不确定您是否一定需要嵌套循环来加载下拉项。

这应该只是您自己就编码实践做出决定的参考点。当然,对某些人有效的方法对其他人无效。

以下是我的编码方式...

ASP.NET Control:

<asp:CheckBoxList ID="CheckBoxListHomeType" runat="server" 
    CssClass="chkbox" RepeatLayout="Flow" CausesValidation="True" />
...
  • ID of CheckBoxListHomeType 很容易记住,智能感知将帮助我完成剩下的工作。 (或者另一种常见的方法是使用 cblHomeType 作为 ID)。让智能感知来帮助像 rh_type 这样的名称可能同样容易,但是类似于它是什么类型的控件的 ID 在维护代码

VB.NET 时确实很有帮助:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        LoadHomeTypes()
    End If
End Sub

Protected Sub LoadHomeTypes()
    Dim houseTypes = "House,Bungalow,Flat/Apartment,Any"
    For Each houseType As String In houseTypes.Split(",")
        CheckBoxListHomeType.Items.Add(New ListItem(houseType))
    Next
End Sub
  • 将逻辑保留在单独的 中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:

<asp:CheckBoxList ID="CheckBoxListHomeType" runat="server" 
    CssClass="chkbox" RepeatLayout="Flow" CausesValidation="True" />
...
  • ID of CheckBoxListHomeType is easy to remember and intellisense will get me the rest of the way. (or another common approach would be cblHomeType as the ID). Getting intellisense to help on a name like rh_type may be just as easy but IDs that resemble what kind of control it is can really help when maintaining code

VB.NET:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        LoadHomeTypes()
    End If
End Sub

Protected Sub LoadHomeTypes()
    Dim houseTypes = "House,Bungalow,Flat/Apartment,Any"
    For Each houseType As String In houseTypes.Split(",")
        CheckBoxListHomeType.Items.Add(New ListItem(houseType))
    Next
End Sub
  • Keeping the logic in a separate LoadHomeTypes function can make the code more readable.
  • Creating a new ListItem while iterating the list of homeTypes should remove the need to iterate over the CheckBoxList items, (if you need to clear out the existing ones you can add CheckBoxListHomeType.Items.Clear() to the top of the function)
  • the Not Page.IsPostBack check prevents the need for loading the drop down values every postback, unless you have need for them to change.
白衬杉格子梦 2024-11-07 23:59:56

这是很好的答案,试试这个

Dim ds As DataSet
    ds = Insertstu.searchrec(txtsearch.Text)
    txtnm.Text = ds.Tables(0).Rows(0)("stuname").ToString()
    txtadd.Text = ds.Tables(0).Rows(0)("stuaddress").ToString()
    txtph.Text = ds.Tables(0).Rows(0)("stuph").ToString()
    rdobtnsex.Text = ds.Tables(0).Rows(0)("sex").ToString()
    Dim arr As String()
    Dim quali As String = ds.Tables(0).Rows(0)("qualified").ToString()
    arr = quali.Split(",")
    Dim i As Integer
    For Each itm As String In arr
        For i = 0 To chkqualify.Items.Count - 1
            If chkqualify.Items(i).Text = itm Then
                chkqualify.Items(i).Selected = True
            End If
        Next
    Next

“chkqualify is checkboxlist id”

This is the good answers , try this

Dim ds As DataSet
    ds = Insertstu.searchrec(txtsearch.Text)
    txtnm.Text = ds.Tables(0).Rows(0)("stuname").ToString()
    txtadd.Text = ds.Tables(0).Rows(0)("stuaddress").ToString()
    txtph.Text = ds.Tables(0).Rows(0)("stuph").ToString()
    rdobtnsex.Text = ds.Tables(0).Rows(0)("sex").ToString()
    Dim arr As String()
    Dim quali As String = ds.Tables(0).Rows(0)("qualified").ToString()
    arr = quali.Split(",")
    Dim i As Integer
    For Each itm As String In arr
        For i = 0 To chkqualify.Items.Count - 1
            If chkqualify.Items(i).Text = itm Then
                chkqualify.Items(i).Selected = True
            End If
        Next
    Next

''chkqualify is checkboxlist id

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