D
Dominic
Hi guys,
I'm not sure if this question belongs to FAQ, but I couldn't find a
concrete answer.
I created a Datagrid control using ItemTemplate, but it's NOT a
in-place editing datagrid. One of the columns of the data grid
contains a DropDownlist. I managed to create this datagrid control as
follows.
<asp:datagrid RunAt="server" id="dgItem" DataKeyField="ItemId"...
<columns>
<asp:TemplateColumn HeaderText="Category">
<ItemTemplate>
<aspropDownList RunAt="server" ID="ddlCategory" />
......
The code-behind source looks like
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
dgTest.DataSource = GetItemList();
dgTest.DataBind();
}
}
private void OnItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
int itemId = (int)dgItem.DataKeys[e.Item.ItemIndex];
DropDownList ddlCategory =
(DropDownList)e.Item.FindControl("ddlCategory");
ddlCategory .DataSource = GetCateoryList();
ddlCategory .DataBind();
}
}
This approach works quite well to populate the drop-down list and the
grid itself. However, I have two questions here.
1. How can I catch the "SelectedItemChanged" event fired from the
drop-down-list "ddlCategory"?
One of the previous posting mentioned that we can catch ControlAdded
event and add the SelectedItemChanged event there....
private void dataGrid1_ControlAdded(object sender, ControlEventArgs e)
{
if(e.Control.GetType == typeof(ComboBox))
((DropDownList)e.Control).SelectedIndexChanged += new
System.EventHandler(dropDownList_SelectedIndexChanged);
}
Unfortunately, ControlAdded event is only for WinForms, not WebForms
(Right??)
A possible alternative is to use event bubbling. However, this
involves subclassing the dropdownlist to raise bubble event. Right?
Other than the event bubbling, is there any other easier method? Some
codes for illustration will be much appreciated.
2. The above datagrid items actually contains subitems. It is
something like
HeaderA HeaderB HeaderC
Item1A Item1B Item1C
Item11A Item11B Item11C
Item12A Item12B Item12C
Item2A Item2B Item2C
Item21A Item22B Item22C
where Item11A and Item12A is the sub-item of Item1A, etc.
I implemented the above as two levels of datagrid. Item1 and Item2
belong to datagrid of level1, while Item11, 12, 21, etc are level2. As
illustrated in the first part of my question, I can easily catch the
ItemDataBound event of level1 datagrid. But, how can I catch the
ItemDataBound of level2 data grid?
Thanks again
Dominic
I'm not sure if this question belongs to FAQ, but I couldn't find a
concrete answer.
I created a Datagrid control using ItemTemplate, but it's NOT a
in-place editing datagrid. One of the columns of the data grid
contains a DropDownlist. I managed to create this datagrid control as
follows.
<asp:datagrid RunAt="server" id="dgItem" DataKeyField="ItemId"...
<columns>
<asp:TemplateColumn HeaderText="Category">
<ItemTemplate>
<aspropDownList RunAt="server" ID="ddlCategory" />
......
The code-behind source looks like
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
dgTest.DataSource = GetItemList();
dgTest.DataBind();
}
}
private void OnItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
int itemId = (int)dgItem.DataKeys[e.Item.ItemIndex];
DropDownList ddlCategory =
(DropDownList)e.Item.FindControl("ddlCategory");
ddlCategory .DataSource = GetCateoryList();
ddlCategory .DataBind();
}
}
This approach works quite well to populate the drop-down list and the
grid itself. However, I have two questions here.
1. How can I catch the "SelectedItemChanged" event fired from the
drop-down-list "ddlCategory"?
One of the previous posting mentioned that we can catch ControlAdded
event and add the SelectedItemChanged event there....
private void dataGrid1_ControlAdded(object sender, ControlEventArgs e)
{
if(e.Control.GetType == typeof(ComboBox))
((DropDownList)e.Control).SelectedIndexChanged += new
System.EventHandler(dropDownList_SelectedIndexChanged);
}
Unfortunately, ControlAdded event is only for WinForms, not WebForms
(Right??)
A possible alternative is to use event bubbling. However, this
involves subclassing the dropdownlist to raise bubble event. Right?
Other than the event bubbling, is there any other easier method? Some
codes for illustration will be much appreciated.
2. The above datagrid items actually contains subitems. It is
something like
HeaderA HeaderB HeaderC
Item1A Item1B Item1C
Item11A Item11B Item11C
Item12A Item12B Item12C
Item2A Item2B Item2C
Item21A Item22B Item22C
where Item11A and Item12A is the sub-item of Item1A, etc.
I implemented the above as two levels of datagrid. Item1 and Item2
belong to datagrid of level1, while Item11, 12, 21, etc are level2. As
illustrated in the first part of my question, I can easily catch the
ItemDataBound event of level1 datagrid. But, how can I catch the
ItemDataBound of level2 data grid?
Thanks again
Dominic