【问题标题】:Apsx.net C# Prevent Page reload on button clickApsx.net C# 防止页面在按钮单击时重新加载
【发布时间】:2019-04-12 09:54:47
【问题描述】:

希望您能提供帮助。

我有一个 aspx web 应用程序,它有一个从 SQlite 填充的列表,用于存储手机号码

我有一个按钮,可以让我启用/禁用一个文本框 - 一切正常。

现在,当我过滤视图并单击“编辑”时,页面会重新加载。

按钮/文本框是从 SQLite 动态创建的。

          TextBox NES_editText = new TextBox();
                        NES_editText.CssClass = "editText";
                        NES_editText.Enabled = false;
                        NES_editText.Text = SQLReader["simName"].ToString();
                        NES_editText.ID = 300 + SQLReader["simNESID"].ToString();                            
                        Sim_Rows.Controls.Add(NES_editText);
                        Sim_Rows.Controls.Add(new LiteralControl("</div>")); // COL 2                                                    
                        Sim_Rows.Controls.Add(new LiteralControl("<div class=\"col col-3\" data-label=\"Edit Name\">"));
                        Button editButton = new Button();
                        editButton.CssClass = "editButton";
                        editButton.ID = SQLReader["simNESID"].ToString();                            
                        editButton.Click += new EventHandler(EditButton_Click);                         

                        Sim_Rows.Controls.Add(editButton);
                        Sim_Rows.Controls.Add(new LiteralControl("</div>")); // COL 3    

这是我的编辑按钮代码

private void EditButton_Click(object sender, EventArgs e)
    {
        Button NES_Edit = sender as Button;
        //TextBox NES_Text = new TextBox();

        String TxtID;

        //Response.Write(NES_Edit.ID);

            foreach (Control obj in Sim_Rows.Controls)
            {
                if (obj is TextBox)
                {
                    TxtID = obj.ID.Substring(3);
                    if (NES_Edit.ID == TxtID)  
                    {   
                        if (((TextBox)obj).Enabled == false)
                        {
                            ((TextBox)obj).Enabled = true;
                            ((TextBox)obj).Style.Add("border-bottom", "1px solid #000");
                            ((TextBox)obj).Style.Add("width", "270px");
                            NES_Edit.CssClass =  "button_enabled";
                            //Response.Write(NES_Edit.ID);
                            break;
                        }
                        else
                        {

                            ((TextBox)obj).Enabled = false;
                            ((TextBox)obj).Style.Remove("border-bottom");
                            NES_Edit.CssClass = "editButton";


                            SQL = "UPDATE Sims SET simName = @simName WHERE simNESID =" + NES_Edit.ID;

                            using (SQLiteConnection SQLCon = new SQLiteConnection(ConnectionString))
                            {

                                SQLiteCommand SQLCmd = new SQLiteCommand(SQL, SQLCon);
                                SQLCmd.Parameters.AddWithValue("@simName", ((TextBox)obj).Text );


                                try
                                {
                                    SQLCon.Open();
                                    SQLCmd.ExecuteNonQuery();

                                }
                                catch (SQLiteException Ex)
                                {
                                    Response.Write(Ex.Message);
                                }
                                SQLCon.Close();

                                // SEND EMAIL
                            }

                            break;

                        }



                    }

            }
        }



    }

当我过滤视图时,我有两个结果并单击编辑,页面重新加载。

有什么办法可以阻止吗?

【问题讨论】:

  • 我想这可能就是你要找的东西:stackoverflow.com/questions/21325211/…
  • 您是否尝试过UpdatePanel 或带有处理程序文件的传统Ajax?您要对抗的是 Web 表单的传统生命周期,您应该进行研究,因为对抗架构会在未来引发问题。
  • @Brad,我看过那篇文章 - 我已经尝试了其中一些,但它禁用了“点击”事件。
  • @Greg,谢谢 - 我会在生命周期中做一些事情。
  • @Azmodan 为您试了一下,希望对您有所帮助。

标签: c# asp.net


【解决方案1】:

遗憾的是,当 Microsoft 发布该框架时,他们认为 Web 应用程序应该像桌面应用程序一样工作。这个概念与构建 Web 应用程序的协议相冲突。 Web 应该体现无状态架构,但微软的 Web Form 框架是由状态驱动的。

以上是页面固有的工作方式。因此,当您按下按钮时,事件将被重新排序并执行。在这种情况下,执行所谓的 PostBack。发生这种情况时,服务器会接受该事件,然后将再次呈现页面。

为了解决这个问题,Microsoft 引入了一个名为 UpdatePanel 的控件。面板会将整个页面状态加载到内存中,然后再次呈现页面后,将在控件中显示修改后的部分。这允许状态驱动架构不向客户端传达服务器正在保留内容。

<asp:UpdatePanel id="upExample" runat="server">
     <ContentTemplate>
          <asp:Label id="lblExample" runat="server" Text="Initial Load."></asp:Label>
     </ContentTemplate>
</asp:UpdatePanel>

<asp:Button id="btnExample" runat="server" OnClick="UpdateLabel_Click" Text="Update" />

目前,每次页面初始加载时,您都会使用默认状态。但是在更新面板中我们有内容,但是现在当我们触发我们的按钮时:

protected void UpdateLabel_Click(object sender, EventArgs e) => 
 lblExample.Text = $"Button clicked at: {DateTime.Now:MMMM dd, yyyy hh:mm:ss}";

您会注意到屏幕闪烁消失了,因为UpdatePanel 中的控件是在服务器事件之后呈现的页面的唯一部分。另一种方法是使用 Ajax 方法和处理程序。

希望这能提供足够的信息来帮助您。

【讨论】:

  • 嗨,格雷格,这是有道理的 - 我会试一试并报告。
猜你喜欢
  • 2021-03-01
  • 2012-12-09
  • 2012-03-21
  • 2019-06-24
  • 2023-03-13
  • 2012-11-24
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多