0

我知道以前有人问过这个问题,我查了一下,发现了这个问题:

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>

有谁知道我做错了什么,以及我应该做什么。

提前致谢。

4

4 回答 4

2

尝试这个

Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "test();", false);

参考

最后一个参数 true/false 表示 - 是否添加脚本标签。

于 2013-06-03T11:33:18.860 回答
2

你可以像下面这样给出javascript,不需要编写javascript函数

Page.ClientScript.RegisterStartupScript(this.GetType(), "click","alert('succes');", true);

更新

ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(),
             "click", "alert('succes');", true);
于 2013-06-03T11:34:46.910 回答
0
<%@ 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);
        }
    }
}

试试这个代码。

于 2013-06-03T11:47:57.420 回答
0

正如http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx所说,第三个参数必须用脚本标记括起来 。

于 2013-06-03T11:35:45.810 回答