在 Asp.NET 中使用 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19751822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using javascript in Asp.NET
提问by Isaac04
I have a problem that I want to call the javascript code, when selection of gridview is changing. But, I can not start my javascript. How can I do that?
我有一个问题,当 gridview 的选择发生变化时,我想调用 javascript 代码。但是,我无法启动我的 javascript。我怎样才能做到这一点?
//Html side
//HTML端
<input ID="addressinput" type="text" runat="server" style="display:none;"/>
<asp:Button ID="Button1" runat="server" Text="Button" style="display:none;" OnClientClick="return myfunction();" onclick="Button1_Click" />
//Javascript
//Javascript
function myfunction() {
FindLocaiton();
}
//C#
//C#
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
addressinput.Value = GridView1.SelectedRow.Cells[1].Text;
Button1.Click += new EventHandler(Button1_Click);
}
protected void Button1_Click(object sender, EventArgs e)
{
}
回答by Kuldeep
As per following code java script function myfunction() will execute first then the server side button click event.... so u need to add following code in button click event to execute javascript code after server side code..
根据以下代码,java 脚本函数 myfunction() 将首先执行,然后是服务器端按钮单击事件......所以你需要在按钮单击事件中添加以下代码以在服务器端代码之后执行 javascript 代码..
ClientScript.RegisterStartupScript(this.GetType(), "msg", "myfunction();")
ClientScript.RegisterStartupScript(this.GetType(), "msg", "myfunction();")
Following is the code that i have tested and its working.
以下是我测试过的代码及其工作。
ASPX Code.
ASPX 代码。
<script type="text/javascript">
function myfunction() {
alert('In JavaScript Code after server side code execution');
}
</script>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
C# Code :-
C# 代码:-
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("In Button Click event");
ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script language=javascript>myfunction();</script>");
}
回答by Stefan van de Laarschot
Place a literal with runat server and fill this literal with the javascript serverside within a stringbuilder
使用 runat server 放置一个文字,并在 stringbuilder 中使用 javascript serverside 填充这个文字