473,382 Members | 1,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Events not firing in templated control

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
0 2321

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Crucifix | last post by:
Hello, I'm writing a small C# app, and part of what I'm trying to do involves the dragging of PictureBox controls on a form. Unfortunately, MouseMove seems to be behaving very oddly, causing...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
1
by: Earl Teigrob | last post by:
PROBLEM: When a user control is loaded into a PlaceHolder control more than once, the events do not fire on the first click of a control on the dynamically loaded user control. In other words, the...
5
by: Ben Fidge | last post by:
I've got a problem where some buttons placed on a user control are only firing their OnClick events when the user clicks on them for the second time. I've got the situation where some common...
3
by: Mike | last post by:
Hi, I am adding controls dynamically in a WebForm, but none of these controls' events fire. Here is the class code I am using. I have tried so many things, but nothing works :-( namespace...
5
by: bryanp10 | last post by:
I have a page that is almost entirely dynamically created. Textboxes and checkbox are working fine, firing events, and persistent their state. DropDownList is giving me a major headache. All...
4
by: thomson | last post by:
Hi all, i do have a user control with 4 buttons, and all the events are firing properly, My problem is that i need to right an event handler in the user control, which gets fired after a...
9
by: Erik Frey | last post by:
Hi there, Just curious as to whether there's a clever way to see the events a control/object is firing off, perhaps written out to the debug console. It would be really handy to know which...
4
by: jehugaleahsa | last post by:
Hello: Is there a way to prevent one event from firing while another event is already being fired? I have a tool that extracts media from web pages and it has multiple events firing when the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.