Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Wednesday, March 28, 2012

dynamic generated clientside javascript

hi,

how can i register a script on asyncPostBack and can check in the next asynPostback if the script is on side ?

i use ajax with scriptmanager and updatepanel on my website.

sometimes i will give the user (after a asyncPostBack) a alert-message about javascript...

i have tryed following:

PrivateSub RegisterMsg(ByVal strMessageAsString,ByVal strScriptTagAsString)

Dim strScriptAsString ="alert(""" & strMessage &""");"

If (Not Page.ClientScript.IsClientScriptBlockRegistered("key"))Then

ScriptManager.RegisterClientScriptBlock(Me,Me.GetType(),"key", strScript,True)

EndIf

EndSub

the IsClientScriptBlockRegistered-methode every time gives false back...thx

Use ScriptManager.RegisterStartupScript() and drop the IsBlockRegistered check.


now i have:

Private Sub RegisterMsg(ByVal strMessage As String, ByVal strScriptTag As String)
Dim strScript As String = "alert(""" & strMessage & """);"

If (Not Page.ClientScript.IsClientScriptBlockRegistered("key")) Then
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "key", strScript, True)
End If
End Sub

and it is the same...it did not work correct, the IsClientScriptBlockRegistered-Method returns every time false and because of this the script will be written a second one, and third one and so on...


Hi,

Since the script is created dynamically, it won't be persisted between different requests.

So, before the method is called on the second request, this script block does not exist.

And my suggestion is you don't have to call this multiple times, just call it on initial request and it'll always stay on the page since the page isn't fully refreshed.


the problem is that the appl. dont makes really postbacks during running, only partial postbacks...

and my wish is that i will register and unregister scripts during partial postbacks...how can i do it ?


For partial postback, you can register new scripts.

But you can't unregister existed scripts on server side.


Has there been any progress made on this issue? I am just experiencing this now.

I've overriden OnInit with the following (similar) code:

if (!Page.ClientScript.IsClientScriptBlockRegistered(typeof(Page),"MyScript")
{
// use StringBuilder/StringWriter to build script
ScriptManager.RegisterClientScriptBlock(this,typeof(Page),"MyScript",sb.ToString(),true);
}
 
But the conditional is always true so the script gets recreated every time there is an asyncPostback.
One thing I noticed (using Firebug in Firefox) is on the initial load of the page the script is created
within the form and then on each successive asyncPostback a new script is created in the <head>.
Why is there no IsClientScriptBlockRegistered function in the ScriptManager class? Is that the problem?
That the Page.ClientScript and ScriptManager are looking at two different collections of scripts?

Dynamic content for modal popup

Hi -

I want to change the contents of a modal popup dynamically through client side script. For that purpose, I've included a div element and change it's innerhtml property. When I test whether the change got done I can read the new value through javascript but the modal popup extender shows the old html (which loaded when the page was loaded). I think I may have to call a method to let the modal popup extender know I updated the content, but I don't know how to do that.

Any help is greatly appreciated!!

Oliver

I thought I'd add some code so you know what I am talking about.

Thanks,

Oliver

 function showpopup() { event.bubble = false; var d = document.getElementById("popupcontent"); d.innerhtml='<img src="images/spinner.gif" alt="Please wait ..." />'; var popup =$find('BMPNoShow'); popup.show(); alert(d.innerhtml); }<ajaxToolkit:ModalPopupExtender ID="MPNoShow" BehaviorID="BMPNoShow" runat="server" CancelControlID="BNoShowCancel" DropShadow="true" PopupControlID="PNoShow" PopupDragHandleControlID="PNoShow" TargetControlID="PNoShow"> </ajaxToolkit:ModalPopupExtender> <asp:Panel ID="PNoShow" runat="server" Style="background: white; border-color: Gray; border-style: solid; width: auto; padding: 10px" EnableViewState="false"> <div id="popupcontent"> </div> <br /> <asp:Button ID="BNoShowCancel" runat="server" Text="Cancel" UseSubmitBehavior="false" /> <asp:Button ID="BNoShowConfirm" runat="server" Text="Mark as no-show" UseSubmitBehavior="false" /> <asp:HiddenField ID="HFNoShowRequest" runat="server" /> </asp:Panel>

Take a look at the Dynamic* properties of the modalpopup, which allow you to load the content of a modalpopup dynamically using a web service. This will automatically load the content into the modalpopup when it is shown without you having to do anything.


Thanks - that's even better than what I was trying to do :)

Monday, March 26, 2012

Duplicate Script Reference

I am trying to create a WebControl dll. There is a common.js file in the dll. It is shared bewteen all WebControls.

If I simply add the script reference using
ScriptManager.Scripts.Add( ...) or implementingGetScriptReferences function of IScriptControl. Duplicate script reference will be created when user add multiple web controls.

It is a problem because a ajax javascript class cannot be registered twice.

If I use ScriptManager.RegisterClientScriptInclude, the script will be unique but it is inserted before ajax javascript liberary.

It seems that I have to do something special to make this work. anyone has suggestions or has experienced the same issue??

thanks

Rushui

Look up the example under ajax.asp.net/docs for the IScriptControl interface. If you implement the interface using their example as a template you shouldn't have issues; I'm not sure what they're doing under the hood, but the GetScriptReferences method specified is being called in some way that helps avoid conflicts.

Upon further investigation, it seems that duplicate script reference of a web control dll can be avoided by implementing GetScriptReference function of IScriptControl interface. The Scripts collection of ScriptManager however doesnot check for duplicates.

My situation is a bit strange since I am trying to share a common js lib between the page and the web control dll. Guess I just have to avoid doing that.

Thanks

Rushui