Saturday, March 10, 2012

user controls-update panel-JavaScript

From what you posted, It may be that you are not referencing the control the right way. Does the " UserControlID.Visible =True " part of your code work? If it does, then you may be to change the code to reference the control the right way.


"UserControlID.Visible =True " This code is in the code behind file. It works fine. The problem comes when the user control is visible and if any JavaScript function is called . The user control structure is below

<script language="JavaScript">

function DummyFunction()

{

alert ("Hi");

}

</script>

<DIV>

</DIV>


"UserControlID.Visible =True " This code is in the code behind file. It works fine. The problem comes when the user control is visible and if any JavaScript function is called . The user control structure is below

<script language="JavaScript">

function DummyFunction()

{

alert ("Hi");

}

</script>

<DIV>

</DIV>


Hi,

According to your description, it seems to be caused by that the javascript isn't rendered.

Please try to use ScriptManager.RegisterClientScriptBlock at the preRender stage of the UserControl to add your javascript.

For instance:

protected void Page_PreRender(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "myfun", "function DummyFunction(){alert ('Hi');}", true);
}


Hi there,

I am having the same problem as described by the OP. The solution provided does work, but as it requires the JS to be inserted using server side code it means that it is not very readable/maintainable; as I have quite a lot of JS in my control it is rather important that it is easy to maintain.

Is there any way to force the JS in the .ascx page to get rendered when the control is made visible?

Cheers


Try creating an external .js file for the user control. you might have to pass the control ids to the functions.


Thanks for the suggestion kulp, but I couldn't get this to work. Besides, it's not an ideal solution for me anyway as some of the javascript on my control is event handlers for third party controls, over which I have no control of the parameters that are passed.

Any other suggestions?

No comments:

Post a Comment