468,241 Members | 1,608 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,241 developers. It's quick & easy.

DataList in a composite control. ItemCommand not firing.

Hi there,

I am using a DataList in a composite control.

The DataList has an Custom ItemTemplate(ITemplate) which adds a Button
to the DataList Item as shown in the code below :

public class DatalistItemTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
Button B = new Button();

B.DataBinding += new EventHandler(B_DataBinding);

container.Controls.Add(B);
}
private void B_DataBinding(object sender, EventArgs e)
{
Button btn = (Button)sender;

DataListItem container = (DataListItem)btn.NamingContainer ;

btn.ID = "Button" +
Convert.ToString(DataBinder.Eval(((DataListItem)co ntainer).DataItem,
"Item"));
btn.Text =
Convert.ToString(DataBinder.Eval(((DataListItem)co ntainer).DataItem,
"Item"));
btn.CommandArgument =
Convert.ToString(DataBinder.Eval(((DataListItem)co ntainer).DataItem,
"Item"));
btn.CommandName = "Select";
}
}

The Custom ItemTemplate is assigned and then the databind is executed
using the following code :

this.DataList1.RepeatColumns = this.ArrayListDisplayedTabs.Count;
this.DataList1.RepeatDirection = RepeatDirection.Horizontal;
DataList1.ItemTemplate = new DatalistItemTemplate();

this.DataList1.DataSource = this.ArrayListData ;
this.DataList1.DataBind();

The ArrayListData which the datalist is bound to contains the following
type of data

[Serializable()]
public class TabItem
{
string StringItem;

public TabItem(string ItemName)
{
this.StringItem = ItemName;
}

public string Item
{
get { return StringItem; }
}
}

THE BUTTONS ARE BEING PROPERLY DISPLAYED.

The ItemCommand is attached to the DataList in the OnInit Override of
the Control just like all the other events.

this.DataList1.ItemCommand += new
DataListCommandEventHandler(DataList1_ItemCommand) ;

When a button in the DataList is clicked, the ItemCommand should be
fired. This is not happening.

The same DataList when used in a User Control fires the ItemCommand
event properly. However, when used in a Composite Control the event
does not fire.

Any suggestions..

Thanks...

Shawn..

Jul 24 '06 #1
1 3700
Hi,

if you databind DataList every time you add it to Controls collection, that
could "eat" up the events. Do initial databinding inside if !Page.IsPostBack
or optionally in overridden dataBind method, so that dataBind of UC should
be called to get dataList databound.

-
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

<sh***********@hotmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
Hi there,

I am using a DataList in a composite control.

The DataList has an Custom ItemTemplate(ITemplate) which adds a Button
to the DataList Item as shown in the code below :

public class DatalistItemTemplate : ITemplate
{
public void InstantiateIn(Control container)
{
Button B = new Button();

B.DataBinding += new EventHandler(B_DataBinding);

container.Controls.Add(B);
}
private void B_DataBinding(object sender, EventArgs e)
{
Button btn = (Button)sender;

DataListItem container = (DataListItem)btn.NamingContainer ;

btn.ID = "Button" +
Convert.ToString(DataBinder.Eval(((DataListItem)co ntainer).DataItem,
"Item"));
btn.Text =
Convert.ToString(DataBinder.Eval(((DataListItem)co ntainer).DataItem,
"Item"));
btn.CommandArgument =
Convert.ToString(DataBinder.Eval(((DataListItem)co ntainer).DataItem,
"Item"));
btn.CommandName = "Select";
}
}

The Custom ItemTemplate is assigned and then the databind is executed
using the following code :

this.DataList1.RepeatColumns = this.ArrayListDisplayedTabs.Count;
this.DataList1.RepeatDirection = RepeatDirection.Horizontal;
DataList1.ItemTemplate = new DatalistItemTemplate();

this.DataList1.DataSource = this.ArrayListData ;
this.DataList1.DataBind();

The ArrayListData which the datalist is bound to contains the following
type of data

[Serializable()]
public class TabItem
{
string StringItem;

public TabItem(string ItemName)
{
this.StringItem = ItemName;
}

public string Item
{
get { return StringItem; }
}
}

THE BUTTONS ARE BEING PROPERLY DISPLAYED.

The ItemCommand is attached to the DataList in the OnInit Override of
the Control just like all the other events.

this.DataList1.ItemCommand += new
DataListCommandEventHandler(DataList1_ItemCommand) ;

When a button in the DataList is clicked, the ItemCommand should be
fired. This is not happening.

The same DataList when used in a User Control fires the ItemCommand
event properly. However, when used in a Composite Control the event
does not fire.

Any suggestions..

Thanks...

Shawn..

Jul 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kenneth | last post: by
2 posts views Thread by Jim Heavey | last post: by
3 posts views Thread by Chris | last post: by
3 posts views Thread by Patrick Olurotimi Ige | last post: by
4 posts views Thread by Patrick.O.Ige | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.