我很困惑,也许我只是熟悉pageLoad、IsPostBack或IsCallback的属性。我创建了一个名为“ first ”的布尔变量并将其设置为True。第一次通过PageLoad,有一行代码if first = False,如果是,write = true。然后我在按钮上附加了一个 Run_write 例程,当它运行时,如果用户对初始问题的回答是 Yes,我使另一组单选按钮可见并首先设置为 false。(我在调试中运行了这个,我知道它命中了这行代码)......所以写入 sql 被忽略 ,因为 write == false并且窗口重新出现了一组新的按钮......太好了!
此外,我再次执行 PageLoad 例程,如果 (!first) 将 write 设置为 TRUE,它就会命中该行。我的问题是首先已重新设置为true?我错过了什么?请注意,我可以通过使用是否选中新按钮集来解决此问题,但我可能不想走这条路,我确实想了解发生了什么。
代码如下。
namespace MEAU.Web.Components.SupportCenter
{
public partial class feedback : System.Web.UI.Page
{
String login;
String myurl;
String response;
String s_call;
String p_ship;
String wrnty;
Boolean write;
Boolean first = true;
protected void Page_Load(object sender, EventArgs e)
{
login = Sitecore.Security.Accounts.User.Current.Profile.Email;
myurl = Request.QueryString["value"];
s_call = "No";
p_ship = "No";
wrnty = "No";
// Hide the question Buttons
scall.Visible = false;
parts.Visible = false;
wrnt.Visible = false;
lit.Visible = false;
write = false;
if (!first)
write = true;
}
protected void Run_Write(object sender, EventArgs e)
{
// Get Reponse
if (yes.Checked)
{
response = "Yes";
// Display the quesiton buttons, and Hide the NO button
scall.Visible = true;
parts.Visible = true;
wrnt.Visible = true;
lit.Visible = true;
no.Visible = false;
first = false;
// Did this Prevent a Service call?
if (scall.Checked)
{
s_call = "Yes";
write = true;
}
// Did this Prevent a parts shipment?
if (parts.Checked)
{
p_ship = "Yes";
write = true;
}
// Is this under warranty?
if (wrnt.Checked)
{
wrnty = "Yes";
write = true;
}
// write = true;
}
if (no.Checked)
{
response = "No";
write = true;
}
if (write == true)
{
SqlConnection conn = new SqlConnection(Sitecore.Configuration.Settings.GetConnectionString("feedback"));
SqlCommand cmd = new SqlCommand("Insert_fb", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@login", login);
cmd.Parameters.AddWithValue("@url", myurl);
cmd.Parameters.AddWithValue("@response", response);
cmd.Parameters.AddWithValue("@dateTime", DateTime.Now);
cmd.Parameters.AddWithValue("@serviceCall", s_call);
cmd.Parameters.AddWithValue("@partsShipment", p_ship);
cmd.Parameters.AddWithValue("@warranty", wrnty);
try
{
conn.Open();
cmd.ExecuteNonQuery();
Response.Write("<script type='text/javascript'>parent.$.fancybox.close();</script>");
Response.Write("<script type='text/javascript'>return false;</script>");
}
catch (Exception ex)
{
throw new Exception("Error on file update" + ex.Message);
}
finally
{
conn.Close();
}
}
}
}
}