aspx vb.net 复选框列表从 sql server 填充

发布于 2024-08-18 21:48:50 字数 147 浏览 3 评论 0原文

你能像下拉列表一样从sql server查询填充复选框列表吗?与 autopostback=true?我正在使用 vb.net,并且有 50 个复选框,这些复选框将根据前一个下拉列表的所选值从数据库数据中显示。我还可以在每次值来自数据库时更改复选框的标签吗?标签应与复选框值相同。

can you populate checkboxlist from sql server query like a dropdownlist? with autopostback=true? i am using vb.net and have 50 checkboxes that shall show up from the database data depending on the selected value of the previous dropdownlist. also can i change the label of the checkbox each time the value is from DB? the label shall be same as the checkbox value.

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

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

发布评论

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

评论(1

寂寞清仓 2024-08-25 21:48:50

假设你的CheckBoxList的ControlId是myCheckBoxList:

Dim mySQL As String = "Name_of_stored_proceedure"
Dim mySqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection("The_connection_string")
Dim mySqlCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(mySQL, mySqlConnection)
mySqlCommand.CommandType = CommandType.StoredProcedure      
Dim myDataAdapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(mySqlCommand)
Dim myDataTable As New DataTable
mySqlConnection.Open()
myDataAdapter.Fill(myDataTable)
mySqlConnection.Close()
myCheckBoxList.DataSource = myDataTable
myCheckBoxList.DataBind()

这是使用存储过程的过程。如果您想使用直接 SQL 或参数化查询,请取出 mySqlCommand.CommandType = CommandType.StoredProcedure 并将 SQL 放入“Name_of_stored_proceedure”中。

请记住将数据库中的列名称放入您想要的值的 CheckBoxList 的 DataValueField 属性中,以及您想要的 DataTextField 的列名称用于文本。

Assuming your CheckBoxList's ControlId is myCheckBoxList:

Dim mySQL As String = "Name_of_stored_proceedure"
Dim mySqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection("The_connection_string")
Dim mySqlCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(mySQL, mySqlConnection)
mySqlCommand.CommandType = CommandType.StoredProcedure      
Dim myDataAdapter As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(mySqlCommand)
Dim myDataTable As New DataTable
mySqlConnection.Open()
myDataAdapter.Fill(myDataTable)
mySqlConnection.Close()
myCheckBoxList.DataSource = myDataTable
myCheckBoxList.DataBind()

This is the process to use a stored procedure. If you would like to use straight SQL or a parametrized query take out the mySqlCommand.CommandType = CommandType.StoredProcedure and put the SQL in for the "Name_of_stored_proceedure".

Remember to put the column name from the database in the DataValueField property of the CheckBoxList that you would like to for the the value and the column name for the DataTextField you would like to use for the text.

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