我目前有一个复选框,根据数据库中的值保持选中或取消选中。我希望复选框能够动态更改,以便在加载页面时检查它并将其更改为未选中,它将更改数据库值以及重定向到其他页面。现在我无法更改数据库值并重定向到其他页面。我目前已将autopost设置为true。
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection dataConnection = new SqlConnection(@"Data Source=184.168.47.21;Initial Catalog=RecruitPursuit;Persist Security Info=True;User ID=RecruitPursuit;Password=Recruit20!8"))
using (SqlCommand dataCommand =
new SqlCommand("select SportHasPositions from Sport Where Sport_Id = @Sport_Id", dataConnection))
{
SqlParameter param2 = new SqlParameter();
param2.ParameterName = "@Sport_Id";
param2.Value = Session["SportID"];
dataCommand.Parameters.Add(param2);
dataConnection.Open();
sportHasPositions = dataCommand.ExecuteScalar().ToString();
}
if (sportHasPositions == "No")
{
CheckBox1.Checked = true;
Panel1.Visible = false;
}
if (sportHasPositions == "Yes")
{
CheckBox1.Checked = false;
Panel1.Visible = true;
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked == false)
{
String conString = @"Data Source=184.168.47.21;Initial Catalog=RecruitPursuit;Persist Security Info=True;User ID=RecruitPursuit;Password=Recruit20!8";
SqlConnection con = new SqlConnection(conString);
//create a command behavior object
String cmdString = "UPDATE Sport SET SportHasPositions = @SportHasPositions WHERE Sport_Id = @Sport_Id";
SqlCommand cmd = new SqlCommand(cmdString, con);
SqlParameter param0 = new SqlParameter();
param0.ParameterName = "@SportHasPositions";
param0.Value = "Yes";
cmd.Parameters.Add(param0);
SqlParameter param1 = new SqlParameter();
param1.ParameterName = "@Sport_Id";
param1.Value = Session["SportID"];
cmd.Parameters.Add(param1);
int added = 0;
try
{
con.Open();
added = cmd.ExecuteNonQuery();
}
catch (Exception err)
{
// Output.Text = err.Message;
}
finally
{
con.Close();
}
Response.Redirect("Pick Positions.aspx");
}
if (CheckBox1.Checked == true)
{
String conString = @"Data Source=184.168.47.21;Initial Catalog=RecruitPursuit;Persist Security Info=True;User ID=RecruitPursuit;Password=Recruit20!8";
SqlConnection con = new SqlConnection(conString);
//create a command behavior object
String cmdString = "UPDATE Sport SET SportHasPositions = @SportHasPositions WHERE Sport_Id = @Sport_Id";
SqlCommand cmd = new SqlCommand(cmdString, con);
SqlParameter param0 = new SqlParameter();
param0.ParameterName = "@SportHasPositions";
param0.Value = "No";
cmd.Parameters.Add(param0);
SqlParameter param1 = new SqlParameter();
param1.ParameterName = "@Sport_Id";
param1.Value = Session["SportID"];
cmd.Parameters.Add(param1);
int added = 0;
try
{
con.Open();
added = cmd.ExecuteNonQuery();
}
catch (Exception err)
{
// Output.Text = err.Message;
}
finally
{
con.Close();
}
}
}
答案 0 :(得分:0)
问题是asp.net life cycle在Page_Load
事件之前运行CheckBox1_CheckedChanged
事件。由于在Page_Load
中,您设置了复选框,即使用户更改了复选框,Page_Load
也会在CheckBox1_CheckedChanged
更改数据库之前对其进行更改。要解决此问题,您可以使用Page.IsPostBack
标记,以便Page_Load
仅在初始页面加载时设置复选框。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
using (SqlConnection dataConnection = new SqlConnection(@"Data Source=184.168.47.21;Initial Catalog=RecruitPursuit;Persist Security Info=True;User ID=RecruitPursuit;Password=Recruit20!8"))
using (SqlCommand dataCommand =
new SqlCommand("select SportHasPositions from Sport Where Sport_Id = @Sport_Id", dataConnection))
{
SqlParameter param2 = new SqlParameter();
param2.ParameterName = "@Sport_Id";
param2.Value = Session["SportID"];
dataCommand.Parameters.Add(param2);
dataConnection.Open();
sportHasPositions = dataCommand.ExecuteScalar().ToString();
}
if (sportHasPositions == "No")
{
CheckBox1.Checked = true;
Panel1.Visible = false;
}
if (sportHasPositions == "Yes")
{
CheckBox1.Checked = false;
Panel1.Visible = true;
}
}
}