call javascript from c#
call javascript from c#
我知道以前有人问过这个问题,我查了一下,发现了这个问题:
Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "test();", true);
所以我把它放在我的c方法中,并在我的脑海中放入下面的javascript代码。
<script type ="text/javascript">
function test() {
alert("succes");
}
</script>
这是我调用代码隐藏方法的html。
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="ampwirecalc">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" RenderMode="Inline">
<ContentTemplate>
<asp:DropDownList ID="dropdownsize" OnSelectedIndexChanged="wirecalc" onchange="runscalc()" AutoPostBack="true" runat="server">
<asp:ListItem Text="Select Wire Size" value="-1"/>
<asp:ListItem Text="1/0 gauge" Value="0" />
<asp:ListItem Text="4 gauge" Value="1" />
<asp:ListItem Text="8 gauge" Value="2" />
</asp:DropDownList>
<script type="text/javascript">
function runscalc()
{
var totalRMS = document.getElementById('<%=tbx_anw3.ClientID%>').value;
document.getElementById('<%=HiddenField1.ClientID%>').value = totalRMS;
}
</script>
<!--These are the texts giving information on the options-->
<div class="textwirecalc">
<p class="selectedwire">Selected Wire:</p>
<p class="neededruns">Needed Runs:</p>
<p class="selectedsize">Selected size:</p>
</div>
<!--These are the labels that wil show the calculated info-->
<div class="labelwirecalc">
<asp:Label ID="wiretype" runat="server" Text="Wire type will show here"></asp:Label>
<asp:Label ID="wireruns" runat="server" Text="Needed runs will show here"></asp:Label>
<asp:Label ID="wiresize" runat="server" Text="Wire size will show here"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
有人知道我做错了什么,我应该做什么吗。
提前谢谢。
试试这个
Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "test();", false);
参考
最后一个参数true/false指示-是否添加脚本标记。
您可以给出如下javascript,无需编写javascript函数
Page.ClientScript.RegisterStartupScript(this.GetType(), "click","alert('succes');", true);
更新
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(),
"click", "alert('succes');", true);
第三个参数必须用脚本标记作为http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx说。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function test() {
alert("succes");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="ampwirecalc">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" RenderMode="Inline">
<ContentTemplate>
<asp:DropDownList ID="dropdownsize" onchange="runscalc()" AutoPostBack="true" runat="server" OnSelectedIndexChanged="dropdownsize_SelectedIndexChanged">
<asp:ListItem Text="Select Wire Size" Value="-1" />
<asp:ListItem Text="1/0 gauge" Value="0" />
<asp:ListItem Text="4 gauge" Value="1" />
<asp:ListItem Text="8 gauge" Value="2" />
</asp:DropDownList>
<script type="text/javascript">
</script>
<!--These are the texts giving information on the options-->
<div class="textwirecalc">
<p class="selectedwire">
Selected Wire:</p>
<p class="neededruns">
Needed Runs:</p>
<p class="selectedsize">
Selected size:</p>
</div>
<!--These are the labels that wil show the calculated info-->
<div class="labelwirecalc">
<asp:Label ID="wiretype" runat="server" Text="Wire type will show here"></asp:Label>
<asp:Label ID="wireruns" runat="server" Text="Needed runs will show here"></asp:Label>
<asp:Label ID="wiresize" runat="server" Text="Wire size will show here"></asp:Label>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
还有后面的代码,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "test();", true);
}
protected void dropdownsize_SelectedIndexChanged(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(),
"click", "test();", true);
}
}
}
试试这个代码。
相关文章:
- Calling VB.Net from Javascript
- AWSELB Cookie access from Javascript
- Routing from Javascript lib in Angular JS 2
- Testing .NET Web API from JavaScript
- ASP.net Identity send AntiForgeryToken from javascript
- URI escape / unescape from Javascript to Perl
- Calling Java Web API from Javascript
- ASMX Web Service from Javascript
- Accessing Amazon RDS from javascript
- writing pdf from javascript/cordova
- Read an MVC @Html.TextBoxFor from Javascript
- SelectOneMenu disable from javascript
- innerHTML from Javascript Array
- Calling Swift from JavaScript
- javascript getElementsByClassName from javascript variable
- Accessing TaublarData from javascript
- Catch the window.open from javascript
- Call python from javascript
- Calling Servlet from JavaScript
- Calling Asp Validator from Javascript