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

Recommended Answers

All 13 Replies

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>")

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.

ok...
try using
HttpContext.Current.Response.Write("<script>YourFunctionName(); <\script>")

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

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 ?

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>

Hi Sancti

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

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

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

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

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

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

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


<aspcriptManager ID="ScriptManager" runat="server" />

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

<asp:Button runat="server" ID="ButtonAddDock" Text="Add Dock" OnClick="ButtonAddDock_Click" />

<asp:Button runat="server" ID="ButtonPostback" Text="Make Postback" />

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.