0

在我的网页上,我有 2 个文本框,它们分别将输入作为年份和月份。然后根据这些值从数据库表中检索一个称为工作日的列值。它的值是这种格式(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>
4

2 回答 2

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();
于 2013-06-24T09:19:26.447 回答
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>

希望能帮助到你。

于 2013-06-24T13:01:29.490 回答