AJAX and embeded controls

J

Jerry C

I am having trouble using a user control with AJAX. The control will put a
script in to a Literal on the page The code is below. It runs the first time
then when the button is pressed it will not run.

Code:

ASPX page:


<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb"
Inherits="_Default" %>

<%@ Register Src="AJAXPlayAsset.ascx" TagName="AJAXPlayAsset"
TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language=javascript
src="App_Java/PlayAssetX.js"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePartialRendering=true >
<Scripts>
<asp:ScriptReference path="App_Java/PlayAssetX.js" />
</Scripts>
</asp:ScriptManager>
<asp:Button ID="btnEnlarge" runat="server" Text="Button" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<uc1:AJAXPlayAsset id="AJAXPlayAsset1" runat="server">
</uc1:AJAXPlayAsset>

</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnEnlarge"
EventName="Click" />
</Triggers>
</asp:UpdatePanel>

</div>
</form>
</body>
</html>




Code Behind for ASPX page:



Partial Class _Default
Inherits System.Web.UI.Page
Dim strHTMLAssetToPlay As String = ""
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
strHTMLAssetToPlay = "<script>PlayAsset('<Embed
src=""tmp330Octave_Stomp.mp3"" width=350 height=350></embed>');</script>"
If Not IsPostBack Then
AJAXPlayAsset1.AssetString = strHTMLAssetToPlay
End If
End Sub
Protected Sub btnEnlarge_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnEnlarge.Click
'do something here like change the size of the Imbed
AJAXPlayAsset1.AssetString = strHTMLAssetToPlay
End Sub
End Class



Code for User control ASCX:


<%@ Control Language="VB" ClassName="AJAXPlayAsset" AutoEventWireup="false"
CodeFile="AJAXPlayAsset.ascx.vb" Inherits="AJAXPlayAsset" %>
<table><tr><td>
<asp:Literal ID="Literal1" runat="server"></asp:Literal><br />
</td></tr></table>


Code behind for the ASCX

'This is the user control
Partial Class AJAXPlayAsset
Inherits System.Web.UI.UserControl
Public Property AssetString() As String
Get
Return ViewState("AString")
End Get
Set(ByVal value As String)
ViewState("AString") = value
Literal1.Text = value
End Set
End Property

End Class

to try the code you may have to find a windows media file to play mine works
ok with a MP3


Thank you
 
W

Walter Wang [MSFT]

Hi Jerry,

To run script in partial-page rendering, you must use
ScriptManager.RegisterStartupScript to register the script instead of
simply outputing the scripting into the page:

Protected Sub btnEnlarge_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnEnlarge.Click
ScriptManager.RegisterStartupScript(AJAXPlayAsset1, GetType(Page),
"test", strHTMLAssetToPlay, False)
End Sub


You could find more information about ScriptManager.RegisterStartupScript
here:

http://ajax.asp.net/docs/mref/M_System_Web_UI_ScriptManager_RegisterStartupS
cript_5_d03cd23f.aspx

Hope this helps.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jerry C

Walter,

Thank you for the reply. I have coded the start up scripts in the user
control and still the first mp3 will play but the second script will not
play. Here is the code

UserControl:

<%@ Control Language="VB" ClassName="AJAXPlayAsset" AutoEventWireup="false"
CodeFile="AJAXPlayAsset.ascx.vb" Inherits="AJAXPlayAsset" %>


Usercontrol code behind:


'This is the user control
Partial Class AJAXPlayAsset
Inherits System.Web.UI.UserControl
Public Property RegScript1() As String
Get
Return ViewState("AString")
End Get
Set(ByVal value As String)
Dim strHTMLAssetToPlay As String
strHTMLAssetToPlay = "<script>PlayAsset('<Embed
src=""tmp330Octave_Stomp.mp3"" width=350 height=350></embed>');</script>"
ScriptManager.RegisterStartupScript(Me, GetType(Page),
"PlayAsset1", strHTMLAssetToPlay, False)
End Set
End Property
Public Property RegScript2() As String
Get
Return ViewState("AString")
End Get
Set(ByVal value As String)
Dim strHTMLAssetToPlay As String
strHTMLAssetToPlay = "<script>PlayAsset('<Embed
src=""tmp4E-Tango.mp3"" width=350 height=350></embed>');</script>"
ScriptManager.RegisterStartupScript(Me, GetType(Page),
"PlayAsset", strHTMLAssetToPlay, False)
End Set
End Property
End Class


Default page:

<%@ Page Language="VB" ClassName="AJAXPage" AutoEventWireup="true"
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register Src="AJAXPlayAsset.ascx" TagName="AJAXPlayAsset"
TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language=javascript
src="App_Java/PlayAssetX.js"></script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePartialRendering=true >
</asp:ScriptManager>
<div>
<asp:Button ID="btnEnlarge" runat="server" Text="Button" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<uc1:AJAXPlayAsset ID="AJAXPlayAsset1" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID=btnEnlarge
EventName=Click/>
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

Default page code behind:

Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not IsPostBack Then
AJAXPlayAsset1.RegScript1 = ""
End If
End Sub
Protected Sub btnEnlarge_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnEnlarge.Click
'do something here like change the size of the Imbed
AJAXPlayAsset1.RegScript2 = ""
End Sub
End Class


The second asset will not play. but if I remove the Trigger Section from the
default page UpdatePanel section then the second asset plays of course the
page has also posted back which is what I want to prevent with AJAX. I want
to have buttons that change the size or MP3 or whatever without posting the
page back just change the MP3 or the player settings of the file played with
the script. This script is what is neccessary because of the law suite about
enbeded objects so it is necessary. I hope this will work with AJAX.

Thank you
 
W

Walter Wang [MSFT]

Hi Jerry,

Registering startup script is not recommended in your property. It's
recommended to do it either in Page_Load or OnPreRender. Since here the
script you need to register is controlled by a property, it should be done
in OnPreRender:

Public Property AssetString() As String
Get
Return ViewState("AString")
End Get
Set(ByVal value As String)
ViewState("AString") = value
End Set
End Property

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "test",
AssetString, False)
MyBase.OnPreRender(e)
End Sub


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Jerry,

I am interested in this issue. Would you mind letting me know the result of
the suggestions? If you need further assistance, feel free to let me know.
I will be more than happy to be of assistance.

Have a great day!


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jerry C

Walter,

Thank you for the replys. I have tried putting the script in the prerender
and it still does not work. All the code is the same and the control code
behing is:

'This is the user control
Partial Class AJAXPlayAsset
Inherits System.Web.UI.UserControl
Dim strHTMLAssetToPlay As String

Public Property RegScript1() As String
Get
Return ViewState("AString")
End Get
Set(ByVal value As String)
strHTMLAssetToPlay = "<script>PlayAsset('<Embed
src=""tmp330Octave_Stomp.mp3"" width=350 height=350></embed>');</script>"
End Set
End Property

Public Property RegScript2() As String
Get
Return ViewState("AString")
End Get
Set(ByVal value As String)
strHTMLAssetToPlay = "<script>PlayAsset('<Embed
src=""tmp4E-Tango.mp3"" width=350 height=350></embed>');</script>"
End Set
End Property


Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "PlayAsset",
strHTMLAssetToPlay, False)
MyBase.OnPreRender(e)
End Sub
End Class

This code all works without AJAX

Thank you
 
W

Walter Wang [MSFT]

Hi Jerry,

Try following example code to see if works for you (make sure you have
those two test mp3 files in your web site with the webform):


1) AJAXPlayAsset.ascx:

<%@ Control Language="VB" ClassName="AJAXPlayAsset" AutoEventWireup="false"
CodeFile="AJAXPlayAsset.ascx.vb"
Inherits="AJAXPlayAsset" %>

<script type="text/javascript" language="javascript">
function PlayAsset(x)
{
document.getElementById('Player').URL = x;
}
</script>

<object id="Player" width="200" height="150"
classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6"
type="application/x-oleobject">
</object>


2) AJAXPlayAsset.ascx.vb

Partial Class AJAXPlayAsset
Inherits System.Web.UI.UserControl

Public Property PlayURL() As String
Get
Return IIf(ViewState("PlayURL") Is Nothing, "track09.mp3",
CType(ViewState("PlayURL"), String))
End Get
Set(ByVal value As String)
ViewState("PlayURL") = value
End Set
End Property

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "PlayAsset",
String.Format("PlayAsset('{0}');", PlayURL), True)
MyBase.OnPreRender(e)
End Sub
End Class


3) Default.aspx

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb"
Inherits="_Default" %>

<%@ Register Src="AJAXPlayAsset.ascx" TagName="AJAXPlayAsset"
TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePartialRendering="true">
</asp:ScriptManager>
<asp:Button ID="btnEnlarge" runat="server" Text="Button" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<uc1:AJAXPlayAsset ID="AJAXPlayAsset1"
runat="server"></uc1:AJAXPlayAsset>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnEnlarge"
EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>


4) Default.aspx.vb

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load

End Sub

Protected Sub btnEnlarge_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnEnlarge.Click
AJAXPlayAsset1.PlayURL = "Track13.mp3"
End Sub
End Class


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jerry C

Walter,

Thank you for the reply. I will try your code but I realy need to be using
the embed tag because it will play any plugin for the media based on the file
associations. embeding player objects adds a aditional layer to decide which
object to embed in the page embed tag seems easer than the object tag
using a script to embed objects is the result of the lawsuit see:
http://www.devx.com/webdev/Article/30154/1763

If anyone has tried to use AJAX for this it would help I think I may have to
work with the AJAX documentation about scripts see the page
/tutorials/EnhancingJavaScriptTutorial.aspx in the AJAX documentation web
site included with the deploy of AJAX 1.0

I have kind of run out of time on this project and will probably not use
AJAX for this. Maybe later someone else will find a solution.

Thank you
 
W

Walter Wang [MSFT]

Hi Jerry,

I'm sorry that I didn't noticed at first that your original requirement is
to use <embed> tag instead of <object>.

Based on my test, the <embed> should be working for AJAX:


1. This is the UserControl:

<%@ Control Language="VB" ClassName="WebUserControl" %>

<script runat="server">
Public Property PlayURL() As String
Get
Return IIf(ViewState("PlayURL") Is Nothing, "track09.mp3",
CType(ViewState("PlayURL"), String))
End Get
Set(ByVal value As String)
ViewState("PlayURL") = value
End Set
End Property

Protected Overrides Sub Render(ByVal writer As
System.Web.UI.HtmlTextWriter)
writer.Write(String.Format("<embed src='{0}' />", PlayURL))
End Sub

</script>


2. This is the test WebForm:

<%@ Page Language="VB" %>

<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl"
TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Me.WebUserControl1.PlayURL = "Track13.mp3"
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="Button" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>


Please test this on your side and let me know the result. Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jerry C

Walter,

Thank you for the reply. I tested this and it works. I will try to
intergrate it into the application and see if it works there.

Thank you
Jerry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top