[英]How to detect a postback in frontend (aspx)
我需要檢測前端中的回發,以便我可以使用它與JQuery來更改頁面加載的類。 我怎樣才能做到這一點?
您可以檢查IsPostBack屬性。 例如:
<script type="text/javascript">
$(function()
{
var isPostBack = <%=Page.IsPostBack.ToString().ToLower()%>;
if (isPostBack)
{
alert("Postback");
}
});
</script>
在服務器端有這個
if(IsPostBack)
{
// NOTE: the following uses an overload of RegisterClientScriptBlock()
// that will surround our string with the needed script tags
ClientScript.RegisterClientScriptBlock(GetType(), "IsPostBack", "var isPostBack = true;", true);
}
在客戶端這個
if(isPostBack) {
// do your thing
}
我把這個變量放在我的asp.net web表單頁面的header標簽里面。
<script type="text/javascript">
var isPostBack = ("true"==="<%= Page.IsPostBack ? "true" : "false" %>");
</script>
var包含一個布爾值。 比較可能會縮短。
簡單:
如果你正在使用jquery它必須追求(jquery除此之外):
$(document).ready(function(){
});
var isPostBack = <%=Convert.ToString(Page.IsPostBack).ToLower()%>;
然后
function whatever(){
if (isPostBack){
//Whatever you want to do
}else{
//Whatever else you want to do
}
}
我實際上使用它與jquery顯示一個Web服務狀態框然后強制回發刷新ListView,所以當它回發它不會調用Web服務或顯示狀態框只是更新的ListView數據。
$("a[href^='javascript:__doPostBack']").click(function () {
// do something
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.