[英]How to avoid page refresh after button click event in asp.net
这是以下代码,一旦 btninsert 单击事件完成,页面就会刷新 我想在单击 btninsert 后停止页面刷新
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div style="margin-bottom: 20px; margin-top: 20px;"><span><strong style="font-size: large;">Edit User</strong></span></div>
<div>
<span style="float: left; width: 50%;"> </span> <span style="float: left; width: 50%;">
<span style="width: 100%; float: left; text-align: right;">
<asp:Label ID="lblMessage" runat="server" Text="-"></asp:Label></span>
</span>
</div>
<div style="width: 100%; float: left;">
<hr />
</div>
<div style="width: 816px; margin-left: 5px; margin-top: 20px; height: 463px;">
<div style="width: 100%; float: left; padding-top: 15px; height: 257px; margin-left: 0px;">
<span class="Divide">
<span class="simDivide1">FullName</span>
<span class="simDivide">
<asp:TextBox ID="txtfullname" runat="server" Width="180px">
</asp:TextBox>
</span>
</span>
<span class="Divide">
<span class="simDivide1"></span>
<span class="simDivide"></span>
</span>
<span class="Divide">
<span class="simDivide1">Username</span>
<span class="simDivide">
<asp:TextBox ID="txtusername" runat="server" Width="180px">
</asp:TextBox>
</span>
</span>
<span class="Divide">
<span class="simDivide1"></span>
<span class="simDivide"></span>
</span>
<span class="Divide">
<span class="simDivide1">Password</span>
<span class="simDivide">
<asp:TextBox ID="txtpassword" runat="server" Width="180px">
</asp:TextBox>
</span>
</span>
<span class="Divide">
<span class="simDivide1"></span>
<span class="simDivide"></span>
</span>
<span class="Divide">
<span class="simDivide1">Mobile No
</span>
<span class="simDivide"><asp:TextBox ID="txtmobileno" runat="server" Width="180px">
</asp:TextBox>
</span>
</span>
<span class="Divide">
<span class="simDivide"></span>
</span>
<span class="Divide">
<span class="simDivide1">Role
</span>
<span class="simDivide"><asp:TextBox ID="txtrole" runat="server" Width="180px">
</asp:TextBox>
</span>
</span>
<script src="jquery-2.0.2.js"></script>
<script language="javascript">
function done() {
var list = document.getElementById("tid");
list.removeChild(list.lastChild);
}
function changecourse(e) {
var change = document.getElementById('mytext').value;
var i = 1;
mNewObj = document.createElement('div');
mNewObj.id = "BOX" + i;
mNewObj.style.visibility = "show";
mNewObj.innerHTML = change + " <a href='#' style='text-decoration: none; color:red' onClick='done()'> x </a> ";
document.getElementById("tid").appendChild(mNewObj);
i++
var a = document.getElementById('mytext').selectedIndex;
document.getElementById("ContentPlaceHolder1_Hidden1").value = a;
//document.getElementById("ContentPlaceHolder1_btninsert").click();
deleted();
}
function yes() {
$("#ContentPlaceHolder1_btninsert").click();
}
//function insert() {
// $.ajax({
// type: "POST",
// url: "Edituser.aspx.cs/insert",
// success: function () { alert('success'); },
// error: function () { alert('error'); }
// });
//}
function cancel() {
var select = document.getElementById('mytext');
select.remove(select.selectedIndex);
}
function deleted() {
document.getElementById("mytext").style.display = 'none';
document.getElementById("Button1").style.display = 'none';
document.getElementById("tid").style.display = 'inline';
document.getElementById("mylink").style.display = 'inline';
}
function showdiv() {
document.getElementById("mylink").style.display = 'none';
document.getElementById("mytext").style.display = 'inline';
document.getElementById("Button1").style.display = 'inline';
}
</script>
<input id="Hidden1" type="hidden" runat="server" />
</div>
<asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Style="margin-left: 5px" Text="Edit" Width="39px" />
<br>
<br>
<asp:UpdatePanel runat="server">
<ContentTemplate>
    <div id="tid" >
</div>
<div id="di">
<a id="mylink" onclick="showdiv()">Add Depot</a>
<select id='mytext' name='mytext' style="display: none">
<option>--Select--</option>
<option>Mumbai</option>
<option>Delhi</option>
<option>Banglore</option>
<option>Ahmedabad</option>
</select>
<input type="button" id="Button1" style="display: none" onclick=" changecourse(); yes(); cancel(); return false;" value="add" />
</div>
<asp:Button ID="btninsert" runat="server" Style="display: none" OnClick="btninsert_Click" Text="Insert" ValidationGroup="C" />
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</asp:UpdatePanel>
这是我有方法的 Edit.aspx.cs
protected void btninsert_Click(object sender, EventArgs e)
{
string a = Hidden1.Value;
string UserId = Convert.ToString(Session["LoginId"]);
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO UserDepot (UserId,DepotId)" +
"VALUES ('" + UserId + "','" + a + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}
添加OnClientClick="return false;"
,
<asp:button ID="btninsert" runat="server" text="Button" OnClientClick="return false;" />
或在代码隐藏中:
btninsert.Attributes.Add("onclick", "return false;");
当您使用asp:Button
这是一个服务器控件时,回发将发生以避免转到html
按钮,
asp:Button .... />
输入到
input type="button" .... />
如果您的 Page_Load 方法中有一些代码并且您不希望这些代码在单击按钮后执行,请在 Page_Load 上使用 if(!IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// put codes here
}
}
asp:Button 是一个服务器控件,用于向服务器发送请求并获得响应需要页面刷新。 您可以使用 JQuery 和 Ajax 来防止整页刷新
访问服务器时页面会刷新,并且像Button这样的服务器控件默认有一个属性AutoPostback = true ,这意味着无论何时单击它们都会进行服务器访问。 为插入按钮设置AutoPostback = false ,这将为您解决问题。
在页面声明中设置MaintainScrollPositionOnPostBack="true"
:
<%@ Page Language="C#" MaintainScrollPositionOnPostBack="true" Title="Home" %>
我想我也有这个问题,我试图在单击按钮后使日历可见,但单击按钮后页面不断刷新
MaintainScrollPositionOnPostBack="true"
这实际上回答了我的问题。
我不能投票或评论,我今天刚加入
您可以使用为asp.net http://www.ajaxcontroltoolkit.com/制作的“ ASP.net AJax控制工具包”
我在执行实际的 button_click 事件之前执行页面刷新的按钮遇到了同样的问题(因此必须单击该按钮两次)。 此行为发生在 server.transfer-command (server.transfer("testpage.aspx") 之后。
在这种情况下,解决方案是将 server.transfer-command 替换为 response-redirect (response.redirect("testpage.aspx")。
有关这两个命令之间差异的详细信息,请参阅Server.Transfer Vs。 响应.重定向
您可以使用Ajax
阻止页面刷新
html
"input type ="text" id="txtnothing" runat ="server" "
脚本
var txt = document.getElementById("txtnothing");
txt.value = Date.now();
代码
If Not (Session("txtnothing") Is Nothing OrElse String.IsNullOrEmpty(Session("txtnothing"))) Then
If Session("txtnothing") = txtnothing.Value Then
lblMsg.Text = "Please try again not a valid request"
Exit Sub
End If
End If
Session("txtnothing") = txtnothing.Value
按钮单击事件完成您的任何任务后...最后一行复制并通过它...工作正常...Asp.Net中的C#
Page.Header.Controls.Add(new LiteralControl(string.Format(@" <META http-equiv='REFRESH' content=3.1;url={0}> ", Request.Url.AbsoluteUri)));
当必须向下滚动 gridview 以选择一行时, MaintainScrollPositionOnPostBack="true"
将使其在选择该行后继续显示该行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.