我如何将动态创建的标签和复选框值保存到sql server中

时间:2013-11-27 12:36:21

标签: c# asp.net sql-server

如何将动态创建的标签和复选框值保存到sql server

 protected void EventDuration_DDL_SelectedIndexChanged(object sender, EventArgs e)
    {
        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 = Calendar1.SelectedDate.Date.AddDays(i);
            NewLabel.Text = eventDate.ToLongDateString();

            CheckBox newcheck = new CheckBox();
            newcheck.ID = "CheckBox" + i;

            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/>"));
        }  
    }

 protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        con.Open();
        SqlCommand cmd1 = new SqlCommand("insert into Event(EventName,StartDate,EventDuration,StartTime,EndTime,SlotDuration) output inserted.EventId values(@EventName,@StartDate,@EventDuration,@StartTime,@EndTime,@SlotDuration)", con);
        cmd1.Parameters.AddWithValue("@EventName", EventName_TB.Text);
        cmd1.Parameters.AddWithValue("@StartDate", StartDate_TB.Text);
        cmd1.Parameters.AddWithValue("@EventDuration", EventDuration_DDL.Text);
        cmd1.Parameters.AddWithValue("@StartTime", StartTime_DDL.Text);
        cmd1.Parameters.AddWithValue("@EndTime", EndTime_DDL.Text);
        cmd1.Parameters.AddWithValue("@SlotDuration", SlotDuration_DDL.Text);
        Int32 id = (Int32)cmd1.ExecuteScalar();

        var label = Labeldiv.FindControl("Label1") as Label;
        var checkbox = Labeldiv.FindControl("CheckBox1") as CheckBox;
        using (SqlCommand cmd2 = new SqlCommand("insert into EventDays(EventDay,EventStatus)values(@EventDay,@EventStatus)", con))
        {
             int n = Int32.Parse(EventDuration_DDL.SelectedItem.ToString());

             for (int i = 0; i < n; i++)
             {
                 var paramDay = cmd2.Parameters.Add("@EventDay", SqlDbType.DateTime);
                 var paramStatus = cmd2.Parameters.Add("@EventStatus", SqlDbType.Int);

                     paramDay.Value = label.Text;
                     paramStatus.Value = checkbox.Checked ? 1 : 0;
                     cmd2.ExecuteNonQuery();

             }
        }
        con.Close();  
    }

我在EventDuration_DDL_SelectedIndexChanged中动态创建了标签和复选框。

现在我想在Wizard1_FinishButtonClick中将这些值保存到sql server中。

如何将动态创建的标签和复选框值保存到sql server

。 。 。 。 ..

1 个答案:

答案 0 :(得分:2)

您还必须重新创建动态创建的标签和按钮单击复选框,因为当用户单击完成按钮时,会导致回发到服务器并重新创建页面,但动态标签和检查框逻辑未执行,因此您的数据库保存逻辑无法“找到”这些控件。

我建议将动态标签和复选框创建逻辑移动到可由EventDuration_DDL_SelectedIndexChanged()Wizard1_FinishButtonClick()调用的单独方法,如下所示:

private void BuildDynamicControls()
{
    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 = Calendar1.SelectedDate.Date.AddDays(i);
        NewLabel.Text = eventDate.ToLongDateString();

        CheckBox newcheck = new CheckBox();
        newcheck.ID = "CheckBox" + i;

        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/>"));
    }  
}

现在在事件处理程序中,您可以调用此方法,如下所示:

protected void EventDuration_DDL_SelectedIndexChanged(object sender, EventArgs e)
{
    BuildDynamicControls();
}

protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
    BuildDynamicControls();
}

或者,您可以在BuildDynamicControls()中致电Page_Load。这会占用页面上的其他按钮,当它们导致回发时会破坏这些值,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    // Do other page load logic here

    BuildDynamicControls();
}
  

注意:如果你走这条路线,那么你不需要在BuildDynamicControls()EventDuration_DDL_SelectedIndexChanged()方法中拨打Wizard1_FinishButtonClick(),因为Page_Load发生在{{1}}之前页面生命周期中的事件。