473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to pass values to javascript from code behind

Hi ,

Basically I want to display a popup box only at a specific time in asp.net application. so i'm using the following code..

aspx page :
Expand|Select|Wrap|Line Numbers
  1. <head runat="server">
  2.     <title>Untitled Page</title>
  3.     <script type="text/javascript">
  4. function timedMsg()
  5. {
  6. var t=setTimeout("alert('good morning!')",6000);
  7. }
  8. </script>
  9. </head>
  10.  
C# code :
Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.         StringBuilder script = new StringBuilder();
  4.         script.Append("<script type=\"text/javascript\">");
  5.         script.Append("timedMsg();");
  6.         script.Append("</script>");
  7.         Page.ClientScript.RegisterClientScriptBlock(typeof(object), "JavaScriptBlock", script.ToString());
  8.     }
  9.  
this works fine but i dont want to set settimeout as fixed as 6000. i need to retrieve value from database and assign it to settimeout. so i modified the coding as below..

aspx page :
Expand|Select|Wrap|Line Numbers
  1. <head runat="server">
  2.     <title>Untitled Page</title>
  3.     <script type="text/javascript">
  4. function timedMsg(var x)
  5. {
  6. var t=setTimeout("alert('I am displayed after 3 seconds!')",x);
  7. }
  8. </script>
  9. </head>
  10.  
C# code :
Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.         int x = 6000;
  4.         StringBuilder script = new StringBuilder();
  5.         script.Append("<script type=\"text/javascript\">");
  6.         script.Append("timedMsg("+x+");");
  7.         script.Append("</script>");
  8.         Page.ClientScript.RegisterClientScriptBlock(typeof(object), "JavaScriptBlock", script.ToString());
  9.     }
  10.  
but this code is not working. the alert box is not popping up. is this a correct method to pass values to the script?. or what else to be done to meet the requirement?.. pls help me..

thanks..
Oct 28 '10 #1
1 10137
Frinavale
9,735 Expert Mod 8TB
In your C# code, create a Public or Protected property that can be called by the ASP code to retrieve the the value from the database:

Expand|Select|Wrap|Line Numbers
  1. public string TimeoutValue{
  2.   get{
  3.     //in here return the value from the database
  4.     return "6000"
  5.   }
  6. }
Now you have to change the timedMsg JavaScript function in your ASP code. Add the ASP code that calls the TimeoutValue property and to write it into the page.

Recall, anything between <% %> is executed on the server. So, to write something into the HTML you need to call the Response.Write() method like this:
Expand|Select|Wrap|Line Numbers
  1. <%Response.Write("hello world")%>
.

A short hand for <%Response.Write()%> is <%= %>.

So, we need to change your timedMsg JavaScript method to print the TimeoutValue property into the page using the Response.Write() technique I was just talking about:

Expand|Select|Wrap|Line Numbers
  1. function timedMsg(var x)
  2. {
  3.   var t=setTimeout("alert('I am displayed after 3 seconds!')",<%=TimeoutValue %>);
  4. }
-Frinny
Oct 29 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: George | last post by:
I need to call my visitor tracking script selectively from Code Behind using the following sub, but it gives me an Error on Page. I've removed my account and other information, but the format is...
3
by: Biff | last post by:
Hello, I am bringing up an iFrame with a calendar control in it in coordination with a text box that holds a date field. In my code behind class I add a method call to the text box's OnFocus...
6
by: Paolo Pignatelli | last post by:
I have an aspx code behind page that goes something like this in the HTML view: <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#"mailto:" &amp;...
2
by: shaun duke | last post by:
I have been researching this over the last two days without success. I have a number of ultility functions that I want to make available to all pages. The pages will all be using code behind so...
1
by: Paul D. Fox | last post by:
I'm trying to launch a Child Window from a hyperlink on a Datagrid and have it recieve multiple values from the Parent Window. Upon recieving the values in the Child Window, I need to access them...
5
by: slowmotiongenius | last post by:
All- I have established an adodb recordset in my code-behind, and I need to pass it to the aspx file. I can't seem to figure out if there is a way to do this. I see you can pass a string over...
2
by: gsmjs6 | last post by:
hi every body I'm new to dotnet. Please excuse. I have a doubt in passing values from code-behind page to aspx page of the same web form page.
4
by: IRC | last post by:
hey, i am pretty new on javascript as well as PHP, Hey, anyone can you help me, how to pass the javascript array value to php page......... i want to retrieve the values which are arrayed on...
2
Hutt
by: Hutt | last post by:
Hello, I'm fairly new to vb.net and have a question I can't find an answer to. The site I'm working on is complex, but to keep things simple here's the basic problem. I have a site designed with a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.