0

I know this question is asked before, and I looked it up, and found this sulotion:

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

so I put that in my c# method, and the following javascript code in my head.

<script type ="text/javascript">
    function test() {
        alert("succes");
    }
</script>

this is my html where i call the code behind method.

<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>

does anyone know what I'm doing wrong, and what I should do instead.

thanks in advance.

8
  • 1
    Could you tell us what's going wrong first? Commented Jun 3, 2013 at 11:33
  • What is the issue here? Function test() is not being called? Commented Jun 3, 2013 at 11:39
  • yes, the function doesnt get called Commented Jun 3, 2013 at 11:41
  • Can you please post the HTML along with the script? Commented Jun 3, 2013 at 11:42
  • I doubt, Jscript in your browser is disabled? Commented Jun 3, 2013 at 11:45

4 Answers 4

2

Try this

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

Reference

last parameter true/false indicate- whether to add script tags.

Sign up to request clarification or add additional context in comments.

1 Comment

doesn't work either, or do i need to add something else in my c# or html, trying to figure this out for a few days and i'm completely lost..
2

you can give the javascript like below, no need to write javascript function

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

UPDATE

ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(),
             "click", "alert('succes');", true);

4 Comments

@TheUnknown - I doubt, Jscript in your browser is disabled?
i get the following error: UpdatePanel1 is a field but it's used like a type.
changed it to UpdatePanel and that fixed it but still won't call the function
doesn't work either, i get the feeling something is off on my updatepanel. when i put a breakpoint on my method it goes in there but nothing changes in on client side..
0

The third parameter must be enclosed with script tags as the http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx says.

Comments

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>

And code behind,

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

Try this code.

4 Comments

it does work when i put it in page_load, but i want to call it when the method is called, in the end of my method protected void wirecalc(object sender, EventArgs e) { //some other code before calling javascript Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "test();", true); } Then it doesnt work..
@TheUnknown - I have updated my answer. Try that code please.
i figured it out guys, just had a debug session with my software engeneering teacher and the call function codes where all right. i had something on pageload that messed things up.
@TheUnknown - Good. Please accept as answer/upvote for the answers that helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.