English 中文(简体)
使用 c# 预选从 Asp. net 数据库字段选择检查框, 使用 c#
原标题:Pre-select checkBox from database fields in asp.net using c#

我有一个小组检查簿列表, 我想从数据库表格中预选它。 我有趣的是, 按照代码来选择它, 但是它不起作用 。

Data in table is store in following format example:-

1,2,
1,5,7,
1,2,3,4,

< 强 > HTML

<asp:checkboxlist id="chkBoxDaysList" runat="server">
 <asp:listitem  runat="server" value="1"  Text="Sunday" />
 <asp:listitem  runat="server" value="2" Text="Monday" />
 <asp:listitem  runat="server" value="3" Text="Tuesday" />
 <asp:listitem  runat="server" value="4" Text="Wednesday" />
 <asp:listitem  runat="server" value="5" Text="Thrusday" />
 <asp:listitem  runat="server" value="6" Text="Friday" />
 <asp:listitem  runat="server" value="7" Text="Saturday" />
</asp:checkboxlist>

< 加固> C# 代码, 用于根据先前保存的数据 预选复选框

public void getSelectedDays() { IDataReader dr;

String strSqlDays = "SELECT * FROM EventCalender WHERE rowID = 6";
DataSet ds = new DataSet();
ds = DataProvider.Connect_Select(strSqlDays);
dr = ds.Tables[0].CreateDataReader();

string[] s = new string[50];
while (dr.Read())
{
    s = dr["EventDays"].ToString().Split( , );

时 时
int length = s.Length;
for (int i = 0; i <= s.Length - 1; i++)
{
    string cntry = s[i];
    for (int j = 0; j <= chkBoxDaysList.Items.Count - 1; j++)
    {
        if (chkBoxDaysList.Items[j].Text == s[i])
        {
            chkBoxDaysList.Items[j].Selected = true;
            break;
        时 时
    时 时
时 时

时 时

现在的代码不会产生任何错误, 但是没有同时选择任何复选框

最佳回答

您正在用 DB 的值检查复选框的文本。 请用 DB 记录中的值检查复选框列表的值。 校正的代码显示在下面 & gt;

 if (chkBoxDaysList.Items[j].Value == s[i])
        {
            chkBoxDaysList.Items[j].Selected = true;
            break;
        }
问题回答
protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from empreg where empid=@empid", con);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@empid", txtid.Text);
            SqlDataAdapter adpt = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adpt.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                IDataReader dr = ds.Tables[0].CreateDataReader();
                txtfname.Text = ds.Tables[0].Rows[0][1].ToString();
                txtlname.Text = ds.Tables[0].Rows[0][2].ToString();
                RadioButtonList1.SelectedValue = ds.Tables[0].Rows[0][3].ToString();
                txtdob.Text = ds.Tables[0].Rows[0][4].ToString();
                txtdoj.Text = ds.Tables[0].Rows[0][5].ToString();
                txtsal.Text = ds.Tables[0].Rows[0][6].ToString();
                txtadd.Text = ds.Tables[0].Rows[0][7].ToString();
                DropDownList1.SelectedItem.Text = ds.Tables[0].Rows[0][8].ToString();
                //checkbox1
                string[] b = new string[50];
                while (dr.Read())
                {
                    b = dr["Dept"].ToString().Split( , );
                }

                for (int i = 0; i <= b.Length - 1; i++)
                {
                    foreach (ListItem item in this.CheckBoxList1.Items)
                        if (item.Value == b[i])
                        {
                            item.Selected = true;
                            i++;
                        }
                }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签