Visual Basic .NET » ASP.NET General Discussion
DataList in a composite control. ItemCommand not firing. -- Shawn --


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

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 postback happens but the event is not fired.
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...
Shantanu..

-- Shawn --


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

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 Cotrols does do a postback but the event is not fired.
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...
Shantanu..

-- Shawn --


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

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...
Shantanu..

[Submit Comment]Home