Markup:
<%@ Page Language="C#" CodeBehind="~/Test.aspx.cs" Inherits="ActiveTest.Test" %>
<html>
<head id="Head2" runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script language="javascript">
$(document).ready(function () {
setUrl('<%= this.frmFrame1.ClientID %>');
setUrl('<%= this.frmFrame2.ClientID %>');
setUrl('<%= this.frmFrame3.ClientID %>');
});
</script>
</head>
<body>
<form id="form2" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true" />
<iframe id="frmFrame1" runat="server"></iframe>
<hr />
<iframe id="frmFrame2" runat="server"></iframe>
<hr />
<iframe id="frmFrame3" runat="server"></iframe>
<hr />
</form>
</body>
</html>
Code:
public partial class Test : Page
{
public Hashtable LinkTable
{
get { return (Hashtable)Session["LINKS"]; }
set { Session["LINKS"] = value; }
}
private string script = @"
var key = ""{0}"";
function setUrl(frameId) {{
PageMethods.GetUrl(frameId, key, OnSucceeded, OnFailed);
}}
function OnSucceeded(result, usercontext, methodName) {{
var id = result.split("","")[0];
var url = result.split("","")[1];
$(""#"" + id).attr(""src"", url);
}}
function OnFailed(error, userContext, methodName) {{
}}
";
private string guid = string.Empty;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (this.LinkTable == null)
this.LinkTable = new Hashtable();
guid = Guid.NewGuid().ToString();
this.LinkTable.Clear();
this.LinkTable.Add(string.Format("{0},{1}", guid, this.frmFrame1.ClientID),
"http://www.google.com");
this.LinkTable.Add(string.Format("{0},{1}", guid, this.frmFrame2.ClientID),
"http://www.yahoo.com");
this.LinkTable.Add(string.Format("{0},{1}", guid, this.frmFrame3.ClientID),
"http://www.web-sphere.co.uk");
}
[WebMethod]
public static string GetUrl(string frameId, string key)
{
Hashtable Links = (Hashtable)HttpContext.Current.Session["LINKS"];
string url = string.Empty;
string k = key + "," + frameId;
if (Links.ContainsKey(k))
{
url = (string)Links[k];
url = string.Format("{0},{1}", frameId, url);
Links.Remove(k);
HttpContext.Current.Session["LINKS"] = Links;
}
else throw new Exception();
return url;
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
this.ClientScript.RegisterClientScriptBlock(this.GetType(),
this.GetType().Name, string.Format(this.script, guid), true);
}
}
No comments:
Post a Comment