ASP.NET AJAX partial postback, dynamic JavaScript values
I am wo开发者_JS百科rking with a user control which is inside an UpdatePanel. The user control uses swfobject to add a flash object to a div in the user control. Part of the functionality of the user control is it allows the user to change 'channel'. The channel is set and handled in the code behind hence the call in the JavaScript below to <%=channel%>.
The problem I have is that when the new channel is saved, the JavaScript code below is still pointing to the old channel. The only way I can fix this is to refresh the page via the code behind, but I'm thinking there must be a better way to do this...
Can anyone help?
<script type="text/javascript">
//Required to readd the widget to the div after partial postback
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(flashVideo_<%=widgetId%>);
// SWFObject embed
function flashVideo_<%=widgetId%>() {
var flashvars = {
initialURL: escape(document.location),
paramXMLPath: "/videoprotoype/single-video.aspx?channel=<%=channel%>"
}
var params = {
bgcolor: "#121212",
allowfullscreen: "true"
}
var attributes = {}
swfobject.embedSWF("/videoprotoype/assets/slideshowpro.swf", "video_<%=widgetId%>", "285", "215", "10.0.0", false, flashvars, params, attributes);
}
</script>
<div id="video_<%=widgetId%>">
</div>
I would take a look at the following links:
http://www.ajaxtutorials.com/ajax-tutorials/updating-an-updatepanel-programmatically-in-c/
http://basgun.wordpress.com/2008/02/27/realtime-dynamic-clock-using-aspnet-ajax-updatepanel/
http://encosia.com/2007/07/13/easily-refresh-an-updatepanel-using-javascript/
You may want to utilize __doPostBack()
to refresh your UpdatePanel.
Would it be a solution to declare the global variable for the currently selected channel at the page level (somewhere in the <head>
section):
<script type="text/javascript">
var channel = <%=channel%>;
</script>
Use this variable in your Javascript instead of using ASP.NET code "nuggets" (<%...%>
).
Now when the user changes the channel and the UpdatePanel
reloads you can register a startup script that will update the channel variable to point to the new one:
Code-behind:
ScriptManager.RegisterStartupScript(myCustomControl, myCustomControl.GetType(),
Guid.NewGuid().ToString(), "channel = " + channel, true);
Where "mycustomControl" is an instance of your user control.
Hope this will help you.
精彩评论