0

我很困惑,也许我只是熟悉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();
                }
            }
        }
    }
}
4

6 回答 6

1

对您站点的每个 HTTP 请求都会创建一个新的页面类实例。
不保留实例状态。

相反,您需要将状态存储在 session 或 ViewState 中,具体取决于您要应用的内容。

于 2013-06-05T20:28:08.347 回答
0

有一些方法可以维护控件的状态或值,所以我一直在维护控件的视图状态,而不是回发,作为检查

(!IspostBack) // means when page loads first time

以及 else 中写的任何内容都意味着当回发发生时,您可以在其中维护对象的视图状态。

如果不使用 Viewstate,我们也可以使用会话。

于 2013-06-05T20:35:36.717 回答
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 将为 false;。

您也可以使用 cookie 或会话变量(我也建议不要加载太多会话变量)。尝试阅读之前的链接,您会更清楚将代码放在哪里以获得最佳性能。

JS

于 2013-06-05T20:45:58.197 回答
0

这里的答案很好,但技术性很强。我将尝试解释一些正在发生的事情。

当浏览器请求您的页面时,在服务器上会创建您的类的新实例。

然后 ASP.NET 运行页面的其余部分,从您的 page_load 开始。这将调用您的所有其他函数,然后将 HTML 渲染为对您的请求的响应并将其发送回浏览器。我喜欢将其解释为断开连接的环境。一旦发送了响应,从某种意义上说,一切都被处理掉了。你的变量、以前的工作等等……都不见了。就服务器而言,它永远不会期望再次从浏览器中获得任何东西......它完成了它的工作。它接受了您对页面的请求,创建了一个结果并将其发布回浏览器。完毕。

因此,每次调用您的代码时都将其视为一个新请求。

您可以使用 ThatBlairGuy 所述的 IsPostback,因为如果您正在响应来自浏览器的回发,这将返回 true,这意味着它已经在上一次回发时将此页面提供给浏览器。

于 2013-06-05T20:48:07.000 回答
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
  }
}

请注意,您的页面对象的特定实例不会从访问到访问持续存在。所以每次调用页面的时候可能会有一些信息需要初始化。

于 2013-06-05T20:32:52.153 回答
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.
}
于 2013-06-05T20:33:03.590 回答