943,712 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 15225
  • ASP.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 2nd, 2008
0

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

Expand 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sancti is offline Offline
27 posts
since Aug 2008
Dec 3rd, 2008
0

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

Click to Expand / Collapse  Quote originally posted by sancti ...
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:

asp.net Syntax (Toggle Plain Text)
  1. Page.ClientScript.RegisterStartupScript(this.GetType(), "Test", "YourFunctionName();",true);

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

Or the other easy option would be
asp.net Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 6
Light Poster
vizy is offline Offline
36 posts
since Dec 2007
Dec 3rd, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
sancti is offline Offline
27 posts
since Aug 2008
Dec 3rd, 2008
0

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

ok...
try using
HttpContext.Current.Response.Write("<script>YourFunctionName(); <\script>")
Reputation Points: 10
Solved Threads: 6
Light Poster
vizy is offline Offline
36 posts
since Dec 2007
Dec 3rd, 2008
0

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

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
Reputation Points: 10
Solved Threads: 0
Light Poster
sancti is offline Offline
27 posts
since Aug 2008
Dec 3rd, 2008
0

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

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 ?
Reputation Points: 10
Solved Threads: 6
Light Poster
vizy is offline Offline
36 posts
since Dec 2007
Dec 3rd, 2008
0

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

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

JavaScript Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
sancti is offline Offline
27 posts
since Aug 2008
Dec 3rd, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 6
Light Poster
vizy is offline Offline
36 posts
since Dec 2007
Dec 3rd, 2008
0

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

hi vizy,
Yes, i am passing the parameter as above.. But the javascript function is not calling itself.. Can u please help me...
thanks
Reputation Points: 10
Solved Threads: 0
Light Poster
sancti is offline Offline
27 posts
since Aug 2008
Dec 3rd, 2008
0

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

ok..
try by putting alert("hello")
inside the function

and when are you calling the function, under which event ?
Reputation Points: 10
Solved Threads: 6
Light Poster
vizy is offline Offline
36 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Object reference not set to an instance of an object.
Next Thread in ASP.NET Forum Timeline: Asp net url hit counter?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC