如何将控件提取到按钮单击事件中并将它们存储到sql-server中

时间:2013-12-04 13:07:24

标签: c# asp.net

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)            
            StartDate_TB.Text = DateTime.Today.ToShortDateString();            
        EventDuration();            
    }

    private void EventDuration()
    {            
        int n = Int32.Parse(EventDuration_DDL.SelectedItem.ToString());
        for (int i = 0; i < n; i++)
        {
            Label NewLabel = new Label();
            NewLabel.ID = "Label" + i;

            CheckBox newcheck = new CheckBox();
            newcheck.ID = "CheckBox" + i;
            newcheck.AutoPostBack = true;
            newcheck.CheckedChanged += new EventHandler(newcheck_CheckedChanged);                
            this.Labeldiv.Controls.Add(NewLabel);
            this.Labeldiv.Controls.Add(newcheck);
            this.Labeldiv.Controls.Add(new LiteralControl("<br/>"));
        }
    }        

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

        if (currentCheckbox.Checked)
        {         
            SlotDuration();
        }     
    }

    public void SlotDuration()
    {
        DateTime start = DateTime.Parse(StartTime_DDL.SelectedItem.Text);
        DateTime end = DateTime.Parse(EndTime_DDL.SelectedItem.Text);
        double duration = double.Parse(SlotDuration_DDL.SelectedItem.Text);
        string header = "<div class='priority low'><span><strong>{0}</strong></span></div>";
        string header1 = "<div class='priority medium'><span><strong>{0}</strong></span></div>";
        string morning = "";
        string afternon = "";
        bool doneMornHeader = false, doneAfternoonHeader = false;
        this.Timediv.Controls.Add(new LiteralControl("<div class='span6'>"));
        int k = 0;
        while (true)
        {               
            DateTime dtNext = start.AddMinutes(duration);                
            if (start > end || dtNext > end)
                break;

            if (start < DateTime.Parse("12:00 PM"))
            {
                if (!doneMornHeader)
                {
                    Label head = new Label();
                    head.Text = string.Format(header, "Morning");
                    this.Timediv.Controls.Add(head);
                    doneMornHeader = true;
                }
                morning = start.ToShortTimeString() + "-" + dtNext.ToShortTimeString();
                Label lbl = new Label();

                lbl.ID = "ImpLabel" + k;
                lbl.Text = morning;
                CheckBox cb = new CheckBox();
                cb.ID = "ImpCheckbox" + k;

                this.Timediv.Controls.Add(lbl);                                 
                this.Timediv.Controls.Add(cb);
                this.Timediv.Controls.Add(new LiteralControl("<br>"));                   
            }
            else
            {
                if (!doneAfternoonHeader)
                {
                    Label head1 = new Label();
                    head1.Text = string.Format(header1, "Afternoon");
                    this.Timediv.Controls.Add(head1);
                    doneAfternoonHeader = true;
                }
                afternon = start.ToShortTimeString() + "-" + dtNext.ToShortTimeString();
                Label lbl1 = new Label();
                lbl1.ID = "ImpLabel" + dtNext;
                lbl1.Text = afternon;
                CheckBox cb1 = new CheckBox();
                cb1.ID = "ImpCheckbox" + dtNext;
                this.Timediv.Controls.Add(lbl1);
                this.Timediv.Controls.Add(cb1);
                this.Timediv.Controls.Add(new LiteralControl("<br>"));
            }
            start = dtNext;
            k++;
        }
    }

    protected void Done_Button_Click(object sender, EventArgs e)
    {            
            StoreDynamicControls();            
    }
    protected void StoreDynamicControls()
    {
        con.Open();
        using (SqlCommand cmd2 = new SqlCommand("insert into EventSlots(EventDayId,SlotStartTime,SlotAvailable) values(@EventDayId,@SlotStartTime,@SlotAvailable)", con))
        {
            foreach (Control ctl in Timediv.Controls)
            {                    
                CheckBox cbx = ctl as CheckBox;
                Label lbl = ctl as Label;
                cmd2.Parameters.AddWithValue("@EventDayId", EventDayId);
                if (cbx != null)
                {
                    // it's a checkbox
                    if (cbx.ID.StartsWith("ImpLabel") == true)
                    {
                        cmd2.Parameters.AddWithValue("@SlotAvailable", cbx.Checked ? "0" : "1");
                    }
                }

                if (lbl != null)
                {
                    // it's a label
                    if (lbl.ID.StartsWith("ImpCheckbox") == true)
                    {
                        var paramSlotStarttime = cmd2.Parameters.Add("@SlotStartTime", SqlDbType.DateTime);
                        paramSlotStarttime.Value = lbl.Text;
                        break;
                    }
                }
                cmd2.ExecuteNonQuery();
            }
        }
    }

我创建了标签(lbl)&amp; SlotDuration()中的复选框(cb)。

现在我想将它们解压缩到Button click事件..

在按钮点击事件中我想将它们存储到sql-server ....

执行按钮点击事件...

但是在按钮点击事件中,它们对于那些标签显示为空&amp;复选框..

那么,我该怎么办?

2 个答案:

答案 0 :(得分:1)

所有变量和控件都放置在页面生命周期的末尾。因此,您需要一种方法来保存变量,您可以使用ViewState或Session。 我认为这就是你的问题。

答案 1 :(得分:0)

这应该用于获取控件

protected void StoreDynamicControls()
{
    con.Open();
    using (SqlCommand cmd2 = new SqlCommand("insert into EventSlots(EventDayId,SlotStartTime,SlotAvailable) values(@EventDayId,@SlotStartTime,@SlotAvailable)", con))
    {
        foreach (Control ctl in Timediv.Controls)
        {  
          CheckBox cbx = (CheckBox) e.FindControl("myCheckBox");
          Label lbl = (Label) e.FindControl("myLabel");
          ... /* rest of you code */
    }
}

但在你的情况下,你应该使用Session变量,因为你的控件是动态创建的

Session["ImpCheckBoxK.checked"] = (ImpCheckBoxK.checked)?1:0;

并在storeDynamicControls()

cmd2.Parameters.AddWithValue("@SlotAvailable",Convert.ToInt32(Session["ImpCheckB‌​oxK.checked"]));

所有控件重复相同的