我很困惑,也许我只熟悉pageLoad,IsPostBack或IsCallback的属性。我创建了一个名为“ first ”的布尔变量,并将其设置为 True 。 首次通过PageLoad,如果 first = False ,则会有一行代码,如果是, write = true 。 然后我有一个Run_write例程附加到一个按钮,当它运行时,如果用户回答是,对于初始问题,我使另一组单选按钮可见并将首先设置为false 。 (我在调试中运行它,我知道它会触及这行代码)...所以写入sql被忽略 因为写== false 并且窗口重新出现使用新的按钮......很棒!
此外,我再次浏览了PageLoad例程,如果(!first)将其设置为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();
}
}
}
}
}
答案 0 :(得分:1)
您网站的每个HTTP请求都会创建一个新的网页类实例 实例状态不会保留。
相反,您需要将状态存储在会话或ViewState中,具体取决于您要应用的内容。
答案 1 :(得分:0)
Page_Load
。这包括回复。
您可以查看IsPostBack
以查看当前的Page_Load执行是针对页面的第一次显示还是后续的回复。
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
// Do first-time things
}
else
{
// Do non-first-time things
}
}
请注意,页面对象的特定实例不会因访问权限而持久存在。因此,每次调用页面时都可能需要初始化一些信息。
答案 2 :(得分:0)
每次访问该页面时,您都在创建该类的新实例。
要区分用户点击按钮的页面加载与第一次到达页面的用户,您需要检查IsPostBack属性。
所以按照
的方式改写你的if// Code that always executes
if (IsPostBack)
{
// Code that only executes on initial page load
}
else
{
// Code that only executes when a postback event occurs
// e.g. A user clicks on a button.
}
答案 3 :(得分:0)
有一些方法可以维护控件的状态或值,所以我一直在维护不是回发中的控件的Viewstate,作为检查
(!IspostBack) // means when page loads first time
以及在else中写入的内容意味着发生回发时可以维护对象的视图状态。
如果没有使用Viewstate,我们也可以使用会话。
答案 4 :(得分:0)
里基尔,
网络的基本概念是(无状态),这就是你必须处理的原因,但无论如何你都可以阅读页面生命周期我建议你阅读它http://www.codeproject.com/Articles/20659/The-ASP-NET-Page-Lifecycle-A-Basic-Approach
你可以使用
if(!isPostBack)
{
first=true;
this portion of code only run when the page is requested first time
}else
{
first = false;
}
如果更改页面中的控件值或单击按钮,则isPostBack将为true。但是如果你刷新页面或点击F5,你的页面将再次被请求并且isPostBack将是假的;。
你也可以使用cookies或会话变量(我也建议不要加载太多的会话变量)。尝试阅读之前的链接,您将更清楚地将代码放在何处以获得最佳性能。
J.S。
答案 5 :(得分:0)
这里的答案很好,但技术性很强。我将尝试解释一些正在发生的事情。
当浏览器请求您的页面时,在服务器上,会创建一个新的类实例。
ASP.NET然后运行页面的其余部分,从page_load开始。这将调用所有其他函数,然后将HTML呈现为对您的请求的响应并将其发送回浏览器。我想将此解释为断开连接的环境。一旦发送响应,一切都在某种意义上被处理掉。你的变量,以前的工作等等都消失了。服务器就其而言,永远不会期望从浏览器中获得任何东西......它完成了它的工作。它接受了您对页面的请求,创建了一个结果并将其发布回浏览器。完成。
因此,每次调用时都要将您的代码视为新请求。
您可以使用ThatBlairGuy所述的IsPostback,因为如果您从浏览器回复回发,那么它将返回true,这意味着它已经在上一次回发中将此页面提供给浏览器。