简体   繁体   English

使用数据库中的项填充checkboxlist?

[英]Populate checkboxlist with items from database?

Ok, so I want to populate/bind some data to a checkboxlist but cannot seem to binf the right values? 好的,所以我想将一些数据填充/绑定到一个复选框列表,但似乎无法将正确的值binf? I want to populate it with the information from a ROW, not the whole column which is what's happening to me. 我想用ROW中的信息填充它,而不是整个列,这就是我发生的事情。 Here's some code anyways to show you what the problem is. 无论如何,这里有一些代码可以向您展示问题所在。

This is the code in the xaml 这是xaml中的代码

<form id="form1" runat="server">
<div>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Option1], [Option2], [Option3] FROM [Questions] WHERE ([QuestionID] = @QuestionID)">
        <SelectParameters>
            <asp:Parameter DefaultValue="1" Name="QuestionID" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

</div>
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataTextField="QuestionID" DataValueField="QuestionID">
    </asp:CheckBoxList>
</form>

This is my database (only an example one) Links are from gyazo 这是我的数据库(仅举例) 链接来自gyazo

Questions 问题

Data 数据


This is my code 这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

namespace ExampleCheckbox
{
public partial class Question_One : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(connectionString);

        SqlCommand command = new SqlCommand();
        command.Connection = con;
        command.CommandType = CommandType.Text;
        command.CommandText = "Select * from Questions";

        SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
        DataSet questionsDataSet = new DataSet();


            con.Open();
            dataAdapter.Fill(questionsDataSet, "Question");

            DataTable dt = questionsDataSet.Tables["Question"];

            foreach (DataRow dr in dt.Rows)
            {
                ListItem newItem = new ListItem(dr["Option1"].ToString(), dr["QuestionID"].ToString());
                CheckBoxList1.Items.Add(newItem);
            }

            CheckBoxList1.DataSource = questionsDataSet;
            CheckBoxList1.DataTextField = "Option1";
            CheckBoxList1.DataValueField = "QuestionID";
            CheckBoxList1.DataBind();
    }
}

} }


This is also the problem i'm having PROBLEM 这也是我遇到问题的问题

Thanks 谢谢

What you want to do is loop through the columns instead of the rows, and get the column names out instead. 你想要做的是遍历列而不是行,并取代列名。

Check out this question/answer here: 看看这个问题/答案:

http://social.msdn.microsoft.com/Forums/en-US/4b6ede3b-093d-46f1-8766-d4a96608997d/loop-thru-a-datatable-for-columnnames-columnvalues?forum=csharpgeneral http://social.msdn.microsoft.com/Forums/en-US/4b6ede3b-093d-46f1-8766-d4a96608997d/loop-thru-a-datatable-for-columnnames-columnvalues?forum=csharpgeneral

Here's a full example. 这是一个完整的例子。 This is what your code should look like: 这是你的代码应该是这样的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

namespace ExampleCheckbox
{
public partial class Question_One : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection con = new SqlConnection(connectionString);

        SqlCommand command = new SqlCommand();
        command.Connection = con;
        command.CommandType = CommandType.Text;
        command.CommandText = "Select * from Questions";

        SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
        DataSet questionsDataSet = new DataSet();


            con.Open();
            dataAdapter.Fill(questionsDataSet, "Question");

            DataTable dt = questionsDataSet.Tables["Question"];

            int i = 0;
            string str1 = string.Empty;
        int i = 0;
                    dr = dt.Rows(ClientID);   //whatever you're using for the row index
            foreach (DataColumn dc in dt.Columns)
            {
                ListItem newItem = new ListItem(dr[dc].ToString(), i.ToString());
                CheckBoxList1.Items.Add(newItem);
                i++;
            }
        }
    } 
        } 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM