0

how I save dynamically created Labels and Checkboxes values into 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.

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

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

. . . . ..

2
  • It's my understanding that label values are not persisted in the ViewState so it might be prudent to move away from using labels for storing data. asp:HiddenField or input elements with display:none would be better practice. Commented Nov 27, 2013 at 12:45
  • possible duplicate of how to save dynamically created Checkboxes values into sql server Commented Nov 27, 2013 at 12:54

1 Answer 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:

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:

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@THOR - The SqlCommand is in the Wizard1_FinishButtonClick() just like in your posted code. I am not recommending to change any of your database interaction logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.