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

时间:2022-12-20 13:43:05

how I save dynamically created Labels and Checkboxes values into 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();  
    }

I have created Labels and CheckBoxes dynamically in EventDuration_DDL_SelectedIndexChanged.

我在EventDuration_DDL_SelectedIndexChanged中动态创建了Labels和CheckBoxes。

now I want to save these values into sql server in Wizard1_FinishButtonClick.

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

how I save dynamically created Labels and Checkboxes values into sql server

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

. . . . ..

。 。 。 。 ..

1 个解决方案

#1


2  

You have to recreate those dynamically created labels and check boxes for the button click as well, because when the user clicks the finish button, that causes a post back to the server and the page is recreated, but the dynamic label and check box logic is not executed, thus your database saving logic cannot "find" these controls.

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

I recommend moving your dynamic label and check box creation logic to a separate method that can be called by the EventDuration_DDL_SelectedIndexChanged() and Wizard1_FinishButtonClick(), like this:

我建议将动态标签和复选框创建逻辑移动到可由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/>"));
    }  
}

Now in your event handlers, you can call this method, like this:

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

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

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

Alternatively, you can call BuildDynamicControls() in the Page_Load. This takes care of other buttons on the page destroying the values when they cause a post back, like this:

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

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

    BuildDynamicControls();
}

Note: If you go this route, then you will not need to call BuildDynamicControls() in the EventDuration_DDL_SelectedIndexChanged() or Wizard1_FinishButtonClick() methods, because the Page_Load happens before either of those events in the page life-cycle.

注意:如果你走这条路线,那么你就不需要在EventDuration_DDL_SelectedIndexChanged()或Wizard1_FinishButtonClick()方法中调用BuildDynamicControls(),因为Page_Load发生在页面生命周期中的任何一个事件之前。

#1


2  

You have to recreate those dynamically created labels and check boxes for the button click as well, because when the user clicks the finish button, that causes a post back to the server and the page is recreated, but the dynamic label and check box logic is not executed, thus your database saving logic cannot "find" these controls.

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

I recommend moving your dynamic label and check box creation logic to a separate method that can be called by the EventDuration_DDL_SelectedIndexChanged() and Wizard1_FinishButtonClick(), like this:

我建议将动态标签和复选框创建逻辑移动到可由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/>"));
    }  
}

Now in your event handlers, you can call this method, like this:

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

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

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

Alternatively, you can call BuildDynamicControls() in the Page_Load. This takes care of other buttons on the page destroying the values when they cause a post back, like this:

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

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

    BuildDynamicControls();
}

Note: If you go this route, then you will not need to call BuildDynamicControls() in the EventDuration_DDL_SelectedIndexChanged() or Wizard1_FinishButtonClick() methods, because the Page_Load happens before either of those events in the page life-cycle.

注意:如果你走这条路线,那么你就不需要在EventDuration_DDL_SelectedIndexChanged()或Wizard1_FinishButtonClick()方法中调用BuildDynamicControls(),因为Page_Load发生在页面生命周期中的任何一个事件之前。