如何为动态创建的CheckBox编写已检查的更改事件

时间:2013-11-27 13:56:52

标签: c# asp.net checkbox

这里我动态创建了CheckBox ..

根据对ddl的选择,创建复选框并将其存储到sql server ....

但是在检查之前插入了那些......好的

所以现在我要检查,因为我创建了CheckedChange事件......

我想要在checkedchange事件中更新已插入的值.....

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++)
            {
                SqlCommand cmd = new SqlCommand("insert into EventDays(EventDay,EventStatus)values(@EventDay,@EventStatus)", con);
                Label NewLabel = new Label();
                NewLabel.ID = "Label" + i;
                var eventDate = dt.AddDays(i); //Calendar1.SelectedDate.Date.AddDays(i);
                NewLabel.Text = eventDate.ToLongDateString();

                CheckBox newcheck = new CheckBox();
                newcheck.ID = "CheckBox" + i;
                newcheck.CheckedChanged += 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/>"));

                cmd.Parameters.AddWithValue("@EventDay", NewLabel.Text);
                cmd.Parameters.AddWithValue("@EventStatus", newcheck.Checked ? 1 : 0);
                cmd.ExecuteNonQuery();

            }

        con.Close();    
    }

    void newcheck_CheckedChanged(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand("update EventDays set EventDay=@EventDay,EventStatus=@EventStatus where EventDay=@OldEventDay)", con);


    }

怎么写?????????????

第一次,所选项必须插入到sql-server中,再次选中的项必须更新已经在sql-server中的行...那么什么是代码???

1 个答案:

答案 0 :(得分:0)

使用System.Text.RegularExpressions;

    protected void EventDuration_DDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        con.Open();
        Labeldiv.Controls.Clear();
        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);
            Label NewLabel = new Label();
            NewLabel.ID = "Label" + i;
            var eventDate = DateTime.Now.AddDays(i); //Calendar1.SelectedDate.Date.AddDays(i);
            NewLabel.Text = eventDate.ToLongDateString();

            CheckBox newcheck = new CheckBox();
            newcheck.AutoPostBack = true;
            newcheck.ID = "CheckBox" + i;
            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/>"));

            cmd.Parameters.AddWithValue("@EventDay", NewLabel.Text);
            cmd.Parameters.AddWithValue("@EventStatus", newcheck.Checked ? 1 : 0);
            cmd.ExecuteNonQuery();

        }

    protected 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);
    }

嗨雷神,

  I think,Your main purpose to extract the corresponding label text and checkbox values to pass parameter to your sql statement.
  I have extracted the required values in the above code.Just map the values with your parameter and execute like you have previous done in the EventDuration_DDL_SelectedIndexChanged.All the Best buddy.