This is javaScript based ASP.net web application.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script language="javascript" type="text/javascript">
function oddEve(){
  var rand = Math.random();
  rand = Math.ceil(6*rand);
  if(rand % 2 == 0){
  document.write("Odd number: ");
  }
  else{
  document.write("Even number: ");
  }
  document.write(rand);
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>JavaScript1</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       [B][U]<asp:[/U][/B]
    </div>
    </form>
</body>
</html>

Please tell me how to make function call of 'oddEve()' at the marked place ???
thnx

Dani AI

Generated

Short answer: call the function on page load (client-side) or ask the server to emit a call. already has the function; to make it run automatically there are a few simple, safe options.

Put a tiny call in the same script block (runs as the browser parses scripts), or attach the function to the DOM ready event so it runs after the page is built:

<script type="text/javascript">
function oddEve() { /* your existing logic here */ }

/* run immediately while parsing */
oddEve();

/* or run when the document is ready (safer if you write into the DOM) */
document.addEventListener('DOMContentLoaded', oddEve);
</script>

Avoid using document.write after the page has loaded — it can erase the page. Instead add a placeholder in your HTML and write into it from the function:

<span id="oddOutput"></span>

<script>
/* inside oddEve build a string `out` and then: */
document.getElementById('oddOutput').textContent = out;
</script>

If you need the server to trigger the client-side call (for example after a postback), ’s suggestion is correct: emit a startup script from code-behind. For AJAX/UpdatePanel scenarios use ScriptManager.RegisterStartupScript. Also remember that ASP.NET can change control IDs; use ClientID or ClientIDMode="Static" (ASP.NET 4+) when you target elements by ID from JavaScript.

Pick the client-side onload approach for the simplest result (no buttons required). Use the server-side script registration only when you must trigger the call from server code.

Recommended Answers

All 8 Replies

assume you have a control named "TextBox1" in aspx file, and its value is "2"
also, you have a javascript function called "ShowValue" which will alert the value of TextBox1.
so, your aspx file like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>Untitled Page</title>
    <script>
        ShowValue = function()
        {
            alert(document.getElementById("TextBox1").value);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Text="2"></asp:TextBox>        
    </div>
    </form>
</body>
</html>

now, in server-side, at Page_Load method, you set value of "TextBox1" to "9" and call "ShowValue" from server.

TextBox1.Text = "9";
Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "ShowValue()", true);

that is.

The RegisterClientScriptBlock method adds a script block to the top of the rendered page.

while, the script block added by the RegisterStartupScript method executes when the page finishes loading but before the page's OnLoad event is raised.
(it means the RegisterStartupScript method adds a script block to the end of the rendered page.)

This the same without function .....
It justs prints the value of rnd on the web page .....
I want the same output when use this in a function .... !:O

<!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>
<title>JavaScript</title>
<script language="JavaScript" type="text/javascript">
var rand = Math.random( );
rand = Math.ceil(6 * rand);
if (rand % 2 == 1) {
document.write("Odd number: ");
} else {
document.write("Even number: ");
}
document.write(rand);
</script>
</head>
<body>
</body>
</html>

Please help

Not getting your exact problem......

If run the above asp.net app .....
That will just print
"Odd Number: 5"
<in my case>
But does not have any function call in it ..... like

function oddEve(){
  var rand = Math.random();
  rand = Math.ceil(6*rand);
  if(rand % 2 == 0){
  document.write("Odd number: ");
  }
  else{
  document.write("Even number: ");
  }
  document.write(rand);
}

This is the same thing in form of a function .....
Now how can I call this ?
I want simple text to be printed .... no buttons or text boxes !!!

You want this function should be called...
and it should print "Odd Number: 5" on web page...
am i right

Exactly .... !

: Thats the same what i wanted

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.