动态显示复选框

时间:2013-06-24 08:54:26

标签: asp.net mysql checkbox checkboxlist

在我的网页上,我有两个文本框,分别将输入作为年份和月份。然后根据这些值从Database表中检索一个称为工作日的列值。它的值是这种格式(111110011111001111100111110011)这里1表示工作日,0表示假期(可以是坐或太阳或任何其他假期)。现在,根据检索到的值,我想显示30/31(取决于月份日期)复选框和假期复选框应该被选中并保持未选中状态。它应该是这样的 1 2 3 4 5 6 7 ............................... 30 并在它们下面的复选框。现在使用的代码只显示1下方的1个复选框(这是第一个标题),然后下一个日期显示在转发器中,但复选框显示在下一行。你能告诉我这个问题吗?

背后的代码

  

公共类MyCheckBox           {               public bool IsChecked {get;组; }           }

    public void search(object sender, EventArgs e)
    {

       string cnnString = "Server=localhost;Port=3307;Database=leavesystem;Uid=root;Pwd=ashish";
        MySqlConnection cnx = new MySqlConnection(cnnString);
        cnx.Open();
        string cmdText = "Select WorkingDays from calender where Year = '" + year.Value + "' and Month = '" + month.Value + "' ";
        MySqlCommand cmd = new MySqlCommand(cmdText, cnx);
        if (month.Value == " January || March || May || July || August || October || December ")
        {
            string str = Convert.ToString(cmd.ExecuteScalar());
            List<MyCheckBox> list = new List<MyCheckBox>();
            foreach (char c in str)
                list.Add(new MyCheckBox { IsChecked = c == '0' });
            MyRepeater1.DataSource = list;
            MyRepeater1.DataBind();
        }
        else if (month.Value == " April || June || September || November ")
        {
            string str = Convert.ToString(cmd.ExecuteScalar());
            List<MyCheckBox> list = new List<MyCheckBox>();
            foreach (char c in str)
                list.Add(new MyCheckBox { IsChecked = c == '0' });
            MyRepeater2.DataSource = list;
            MyRepeater2.DataBind();
        }
        else if (month.Value == " February ")
        {
            string str = Convert.ToString(cmd.ExecuteScalar());
            List<MyCheckBox> list = new List<MyCheckBox>();
            foreach (char c in str)
                list.Add(new MyCheckBox { IsChecked = c == '0' });
            MyRepeater3.DataSource = list;
            MyRepeater3.DataBind();
        }

}

转发器看起来像这样

 <asp:Repeater ID="MyRepeater" runat="server">
>                       <HeaderTemplate>
>                              <table width="100%" class="table" border="1" style="font: 8pt verdana">
>                                <tr>
>                                      <%
>                                           for (int i = 1; i <= 31; i++)
>                                                   {
>                                                           Response.Write("<td>" + i + "</td>");
>                                                   }    
>                                      %>
>                               </tr>
>             
>                      </HeaderTemplate>
>                       <ItemTemplate>
>                               <tr> 
>                                 <td>
>                                         <asp:CheckBox ID="CheckBox1" Checked='<%# Eval("IsChecked") %>' runat="server" />
>                                 </td>
>                              </tr>
>                             </table>  
>                     </ItemTemplate>
>       </asp:Repeater>

<asp:Repeater ID="MyRepeater2" runat="server">
           <HeaderTemplate>
               <table width="100%" class="table" border="1" style="font: 8pt verdana">
                    <tr>
                       <% for (int i = 1; i <= 30; i++)
                          {
                              Response.Write("<td>" + i + "</td>");
                          } %>
                    </tr>
                    <tr>
           </HeaderTemplate>
           <ItemTemplate>
                    <td>
                    <asp:CheckBox ID="CheckBox1" Checked='<%# Eval("IsChecked") %>' runat="server" />
                    </td>
           </ItemTemplate>
           <FooterTemplate>
                    </tr>
               </table>
           </FooterTemplate>
       </asp:Repeater>

       <asp:Repeater ID="MyRepeater3" runat="server">
           <HeaderTemplate>
               <table width="100%" class="table" border="1" style="font: 8pt verdana">
                    <tr>
                       <% for (int i = 1; i <= 28; i++)
                          {
                              Response.Write("<td>" + i + "</td>");
                          } %>
                    </tr>
                    <tr>
           </HeaderTemplate>
           <ItemTemplate>
                    <td>
                    <asp:CheckBox ID="CheckBox1" Checked='<%# Eval("IsChecked") %>' runat="server" />
                    </td>
           </ItemTemplate>
           <FooterTemplate>
                    </tr>
               </table>
           </FooterTemplate>
       </asp:Repeater>

2 个答案:

答案 0 :(得分:0)

您不应在此处使用DataSet,因为您只需要一个值。

string cnnString = "Server=localhost;Port=3307;Database=leavesystem;Uid=root;Pwd=ashish";

MySqlConnection cnx = new MySqlConnection(cnnString);
cnx.Open();
string cmdText = "Select WorkingDays from calender where Year = '" + year.Value + "' and Month = '" + month.Value + "' ";
MySqlCommand cmd = new MySqlCommand(cmdText, cnx);

char[] a = Convert.ToString(cmd.ExecuteScalar()).ToCharArray();
ListItem[] items = new ListItem[a.Length];
for (int i = 0; i < items.Length; i++)
{
    items[i] = new ListItem(m.ToString());
    if (m == '0')
        items[i].Selected = true;
    else
        items[i].Selected = false;
    checks.Items.Add(items[i]);
    i++;
}
cnx.Close();

答案 1 :(得分:0)

让我再给你一个答案。 首先,您定义一个类,表示该复选框是否已被加密。

public class MyCheckBox
{
    public bool IsChecked { get; set; }
}

然后,填充列表:

string cnnString = "Server=localhost;Port=3307;Database=leavesystem;Uid=root;Pwd=ashish";

MySqlConnection cnx = new MySqlConnection(cnnString);
cnx.Open();
string cmdText = "Select WorkingDays from calender where Year = '" + year.Value + "' and Month = '" + month.Value + "' ";
MySqlCommand cmd = new MySqlCommand(cmdText, cnx);

string str = Convert.ToString(cmd.ExecuteScalar());
List<MyCheckBox> list = new List<MyCheckBox>();
foreach (char c in str)
    list.Add(new MyCheckBox { IsChecked = c == '1' });
MyRepeater.DataSource = list;
MyRepeater.DataBind();

最后,转发器看起来像:

<table>
    <tr>
        <%
            for (int i = 1; i <= 31; i++)
            {
                Response.Write("<td>" + i + "</td>");
            }    
        %>
    </tr>
    <tr>
        <asp:Repeater ID="MyRepeater" runat="server">
            <ItemTemplate>
                <td>
                    <asp:CheckBox ID="CheckBox1" Checked='<%# Eval("IsChecked") %>' runat="server" />
                </td>
            </ItemTemplate>
        </asp:Repeater>
    </tr>
</table>

希望它有所帮助。