[英]Code Behind calling a JavaScript function on a button click - C#
我有一個Js函數調用bootstrap模式彈出窗口。 我在按鈕點擊后從代碼后面調用這個Js函數。 問題是,當我點擊按鈕時,它顯示模態彈出窗口,但是當我導航到另一個頁面時(當我點擊另一個按鈕時,它導航到谷歌文檔閱讀器)並返回到主頁,它再次出現沒有任何按鈕點擊加載彈出窗口。 誰能幫我這個。
<head>
<script type="text/javascript">
function showModal() {
$('#viewOc').modal();
};
</script>
</head>
<div class="modal fade" id="viewOc" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4>TRIAL</h4>
</div>
<div class="modal-body">
LOREM IPSUM
</div>
<div class="modal-footer">
<a class="btn btn-primary" data-dismiss="modal">
Close
</a>
</div>
</div>
</div>
</div>
protected void LinkButton1_Click(object sender, CommandEventArgs e)
{
//some operations here
string script = "window.onload = function() { showModal(); };";
ClientScript.RegisterStartupScript(this.GetType(), "modalShow", script, true);
}
<asp:LinkButton ID="LinkButton1" runat="server" Text="Click Here" CausesValidation="false" commandArgument='<%#Eval("value")%>' OnCommand="LinkButton1_Click"/>
protected void btnView_View(object sender, CommandEventArgs e)
{
//not sure if the below code is correct or wrong. But as of now im using this in my application. Didnt try it on server yet.
string path = e.CommandArgument.ToString();
Response.Redirect("http://docs.google.com/viewer?url=" + Server.UrlEncode(path),true);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
IndexDocuments();
}
}
PS:我知道類似的問題,但在SO中提出了不同的問題。 由於我沒有足夠的聲譽來評論這個問題,我不得不發一個新帖子。
你應該點擊按鈕調用模型
$('buttonID').click(function(){ $('modalID').modal('show'); });
你正在調用window.onload上的模態,這就是為什么它在頁面加載時彈出的原因。
Vishal在下面提到了一種更好的處理方法。
$('buttonID')。click(function(){$('modalID')。modal('show');});
您可以使用帶有上述代碼的普通HTML按鈕,而不是使用ASP按鈕。
謝謝,Souvik
每次加載窗口時都綁定showModal()。 像這樣更改代碼隱藏:
protected void LinkButton1_Click(object sender, CommandEventArgs e)
{
//some operations here
ScriptManager.RegisterStartupScript(this, this.GetType(), "modalShow", "showModal()", true);
}
添加js和css引用
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"> </script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript">
function showModal() {
$('#viewOc').modal();
};
</script>
</head>
使用此代碼:
protected void LinkButton1_Click(object sender, CommandEventArgs e)
{
//some operations here
if(!ClientScript.IsStartupScriptRegistered("JSScript"))
{
ClientScript.RegisterStartupScript(this.GetType(),"JSScript",
@"<script type='text/javascript'>showModal()</script>");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.