1

我已经创建了LabelsCheckBoxes......但我想在检查后将 CheckBox 值存储为 0 或 1 以表示 'on' 或 'off' 到 sql server ...... 我该怎么办?

   protected void EventDuration_DDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        con.Open();
        DateTime dt = DateTime.Parse(StartDate_TB.Text);

        int n = Int32.Parse(EventDuration_DDL.SelectedItem.ToString());
        for (int i = 0; i < n; i++)
        {

            Label NewLabel = new Label();
            NewLabel.ID = "Label" + i;
            var eventDate = dt.AddDays(i);
            NewLabel.Text = eventDate.ToLongDateString();

            CheckBox newcheck = new CheckBox();
            newcheck.ID = "CheckBox" + i;
            newcheck.AutoPostBack = true;
            newcheck.CausesValidation = false;
            newcheck.CheckedChanged += new EventHandler(newcheck_CheckedChanged);
            this.Labeldiv.Controls.Add(new LiteralControl("<span class='h1size'>"));
            this.Labeldiv.Controls.Add(NewLabel);
            this.Labeldiv.Controls.Add(new LiteralControl("</span>"));
            this.Labeldiv.Controls.Add(new LiteralControl("<div class='make-switch pull-right' data-on='info'>"));
            this.Labeldiv.Controls.Add(newcheck);
            this.Labeldiv.Controls.Add(new LiteralControl("</div>"));
            this.Labeldiv.Controls.Add(new LiteralControl("<br/>"));
        }

        con.Close();
    }

    void newcheck_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox currentCheckbox = sender as CheckBox;
        string extractInteger = Regex.Match(currentCheckbox.ID, @"\d+").Value;
        Label currentlabel = (Label)Labeldiv.FindControl("Label" + extractInteger);
        con.Open();

        int n = Int32.Parse(EventDuration_DDL.SelectedItem.ToString());
        for (int i = 0; i < n; i++)
        {
            SqlCommand cmd = new SqlCommand("insert into EventDays(EventDay,EventStatus)values(@EventDay,@EventStatus)", con);
            cmd.Parameters.AddWithValue("@EventDay", currentlabel.Text);
            cmd.Parameters.AddWithValue("@EventStatus", currentCheckbox.Checked ? "true" : "false");
            cmd.ExecuteNonQuery();

        }
        con.Close();
    }

复选框必须根据 ddl 选择创建....如果我们选择 6,则必须创建 6 个复选框,之后当我们选中复选框时,值必须存储到 sql-server....

这些值没有存储到sql server中......

4

1 回答 1

0

假设EventStatus是一个数字列,我能想到的最懒的解决方案是

cmd2.Parameters.AddWithValue("@EventStatus", newcheck.Checked ? 1 : 0);
于 2013-11-14T11:52:40.723 回答