A
Ahmet Gunes
Hi all,
I am developing a container control like a standard Div with a template
property called Content.
Unfortunately, although the button implements the IPostBackEventHandler
interface (since it is the standard Button control) it's click event does
not fire.
I am almost running crazy. I don't understand the problem. Could you please
help?
Here is my code for my custom container:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
namespace MyCustomControlLibrary {
[ParseChildren(true)]
[PersistChildren(false)]
public class MyDiv: WebControl {
private Control container = null;
protected override HtmlTextWriterTag TagKey {
get {
return HtmlTextWriterTag.Div;
}
}
[DefaultValue(null)]
[Browsable(false)]
[TemplateInstance(TemplateInstance.Single)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[NotifyParentProperty(true)]
public ITemplate Content {
get;
set;
}
protected override void OnInit(EventArgs e) {
if (Content != null) {
container = new HtmlGenericControl("div");
container.ID = ClientID + "_Content";
Content.InstantiateIn(container);
}
}
protected override void CreateChildControls() {
base.CreateChildControls();
if (container != null) {
Controls.Add(container);
}
}
}
}
Here is a sample ASPX page that uses MyDiv:
<%@ Page Debug="true" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="MyTestWebApplication._Default" %>
<%@ Register Assembly="MyCustomControlLibrary"
Namespace="MyCustomControlLibrary" TagPrefix="mcc" %>
<!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">
<mcc:MyDiv ID="MyDiv1" runat="server">
<Content>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" />
</Content>
</mcc:MyDiv>
</form>
</body>
</html>
I am developing a container control like a standard Div with a template
property called Content.
Unfortunately, although the button implements the IPostBackEventHandler
interface (since it is the standard Button control) it's click event does
not fire.
I am almost running crazy. I don't understand the problem. Could you please
help?
Here is my code for my custom container:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
namespace MyCustomControlLibrary {
[ParseChildren(true)]
[PersistChildren(false)]
public class MyDiv: WebControl {
private Control container = null;
protected override HtmlTextWriterTag TagKey {
get {
return HtmlTextWriterTag.Div;
}
}
[DefaultValue(null)]
[Browsable(false)]
[TemplateInstance(TemplateInstance.Single)]
[PersistenceMode(PersistenceMode.InnerProperty)]
[NotifyParentProperty(true)]
public ITemplate Content {
get;
set;
}
protected override void OnInit(EventArgs e) {
if (Content != null) {
container = new HtmlGenericControl("div");
container.ID = ClientID + "_Content";
Content.InstantiateIn(container);
}
}
protected override void CreateChildControls() {
base.CreateChildControls();
if (container != null) {
Controls.Add(container);
}
}
}
}
Here is a sample ASPX page that uses MyDiv:
<%@ Page Debug="true" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="MyTestWebApplication._Default" %>
<%@ Register Assembly="MyCustomControlLibrary"
Namespace="MyCustomControlLibrary" TagPrefix="mcc" %>
<!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">
<mcc:MyDiv ID="MyDiv1" runat="server">
<Content>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" />
</Content>
</mcc:MyDiv>
</form>
</body>
</html>