473,394 Members | 1,800 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,394 software developers and data experts.

Javascript loop calling code behind

2
hi, i have found a solution to calling code behind function but it does not work if i put the call statement in a loop

Javascript
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. function init()
  3. {
  4. alert('hello ');
  5. <%SetData();%>; //------------------------------> main line
  6. alert('<%=strName%>');
  7. }
  8. </script>
C#
Expand|Select|Wrap|Line Numbers
  1. private void Page_Load(object sender, System.EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. this.ImageButton1.Attributes.Add("onclick",
  6. "javascript:init();");
  7. }
  8. }
  9. public void SetData()
  10. {
  11. strName = "mainpico";
  12. }
if i put the <%SetData();%>; in a loop it only executes once.....please help
Sep 29 '11 #1
3 3152
Rabbit
12,516 Expert Mod 8TB
Server side code can only run on the server. Javascript, a client side language, can not call a server side function. What you think of javascript calling the server side function is nothing of the sort. It's not being called by the javascript function, it's being evaluated at the server and then sending the page along to the client. If you look at the source code from the resulting page in a web browser, that server side code won't be there.
Sep 29 '11 #2
kenan
2
thanks for the reply rabbit, Im not so clued up with this...still studying, is there anyway 2 call a c# code behind function from javascript...
Sep 29 '11 #3
Rabbit
12,516 Expert Mod 8TB
You can use an XMLHTTPRequest to call a server side code page. Once the server has processed the code, it will call back to the client with the processed page. You then use the call back function to pull whatever data you need out of the result. The whole process is called AJAX.

A simple example of that would be
Expand|Select|Wrap|Line Numbers
  1. function ajaxExample() {
  2.     var xmlhttp;
  3.   xmlhttp=new XMLHttpRequest();
  4.  
  5.     xmlhttp.onreadystatechange=function() {
  6.         if (xmlhttp.readyState==4 && xmlhttp.status==200) {
  7.             document.getElementById("someID").innerHTML=xmlhttp.responseText;
  8.     }
  9.   }
  10.  
  11.     xmlhttp.open("GET","ajax.asp?q="+str,true);
  12.     xmlhttp.send();
  13. }
Sep 29 '11 #4

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

Similar topics

2
by: Fred Armitage | last post by:
I have a fairly complex application written using a vb code-behind dll. However, I'd like to write one particular aspx page utilising in-line code so that it can easily be modified as needed by the...
8
by: Brett Robichaud | last post by:
I understand how code-behind can handle events for a page, but can I call a code-behind method from within a <script> tag in my ASP.Net page, or can I only call methods defined in other <script>...
6
by: Jim | last post by:
Group, How would I go about firing a JavaScript method from a C# method in the aspx code behind file? It seems as though this could not be done since JavaScript is executed from within the Web...
5
by: Mark | last post by:
Hi all, how do I open a javascript window from a code behind page? I was using Response.Write("<script language='javascript'>window.open('url','name','options');</script>") but now for some...
2
by: mikepolitowski | last post by:
Hi folks, I am have been trying to solve this problem for quite some time now and would appreciate any advice. I have been trying to call a code-behind function that is defined in my aspx.cs...
3
by: UJ | last post by:
I have a JavaScript that fills in a text box with a date/time. I then need to access it my code behind code. If I make the input field runat=server, I can get at it by code behind but JavaScript...
1
by: elavarasum | last post by:
Hi, Can any one help me. i have created one color pallet. if i click particular color, it will set as background-color of CardTable. I want to access the particular set in the CardTable from...
0
by: theOr1ent | last post by:
Hi. I am having a problem with a JavaScript activation when leaving a TextBox control. My default.aspx page has the following control and event; <asp:TextBox ID="txtBirthDate"...
4
by: sajithkahawatta | last post by:
i want to call a java script function from code behind. in page1.aspx page i placed script' <script language="javascript"> function SetSelected() { infoTextBox.select(); } </script>
2
by: Palvids | last post by:
Hi, I want to assign some value to session variable in JavaScript written in c# code behind.I hv written like this: string map2 ="<script language='javascript'>"<%Session=varname;%>"; </script>";...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.