简体   繁体   English

在Asp.NET中使用javascript

[英]Using javascript in Asp.NET

I have a problem that I want to call the javascript code, when selection of gridview is changing. 我有一个问题,我想调用javascript代码,当gridview的选择正在改变。 But, I can not start my javascript. 但是,我无法启动我的JavaScript。 How can I do that? 我怎样才能做到这一点?

//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)
{

}

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>");
    }

使用runat服务器放置一个文字,并在stringbuilder中使用javascript服务器端填充此文字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM