how to call a function from .aspx.cs file to .aspx file

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 27
Reputation: sancti is an unknown quantity at this point 
Solved Threads: 0
sancti sancti is offline Offline
Light Poster

how to call a function from .aspx.cs file to .aspx file

 
0
  #1
Dec 2nd, 2008
hi all,
I am developing a web application. I have written one function in .aspx file. Now i want to call that function from the .aspx.cs file. How can I do this? Can anybody help me regarding this?

thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 34
Reputation: vizy is an unknown quantity at this point 
Solved Threads: 6
vizy's Avatar
vizy vizy is offline Offline
Light Poster

Re: how to call a function from .aspx.cs file to .aspx file

 
0
  #2
Dec 3rd, 2008
Originally Posted by sancti View Post
hi all,
I am developing a web application. I have written one function in .aspx file. Now i want to call that function from the .aspx.cs file. How can I do this? Can anybody help me regarding this?

thanks in advance
You can call that function using Page.ClientScript method
something like:

  1. Page.ClientScript.RegisterStartupScript(this.GetType(), "Test", "YourFunctionName();",true);

Or you can also attach a JavaScript event to a Server Control like:
  1. Button1.Attributes.Add("onclick", "YourFunctionName();");

Or the other easy option would be
  1. Response.write("<script>YourFunctionName(); <\script>")
Last edited by peter_budo; Dec 3rd, 2008 at 7:06 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Njoy koding... >>>
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 27
Reputation: sancti is an unknown quantity at this point 
Solved Threads: 0
sancti sancti is offline Offline
Light Poster

Re: how to call a function from .aspx.cs file to .aspx file

 
0
  #3
Dec 3rd, 2008
hi vizy,
When i used Page.ClientScript the function which is in .aspx file is not calling. When i used Response.Write method in .aspx.cs file, I am getting error.
error: Response is not available in this context.
Please help me how can I solve this
thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 34
Reputation: vizy is an unknown quantity at this point 
Solved Threads: 6
vizy's Avatar
vizy vizy is offline Offline
Light Poster

Re: how to call a function from .aspx.cs file to .aspx file

 
0
  #4
Dec 3rd, 2008
ok...
try using
HttpContext.Current.Response.Write("<script>YourFunctionName(); <\script>")
Njoy koding... >>>
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 27
Reputation: sancti is an unknown quantity at this point 
Solved Threads: 0
sancti sancti is offline Offline
Light Poster

Re: how to call a function from .aspx.cs file to .aspx file

 
0
  #5
Dec 3rd, 2008
hi vizy,
i tried ur method.. But it is showing error with the function name.. Can u please help me to solve this in a better way since i am new to asp.net
thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 34
Reputation: vizy is an unknown quantity at this point 
Solved Threads: 6
vizy's Avatar
vizy vizy is offline Offline
Light Poster

Re: how to call a function from .aspx.cs file to .aspx file

 
0
  #6
Dec 3rd, 2008
ok...
is previous error is solved ?
can u show me the code where you calling this function from .cs page ?
and what is error getting calling the function ?
is it the JavaScript error ?
Njoy koding... >>>
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 27
Reputation: sancti is an unknown quantity at this point 
Solved Threads: 0
sancti sancti is offline Offline
Light Poster

Re: how to call a function from .aspx.cs file to .aspx file

 
0
  #7
Dec 3rd, 2008
hi vizy,
The error i am getting is due to the javascript error.. It is showing the error function name javascript error.. object is expected..
I want to pass the parameter inside the function.
I have written function as follows in .aspx file

  1. <script type="text/javascript">
  2. function func(DropDownList1.SelectedIndex, startDate, endDate)
  3.  
  4. {
  5.  
  6. ....
  7.  
  8. }
  9.  
  10. </script>
Last edited by peter_budo; Dec 3rd, 2008 at 7:07 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 34
Reputation: vizy is an unknown quantity at this point 
Solved Threads: 6
vizy's Avatar
vizy vizy is offline Offline
Light Poster

Re: how to call a function from .aspx.cs file to .aspx file

 
0
  #8
Dec 3rd, 2008
Hi Sancti

Are you passing the parameter values to the function
As follows...
Response.write("<script>func('"+DropDownList1.SelectedIndex+"','"+ startDate+"','"+ endDate+"')</script>");
Last edited by peter_budo; Dec 3rd, 2008 at 7:07 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Njoy koding... >>>
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 27
Reputation: sancti is an unknown quantity at this point 
Solved Threads: 0
sancti sancti is offline Offline
Light Poster

Re: how to call a function from .aspx.cs file to .aspx file

 
0
  #9
Dec 3rd, 2008
hi vizy,
Yes, i am passing the parameter as above.. But the javascript function is not calling itself.. Can u please help me...
thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 34
Reputation: vizy is an unknown quantity at this point 
Solved Threads: 6
vizy's Avatar
vizy vizy is offline Offline
Light Poster

Re: how to call a function from .aspx.cs file to .aspx file

 
0
  #10
Dec 3rd, 2008
ok..
try by putting alert("hello")
inside the function

and when are you calling the function, under which event ?
Njoy koding... >>>
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC