By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,689 Members | 1,106 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,689 IT Pros & Developers. It's quick & easy.

Events not firing in templated control

P: n/a
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_editItemTemplateC ontainer);
Controls.Add(m_editItemTemplateContainer);
}
}

}

}

Jul 14 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.