Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将控件提取到button click事件中并将其存储到sql server中 受保护的无效页面加载(对象发送方,事件参数e) { 如果(!IsPostBack) StartDate_TB.Text=DateTime.Today.ToSortDateString(); EventDuration(); } 私有void EventDuration() { int n=Int32.Parse(EventDuration_DDL.SelectedItem.ToString()); 对于(int i=0;i结束| | dtNext>结束) 打破 如果(开始_C#_Asp.net - Fatal编程技术网

C# 如何将控件提取到button click事件中并将其存储到sql server中 受保护的无效页面加载(对象发送方,事件参数e) { 如果(!IsPostBack) StartDate_TB.Text=DateTime.Today.ToSortDateString(); EventDuration(); } 私有void EventDuration() { int n=Int32.Parse(EventDuration_DDL.SelectedItem.ToString()); 对于(int i=0;i结束| | dtNext>结束) 打破 如果(开始

C# 如何将控件提取到button click事件中并将其存储到sql server中 受保护的无效页面加载(对象发送方,事件参数e) { 如果(!IsPostBack) StartDate_TB.Text=DateTime.Today.ToSortDateString(); EventDuration(); } 私有void EventDuration() { int n=Int32.Parse(EventDuration_DDL.SelectedItem.ToString()); 对于(int i=0;i结束| | dtNext>结束) 打破 如果(开始,c#,asp.net,C#,Asp.net,我已经在SlotDuration()中创建了标签(lbl)和复选框(cb) 现在我想将它们提取到Button click事件中 在按钮单击事件中,我想将它们存储到sql server 按钮单击事件已执行 但在按钮单击事件中,这些标签和复选框显示为空 那么,我该如何处理呢?所有变量和控件都将在页面生命周期结束时进行处理。因此,您需要一种持久化变量的方法,可以使用ViewState或Session。 我想这是你的问题。这应该用来控制控制 protected void Page_Load(ob

我已经在SlotDuration()中创建了标签(lbl)和复选框(cb)

现在我想将它们提取到Button click事件中

在按钮单击事件中,我想将它们存储到sql server

按钮单击事件已执行

但在按钮单击事件中,这些标签和复选框显示为空


那么,我该如何处理呢?

所有变量和控件都将在页面生命周期结束时进行处理。因此,您需要一种持久化变量的方法,可以使用ViewState或Session。
我想这是你的问题。

这应该用来控制控制

    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();
            }
        }
    }
但是在您的情况下,您应该使用会话变量,因为您的控件是动态创建的

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 */
    }
}
和在StoreDynamicControl()中

尽管如此
Session["ImpCheckBoxK.checked"] = (ImpCheckBoxK.checked)?1:0;
cmd2.Parameters.AddWithValue("@SlotAvailable",Convert.ToInt32(Session["ImpCheckB‌​oxK.checked"]));