954,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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

sancti
Light Poster
27 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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:

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


Or you can also attach a JavaScript event to a Server Control like:

Button1.Attributes.Add("onclick", "YourFunctionName();");


Or the other easy option would be

Response.write("<script>YourFunctionName(); <\script>")
vizy
Light Poster
36 posts since Dec 2007
Reputation Points: 10
Solved Threads: 6
 

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.

sancti
Light Poster
27 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

ok...
try using
HttpContext.Current.Response.Write("

vizy
Light Poster
36 posts since Dec 2007
Reputation Points: 10
Solved Threads: 6
 

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

sancti
Light Poster
27 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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 ?

vizy
Light Poster
36 posts since Dec 2007
Reputation Points: 10
Solved Threads: 6
 

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

<script type="text/javascript"> 
function func(DropDownList1.SelectedIndex, startDate, endDate)

{

....

}

</script>
sancti
Light Poster
27 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Hi Sancti

Are you passing the parameter values to the function
As follows...
Response.write("<script>func('"+DropDownList1.SelectedIndex+"','"+ startDate+"','"+ endDate+"')</script>");

vizy
Light Poster
36 posts since Dec 2007
Reputation Points: 10
Solved Threads: 6
 

hi vizy,
Yes, i am passing the parameter as above.. But the javascript function is not calling itself.. Can u please help me...
thanks

sancti
Light Poster
27 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

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

and when are you calling the function, under which event ?

vizy
Light Poster
36 posts since Dec 2007
Reputation Points: 10
Solved Threads: 6
 
sierrainfo
Junior Poster
144 posts since May 2008
Reputation Points: 10
Solved Threads: 9
 

hi vizy,
i am not calling the function with the event. Just i am calling the function with a subfunction. I wrote that response.write line in subfunction...

thanks

sancti
Light Poster
27 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

hi vizy,
i am pasting that response line in a sub function.
thanks

sancti
Light Poster
27 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Please try this code for call function of aspx.cs page on .aspx page by javascript

<%--var str = "<%= dock_Command("+ dock + "," + args + ") %>"--%>

guptanitesh1988
Newbie Poster
3 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You