在单击按钮时调用JavaScript函数背后的代码-C#

在单击按钮时调用JavaScript函数背后的代码-C#,javascript,c#,html,asp.net,twitter-bootstrap,Javascript,C#,Html,Asp.net,Twitter Bootstrap,我有一个Js函数,它调用引导模式弹出窗口。我通过单击按钮从代码隐藏处调用这个Js函数。问题是,当我点击按钮时,它会显示模式弹出窗口,但当我导航到另一个页面(当我点击另一个按钮时,它会导航到谷歌文档阅读器)并返回主页时,它会再次加载弹出窗口,而不点击任何按钮。有人能帮我吗 JS函数 Html代码 页面加载 注:我知道类似的问题,但在SO中有不同的问题。由于我没有足够的声誉来评论这个问题,我不得不写一篇新的帖子 您应该在单击按钮时调用模型 $('buttonID').click(function()

我有一个Js函数,它调用引导模式弹出窗口。我通过单击按钮从代码隐藏处调用这个Js函数。问题是,当我点击按钮时,它会显示模式弹出窗口,但当我导航到另一个页面(当我点击另一个按钮时,它会导航到谷歌文档阅读器)并返回主页时,它会再次加载弹出窗口,而不点击任何按钮。有人能帮我吗

JS函数 Html代码 页面加载
注:我知道类似的问题,但在SO中有不同的问题。由于我没有足够的声誉来评论这个问题,我不得不写一篇新的帖子

您应该在单击按钮时调用模型

$('buttonID').click(function(){ $('modalID').modal('show'); });

您正在调用window.onload上的模式,这就是为什么页面加载时它会显示弹出窗口。

Vishal在下面提到了一种更好的处理方法

$('buttonID')。单击(函数(){$('modalID')。模态('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>

函数showmodel(){
$('#viewOc').modal();
};
使用此代码:

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>");
    } 
}
受保护的无效链接按钮1\u单击(对象发送者,CommandEventArgs e)
{
//这里有些行动
如果(!ClientScript.isstartupscript已注册(“JSScript”))
{
ClientScript.RegisterStartupScript(this.GetType(),“JSScript”,
@“showModal()”;
} 
}

我必须在代码隐藏或JS函数中更改此选项吗?在JS中进行更改,只需写入$('buttonID')。单击(Function(){$('viewOc')。modal('show');),当你不需要在模式中显示任何动态数据时,JS将完成所有的工作。所以我只需要在JS中做这个更改,保持按钮点击事件不变,并删除调用方法?不。我也试过了。它没有打开任何模式弹出窗口,但我需要从代码中获取一些数据,然后在模式弹出窗口上显示。。因此,我认为我不能使用方法u said
protectedvoidbtnview\u View(objectsender,CommandEventArgs e){string path=e.CommandArgument.ToString();Response.Redirect('http://docs.google.com/viewer?url=“+Server.UrlEncode(path),true);}
我试过这个。但是如果我使用这个代码,它不会显示模式弹出窗口。对不起,我不知道你在使用“ClientScript”。它应该可以与ScriptManager.RegisterStartupScriptI一起使用。我甚至尝试过这个。。它不显示任何模式弹出窗口。。我不知道为什么会这样。只有当我使用window.onload时,它才会显示模式弹出窗口。我用你提供的内容创建了一个简单的页面,它适合我。你能从开发者控制台看到任何错误吗(如果你使用的是Chrome,则为F12)?对我来说,它抛出了一个类似这样的错误。Home.aspx:277未捕获引用错误:showmodel未定义为此。它没有打开任何弹出窗口。您是否将jquery和引导js和css文件放在topyeah。。在把它们放在顶端之后,我尝试了,我面临着这个问题$(…).modal不是一个函数。您是否检查了我的更新答案,包括什么以及它必须包含在哪里。请试试那个,嗨,卡蒂克。在将所有bootstrap.css、jquery和js脚本的位置更改为头之后,我尝试了这两种代码。。一个是你的,另一个是新阮的。。两人的行为方式与我在新阮的评论中提到的相同。。你可以看一下这些评论。
 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();

            }
        }
$('buttonID').click(function(){ $('modalID').modal('show'); });
protected void LinkButton1_Click(object sender, CommandEventArgs e)
        {
            //some operations here
            ScriptManager.RegisterStartupScript(this, this.GetType(), "modalShow", "showModal()", true); 
        }
<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>");
    } 
}