Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to extract the controls into the button click event and store them into sql-server
    text
    copied!<pre><code> 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 &lt; 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("&lt;br/&gt;")); } } 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 = "&lt;div class='priority low'&gt;&lt;span&gt;&lt;strong&gt;{0}&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;"; string header1 = "&lt;div class='priority medium'&gt;&lt;span&gt;&lt;strong&gt;{0}&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;"; string morning = ""; string afternon = ""; bool doneMornHeader = false, doneAfternoonHeader = false; this.Timediv.Controls.Add(new LiteralControl("&lt;div class='span6'&gt;")); int k = 0; while (true) { DateTime dtNext = start.AddMinutes(duration); if (start &gt; end || dtNext &gt; end) break; if (start &lt; 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("&lt;br&gt;")); } 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("&lt;br&gt;")); } 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(); } } } </code></pre> <p>I have Created Labels(lbl) &amp; Checkboxes(cb) in the SlotDuration().</p> <p>now I want to extract them into the Button click event..</p> <p>in button click event I want to store them into sql-server....</p> <p>button click event is executed...</p> <p>but in the button click event it shows null for those labels &amp; checkboxes..</p> <p>then, how can I???</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload