I began by following the steps suggested in this post http://www.timgittos.com/
I’ve embedded the ascx as resource (build action), deleted the AutoEventWireup and declared all the components. When I tried to access it from the actual website – NOTHING. The component does not exist.
Searching back and forth I’ve found that the namespace is a MUST. If you omit it, it doesn’t show.
The resulting code is something like
namespace MyControls
{
public partial class MyControls : System.Web.UI.UserControl
{
HiddenField hfStatus;
protected void Page_Load(object sender, EventArgs e)
{
String content = String.Empty;
System.IO.Stream stream = System.Reflection.Assembly.
using (System.IO.StreamReader reader = new System.IO.StreamReader(stream)
{
content = reader.ReadToEnd();
}
Control control = Page.ParseControl(content);
this.Controls.Add(control);
hfStatus = ((HiddenField)control.
}
}
}
So far so good, but, I had to add a javascript file into the pot.
Plus I wanted this javascript to appear only once on the page, even if the control appears twice or more. In order to achieve this I added a restriction: use of the ID.
But first thing first, meaning, add the Javascript file as embedded resource.
Now move the loading code to OnInit (where it should be placed) instead of Page_Load:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
String content = String.Empty;
System.IO.Stream stream = System.Reflection.Assembly.
using (System.IO.StreamReader reader = new System.IO.StreamReader(stream)
{
content = reader.ReadToEnd();
}
Control control = Page.ParseControl(content);
this.Controls.Add(control);
hfStatus = ((HiddenField)control.
if ((Session["MyControlScript"] == null) || (Session["MyControlScript"].
{
Session["MyControlScript"] = this.ID;
ScriptManager.GetCurrent(Page)
}
}
And in the AssemblyInfo.cs I added the following line:
[assembly: System.Web.UI.WebResource("
And voila! It compiles.
Now all we need is a test page (inside a site)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="tg" Assembly="MyControls" Namespace="MyControls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/
<html xmlns="http://www.w3.org/1999/
<head runat="server">
<title></title>
</head>
<body>
<script type="text/javascript">
function check() {
alert(document.getElementById(
alert(document.getElementById(
}
</script>
<form id="form1" runat="server">
<div>
<input type="button" onclick="check();" value="TEST!!!" />
</div>
<br />
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<tg: MyControl ID="Cont1" runat="server />
<br />
<tg: MyControl ID="Cont2" runat="server />
</div>
</form>
</body>
</html>
note: DON'T USE WORD, it took twice the time just to fix the mess this bloody editor did:(
No comments:
Post a Comment