0

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

3
Contributors
8
Replies
15
Views
6 Years
Discussion Span
Last Post by AnujaCool
0

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

0

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

0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.