gpt4 book ai didi

c# - 避免在 asp.net 中的按钮单击事件期间刷新页面

转载 作者:行者123 更新时间:2023-11-30 22:46:34 26 4
gpt4 key购买 nike

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"];
}

}

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

最佳答案

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

参见 this page关于 updatepanel 控件的工作原理。

关于c# - 避免在 asp.net 中的按钮单击事件期间刷新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2590661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com