In my ASP.NET web application I have a form submit with checkboxes.
If in the first option 2 or 3 are selected, then a new questionnaire opens with 7 more questions. It's necessary to select all fields to navigate to next one.
protected void btnNextSDGSection2_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(rblSDG1Question1.SelectedValue))
{
lblMessageSection3.Text = "<br />⚠ Please select an option for Q1 before proceeding.";
lblMessageSection3.ForeColor = System.Drawing.Color.Red;
return;
}
int recordId = (int)Session["companyProfileID"];
string selectedOption = rblSDG1Question1.SelectedValue;
string query= @"
SELECT COUNT(*)
FROM sdgAlignmentSurvey_Section2
WHERE sdgAlignmentSurvey_Section2ID = @id";
using (SqlConnection conn = SqlConnectionClass.GetConnection())
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@id", recordId);
int exists = (int)cmd.ExecuteScalar();
if (selectedOption == "0" || selectedOption == "1")
{
if (exists > 0)
{
string queryUpdate = @"
UPDATE sdgAlignmentSurvey_Section2
SET Q1 = @option
WHERE sdgAlignmentSurvey_Section2ID = @id";
using (SqlCommand cmdUpdate = new SqlCommand(queryUpdate, conn))
{
cmdUpdate.Parameters.AddWithValue("@option", selectedOption);
cmdUpdate.Parameters.AddWithValue("@id", recordId);
cmdUpdate.ExecuteNonQuery();
lblMessageSection3.Text = $"<br />✅The option '{selectedOption}' is updated successfully.";
lblMessageSection3.ForeColor = System.Drawing.Color.Green;
}
}
else
{
string queryInsert = @"
INSERT INTO sdgAlignmentSurvey_Section2 (sdgAlignmentSurvey_Section2ID, Q1)
VALUES (@id, @option)";
new SqlCommand("SET IDENTITY_INSERT sdgAlignmentSurvey_Section2 ON", conn).ExecuteNonQuery();
using (SqlCommand cmdInsert = new SqlCommand(queryInsert, conn))
{
cmdInsert.Parameters.AddWithValue("@id", recordId);
cmdInsert.Parameters.AddWithValue("@option", selectedOption);
cmdInsert.ExecuteNonQuery();
lblMessageSection3.Text = $"<br />✅The option '{selectedOption}' is inserted successfully.";
lblMessageSection3.ForeColor = System.Drawing.Color.Green;
}
new SqlCommand("SET IDENTITY_INSERT sdgAlignmentSurvey_Section2 OFF", conn).ExecuteNonQuery();
}
}
}
bool allValid = true;
if (selectedOption == "2" || selectedOption == "3")
{
var checkboxes = new[] {
CheckBoxList1, CheckBoxList2, CheckBoxList3,
CheckBoxList4, CheckBoxList5, CheckBoxList6, CheckBoxList7
};
foreach (var list in checkboxes)
{
if (string.IsNullOrEmpty(list.SelectedValue))
{
allValid = false;
break;
}
}
if (allValid)
{
SaveRelatedOptions(recordId, selectedOption);
}
else
{
lblMessageSection3.Text = "<br />⚠ Please select an option in all sub-questions before proceeding.";
lblMessageSection3.ForeColor = System.Drawing.Color.Red;
}
}
else
{
string queryDelete = "DELETE FROM sdgAlignmentSurvey_Section2_q1_options WHERE FK_companyProfileID = @recordId";
using (SqlCommand cmdDelete = new SqlCommand(queryDelete, conn))
{
cmdDelete.Parameters.AddWithValue("@recordId", recordId);
cmdDelete.ExecuteNonQuery();
}
}
Session["SDG_Q1_Answer"] = selectedOption;
if (allValid)
{
((Pages.SDG_Alignment_Flow.SDG_Alignment_Genel)this.Page).NextStep();
}
}
}
This code works properly and saves the selected options to the database as expected.
When all checkboxes are not selected, an error is displayed to select all, but the issue is that all the selected ones are reset to null.
How should I modify it so that the selected options do not reset and after the error (for missing checkboxes) I can see already the selected ones on page postback?
I have no related part in my if (Page.IsPostBack == false) part.
As well, I have a function to load saved question answers from the database as follows:
private void LoadQ1Answer()
{
using (SqlConnection conn = SqlConnectionClass.GetConnection())
{
conn.Open();
int recordId = (int)Session["companyProfileID"];
string query = @"
SELECT Q1
FROM sdgAlignmentSurvey_Section2
WHERE sdgAlignmentSurvey_Section2ID = @id";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@id", recordId);
object result = cmd.ExecuteScalar();
if (result != null && result != DBNull.Value)
{
string savedOption = result.ToString();
ListItem item = rblSDG1Question1.Items.FindByValue(savedOption);
if (item != null)
{
rblSDG1Question1.ClearSelection();
item.Selected = true;
if (savedOption == "2" || savedOption == "3")
{
pnlSDG1ExtraMessage.Visible = true;
LoadQ1Details(recordId, savedOption);
}
else
{
pnlSDG1ExtraMessage.Visible = false;
}
lblMessageSection2.Text = $"Selected: {item.Text}";
}
}
}
}
}
This function, is out of if (Page.IsPostBack == false) so the question answers are always loaded from the database and if I place that inside if (Page.IsPostBack == false) the results are not displayed at all.