Monday, March 26, 2012

Multiview and Javascript in a view.

Hi, I am using a Multiview control in an update panel, and I need to output some dynamic Javascript inside a View control. Please see below.

<%@dotnet.itags.org. Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" Height="478px" Width="557px">
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">


</asp:View>
<asp:View ID="View2" runat="server">

<script type="text/Javascript">//<![CDATA[
function aa(){alert('<%= DateTime.Now %>');}
//]]></script>
<input type='button' onclick='aa()' />

</asp:View>
</asp:MultiView>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" /></asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>

--BEHIND--

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button2_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(View2);
}
}

The problem is, that when you click the button that runs the function "aa", an "Object expected" error is thrown. Presumably because the newly outputted script has not been 'registered' with the browser (I checked with Nikhils Web Dev Helper and the script is present).

As I say, the script is dynamic, so I can't move it to an external file, nor would it be good to have to write the script outside of the Multiview (since it's dependent on the controls in View).

Any help or tips greatly welcome - thanks,

Jim


Read this, will give you solution.

http://forums.asp.net/thread/1502486.aspx

WS


Thanks, that did help with my example -however...

In reality my UpdatePanel and Multiview are inside a UserControl. So I dont have access to ScriptManager from the UserControl.

I looked at ScriptManagerProxy, but that doesn't have the RegisterClientScriptBlock method :(

Any idea how I can do this from within a UserControl?

Many thanks

Jim


Is this your code behind from your UserControl?

protected void Button2_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(View2);
}

??

You can add

protected void Button2_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(View2);
System.Web.UI.ScriptManager.RegisterClientScriptBlock ...
}

It works on my end ... should be the same on yours.

WS


AHH! RegisterClientScriptBlock is static - sorry I assumed it wasn't.

Yes that works great, thanks very much - you saved me some awkward workarounds.

Smile

Jim


Can I open up this topic, and add a little twist to it?

I have u panel inside a multiview, this panel has a usercontrol which shows a HTML page from a database (response.binarywrite because it's all blobs) I hav e NO control of the javascript that is used in the HTML files that are loaded from the database.

One of the HTML files that is loaded, contains javascript to have a menu with hoverfunctions enabled on the html-page:

if (window.attachEvent) window.attachEvent("onload", sfHover);

When I switch the multiview.activeindex, back and forth to the panel with the usercontrol, the hover functions do not work anymore...anyway, in IE they don't. In Firefox they do work.

The RegisterClientScriptBlock is no option, because I am not aware of the javascript functions that are used in the database-files.

I hope my explanation is clear...

Rob

No comments:

Post a Comment