1

I'm adding user control dynamically on my page.

MyPage.aspx:

MyControl cntl = (MyControl)Page.LoadControl("MyControl.ascx");

The MyControl.ascx has a javascript function:

function myfun(a, b)
{
    ....
}

I can pass parameters to myfun from code behind of MyControl.ascx like:

string script2 = String.Format("myfun({0},{1})", param1, param2);
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "initialize control", script2, true);

Is it possible to call this javascript function from MyPage.aspx?

3
  • Check out this question Commented Nov 4, 2012 at 17:02
  • It doesn't work if you transfer your code in script2 inside MyPase.aspx? Commented Nov 4, 2012 at 17:12
  • Could you please clarify your requirement a bit more? As soon as the control is loaded in the page, the javascript function inside the user control is rendered and ready to use from the ASPX page. In .aspx if you try to invoke e.g. myfun(10,12), it will work. Commented Nov 4, 2012 at 20:05

1 Answer 1

0

Yes you can call javascript dunction from mypage.aspx e.g. with a button

<asp:Button id="yourid" runat="server" OnClientClick="yourJavaScriptFunction("+a+","+b+")" />

Or anywhere in mypage.aspx.cs file

Response.Write("<script>yourJavaScriptFunction("+a+","+b+")</script>");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.