【问题标题】:to avoid page refresh during button click event in asp.net避免在 asp.net 中的按钮单击事件期间刷新页面
【发布时间】:2010-04-07 07:25:57
【问题描述】:
public partial class _Default : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)  // If page loads for first time
    {
        Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());   // Assign the Session["update"] with unique value

        //=============== Page load code =========================





        //============== End of Page load code ===================
    }

}



protected void Button1_Click(object sender, EventArgs e)
{
    if (Session["update"].ToString() == ViewState["update"].ToString())    // If page not Refreshed
    {
        //=============== On click event code =========================

        Label1.Text = TextBox1.Text;
        //lblDisplayAddedName.Text = txtName.Text;


        //=============== End of On click event code ==================

        // After the event/ method, again update the session  
        Session["update"] = Server.UrlEncode(System.DateTime.Now.ToString());
    }
    else  // If Page Refreshed
    {
        // Do nothing 
    }
}
protected override void OnPreRender(EventArgs e)
{
    ViewState["update"] = Session["update"];
}

}

这不适用于高分辨率渐变背景。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    考虑将按钮和标签包装在updatepanel 控件中,该控件使用 AJAX 刷新其内容。 页面的其余部分不会重新加载,并且该操作不会影响浏览器导航。

    请参阅 this page,了解 updatepanel 控件的工作原理。

    【讨论】:

      【解决方案2】:

      由于您在服务器端处理按钮单击事件,因此必须有一个回发来处理它。

      如果您不希望回帖发生,请将事件处理更改为“客户端点击”

      【讨论】:

        【解决方案3】:

        //Heinzi 代码对我有用,只是在 OnPreRender 事件中做了一个小改动,当它没有回发时分配 ViewsState 值

        protected override void OnPreRender(EventArgs e)
            {
                if (!IsPostBack)
                {
                    ViewState["update"] = Session["update"];
        
                }
        
            }
        

        【讨论】:

          猜你喜欢
          • 2017-05-17
          • 2014-02-15
          • 2021-09-12
          • 2011-01-11
          • 1970-01-01
          • 2023-03-13
          • 2021-06-30
          • 2013-10-07
          • 1970-01-01
          相关资源
          最近更新 更多