Events not firing in templated control

M

Mike

Hi. I can't figure out why a button's click event is not firing in a
templated control I've created (first time I've tried creating one).
Please can someone help? On a point of lesser importance, how can I get
the designer to realise that my control can contain markup?

This is all ASP.Net v 2.0

This is my default.aspx:

Below is the source of three files: Default.aspx, Default.aspx.cs and
TemplateContainer.cs which will demonstrate the problem.

You will notice that in the btnEdit_Click event I raise a
NotImplementedException - this exception is never thrown. Why is the
button losing the event wireup?

TIA


Default:aspx
============
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="tc" Namespace="Controls.Templated" %>
<!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">
<div>
<tc:TemplatedControl ID="tc" runat="server">
<ItemTemplate>
<asp:Button ID="btnEdit" Text="Edit" runat="server"
OnClick="btnEdit_Click" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="btnCancel" Text="Cancel" runat="server"
OnClick="btnCancel_Click" />
</EditItemTemplate>
</tc:TemplatedControl>

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


Default.aspx.cs
===========
using System;
using System.Web.UI;
using Controls.Templated;

public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
tc.ChangeMode(TemplateMode.ReadOnly);

}

protected void btnEdit_Click(object sender, EventArgs e)
{
throw new NotImplementedException("Not implemented");
}

protected void btnCancel_Click(object sender, EventArgs e)
{
throw new NotImplementedException("Not implemented");
}
}


TemplateContainer.cs
===============
using System;
using System.Web;
using System.Web.UI;

namespace Controls.Templated
{
public class TemplateContainer : Control, INamingContainer
{
private TemplatedControl parent;
public TemplateContainer(TemplatedControl parent)
{
this.parent = parent;
}

public string MyValue
{
get
{
return parent.MyValue;
}
}
}

public enum TemplateMode
{
Invisible,
Edit,
Insert,
ReadOnly
}

[
ParseChildren(ChildrenAsProperties = true),
]
public class TemplatedControl : Control, INamingContainer
{
private ITemplate m_itemTemplate;
private ITemplate m_editTemplate;
private string m_myValue = null;
private Control m_itemTemplateContainer;
private Control m_editItemTemplateContainer;
private TemplateMode m_mode;

protected override void OnDataBinding(EventArgs e)
{
EnsureChildControls();
base.OnDataBinding(e);
}

public void ChangeMode(TemplateMode newMode)
{
m_mode = newMode;
}

[TemplateContainer(typeof(TemplateContainer))]
public ITemplate ItemTemplate
{
get
{
return m_itemTemplate;
}
set
{
m_itemTemplate = value;
}
}

[TemplateContainer(typeof(TemplateContainer))]
public ITemplate EditItemTemplate
{
get
{
return m_editTemplate;
}
set
{
m_editTemplate = value;
}
}

public string MyValue
{
get
{
return m_myValue;
}
set
{
m_myValue = value;
}
}

public Control ItemTemplateContainer
{
get
{
return m_itemTemplateContainer;
}
}

public Control EditItemTemplateContainer
{
get
{
return m_editItemTemplateContainer;
}
}

protected override void CreateChildControls()
{
if (ItemTemplate != null && m_mode ==
TemplateMode.ReadOnly)
{
m_itemTemplateContainer = new TemplateContainer(this);
ItemTemplate.InstantiateIn(m_itemTemplateContainer);
Controls.Add(m_itemTemplateContainer);
}
else if (EditItemTemplate != null && m_mode ==
TemplateMode.Edit)
{
m_editItemTemplateContainer = new
TemplateContainer(this);

EditItemTemplate.InstantiateIn(m_editItemTemplateContainer);
Controls.Add(m_editItemTemplateContainer);
}
}

}

}
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top