我有一個js函數,它從後面的代碼中調用(在按鈕單擊事件上),它打開一個模式窗口。按鈕點擊事件或調用模態窗口的js函數沒有問題。但主要問題來了,如果我刷新頁面。無論何時刷新頁面,都會調用js函數,而無需單擊任何按鈕並加載模態窗口。如何在頁面刷新時避免js函數調用
如何避免頁面刷新時出現此模式窗口加載?
這是我的代碼。
protected void btnTextView_View(object sender, CommandEventArgs e)
{
//some processing here
if (!ClientScript.IsStartupScriptRegistered("JSScript") && !Page.IsPostBack)
{
ClientScript.RegisterStartupScript(this.GetType(), "JSScript",
@"<script type='text/javascript'>showModal()</script>");
}
}
編輯:
按您的意見,我的理解是
<head>
<script type="text/javascript">
function showModal() {
$('#viewOc').modal();
return false;
};
</script>
</head>
<body>
<asp:button class="btn btn-default" id="btnTextView" runat="server" Text="View as Text" commandArgument="1234" OnCommand="btnTextView_View"> </asp:button>
<div class="modal fade" id="viewOc" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
//some content here
</div>
</div>
</div>
</body>
protected void btnTextView_View(object sender, CommandEventArgs e)
{
//some processing here
if (!ClientScript.IsStartupScriptRegistered("JSScript"))
{
ClientScript.RegisterStartupScript(this.GetType(), "JSScript",
@"<script type='text/javascript'>showModal()</script>");
}
}
檢查此:http://stackoverflow.com/questions/1135404/btnadd-click-fires-when-i-press-f5 @ASN – NEER