DataList in a composite control. ItemCommand not firing.

S

shantanu_kush

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)container).DataItem,
"Item"));
btn.Text =
Convert.ToString(DataBinder.Eval(((DataListItem)container).DataItem,
"Item"));
btn.CommandArgument =
Convert.ToString(DataBinder.Eval(((DataListItem)container).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..
 
T

Teemu Keiski

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
 

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