简体   繁体   English

Apsx.net C# 防止页面重新加载按钮点击

[英]Apsx.net C# Prevent Page reload on button click

Hope you can help.希望你能帮忙。

I have aspx web application, which has a list populated from SQlite which is stored for mobile numbers我有一个 aspx web 应用程序,它有一个由 SQlite 填充的列表,该列表存储手机号码

I have a button, which i lets me enable / disable a textbox - which all works.我有一个按钮,可以让我启用/禁用文本框 - 一切正常。

Now, when I filter the view and click on "Edit" the page reloads.现在,当我过滤视图并单击“编辑”时,页面会重新加载。

The button/textbox is dynamically created from 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    

Here is my edit button code这是我的编辑按钮代码

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;

                        }



                    }

            }
        }



    }

When I filter the view, and i have two results and click on the edit, the page reloads.当我过滤视图时,我有两个结果并单击编辑,页面会重新加载。

Any way to stop that?有什么办法可以阻止吗?

Sadly, when Microsoft released the framework they had the notion that a web application should work like a desktop application.可悲的是,当微软发布框架时,他们认为 Web 应用程序应该像桌面应用程序一样工作。 This notion conflicted with protocol in which a web application is built upon.这个概念与构建 Web 应用程序的协议相冲突。 The web should embody a stateless architecture, but Microsoft's Web Form framework is driven by state. Web 应该体现无状态架构,但微软的 Web Form 框架是由状态驱动的。

页面生命周期

The above is how a page inherently will work.以上是页面固有的工作方式。 So when you hit a button, the events will be resequenced and executed.因此,当您按下按钮时,事件将被重新排序并执行。 In this case, execute what is called a PostBack.在这种情况下,执行所谓的 PostBack。 When this occurs the server takes the event, then will render the page again.发生这种情况时,服务器会接受该事件,然后将再次呈现页面。

To combat this nuisance, Microsoft introduced a control called an UpdatePanel .为了消除这种麻烦,Microsoft 引入了一个名为UpdatePanel的控件。 The panel will load the entire page state into memory, then once the page is rendered again, will display the modified portion within the control.面板会将整个页面状态加载到内存中,然后一旦页面再次呈现,将在控件中显示修改后的部分。 This allows the state driven architecture to not convey to the client that the server is reserving the content.这允许状态驱动架构不向客户端传达服务器正在保留内容。

<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" />

Currently, every time the page initially loads you have the default state.目前,每次初始加载页面时,您都拥有默认状态。 But within an update panel we have content, but now when we trigger our button:但是在更新面板中我们有内容,但是现在当我们触发按钮时:

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

You will notice the screen flicker has vanished, because the control within the UpdatePanel is the only portion of the page being rendered after the server event.您会注意到屏幕闪烁已经消失,因为UpdatePanel的控件是在服务器事件之后呈现的页面的唯一部分。 The other approach, would be to use the Ajax approach and handler.另一种方法是使用 Ajax 方法和处理程序。

Hopefully this provides enough information to help you.希望这提供了足够的信息来帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM