472,985 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Catching an SelectedIndexChanged event from a DropDownList inside a DataGrid

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>
<asp:DropDownList 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_SelectedIndexChan ged);
}

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
Nov 17 '05 #1
2 16916
Thank you for your reply. But I still don't quite get it.

Since there are multiple rows in a datagrid, there will be multiple
Dropdownlists (each one corresponds to one row). So, when
DG1DropDownList is called, which row does it correspond to this event?

Please also see my second question regarding ItemDataBound. This is a
similar question to the first one. Instead of catching the
SelectedItemChanged event from a dropdownlist inside a row of a
datagrid, this is a question about how to catch a ItemDataBound event
from a datagrid inside a row of a datagrid.

Thanks
Dominic
"Saravana" <sa******@sct.co.in> wrote in message news:<u9**************@TK2MSFTNGP11.phx.gbl>...
You can add event handler to dropdownlist like this,
<asp:TemplateColumn headertext="Month">
<ItemTemplate>
<asp:DropDownList id="MonthACT" datavaluefield="Value"
datatextfield="Name" DataSource="<%#oMonthDataSet%>" runat="server"
OnSelectedIndexChanged="DG1DropDownListSelect" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
--
Saravana
Microsoft India Community Star,
MCAD,SE,SD,DBA.
"Dominic" <do****@hotmail.com> wrote in message
news:2b*************************@posting.google.co m...
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>
<asp:DropDownList 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_SelectedIndexChan ged);
}

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

Nov 17 '05 #2
You are correct, there will one eventhandler for all the dropdownlist. In
that eventhanlder,
If your try to get parent object of source, then it will give the
datagriditem(row) in which that dropdownlist was changed. From dataitem you
can do whatever you want, but you cant get the rowindex directly. For
example, check out this event handler for dropdownlist in which i am
changing a hiddenitem value in that row when dropdownlist value is changed
in that row

Sub DG2DropDownListSelect(ByVal sender As Object, ByVal e As
System.EventArgs)

Dim oDropDownList As DropDownList

Dim oGenControl As System.Web.UI.HtmlControls.HtmlInputHidden

oDropDownList = CType(sender, DropDownList)

oGenControl = oDropDownList.Parent.FindControl("DataGrid2RecordS tate")

oGenControl.Value = "Changed"
End Sub
i hope you understand this approach...

--
Saravana
Microsoft India Community Star,
MCAD,SE,SD,DBA.
"Dominic" <do****@hotmail.com> wrote in message
news:2b**************************@posting.google.c om...
Thanks again for your response. I'm sorry if I didn't put my question
more clearly. Let me try to understand your approach first. My
understanding is that even though there may be multiple rows (each
contains one one dropdownlist control), there is only one event
handler DG1DropDownListSelect for all of the dropdownlist controls. Is
that right? If this correct, I expect that there must be a way that
the event handler can figure out which row it corresponds to. For
example, below is the event handler.
private void DG1DropDownListSelect(object sender, System.EventArgs e)
{
rowIndex = e.????
}

Thanks again
Dominic
"Saravana" <sa******@sct.co.in> wrote in message

news:<Oe**************@TK2MSFTNGP12.phx.gbl>...
when DG1Dropdownlist is called, it corresponds to the row in which you have changed dropdownlist value.

--
Saravana
Microsoft India Community Star,
MCAD,SE,SD,DBA.
"Dominic" <do****@hotmail.com> wrote in message
news:2b**************************@posting.google.c om...
Thank you for your reply. But I still don't quite get it.

Since there are multiple rows in a datagrid, there will be multiple
Dropdownlists (each one corresponds to one row). So, when
DG1DropDownList is called, which row does it correspond to this event?

Please also see my second question regarding ItemDataBound. This is a
similar question to the first one. Instead of catching the
SelectedItemChanged event from a dropdownlist inside a row of a
datagrid, this is a question about how to catch a ItemDataBound event
from a datagrid inside a row of a datagrid.

Thanks
Dominic
"Saravana" <sa******@sct.co.in> wrote in message

news:<u9**************@TK2MSFTNGP11.phx.gbl>...
> You can add event handler to dropdownlist like this,
> <asp:TemplateColumn headertext="Month">
> <ItemTemplate>
> <asp:DropDownList id="MonthACT" datavaluefield="Value"
> datatextfield="Name" DataSource="<%#oMonthDataSet%>" runat="server"
> OnSelectedIndexChanged="DG1DropDownListSelect" >
> </asp:DropDownList>
> </ItemTemplate>
> </asp:TemplateColumn>
> --
> Saravana
> Microsoft India Community Star,
> MCAD,SE,SD,DBA.
>
>
> "Dominic" <do****@hotmail.com> wrote in message
> news:2b*************************@posting.google.co m...
> > 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>
> > <asp:DropDownList 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_SelectedIndexChan ged);
> > }
> >
> > 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

Nov 17 '05 #3

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

Similar topics

1
by: Donal | last post by:
I have 3 related dropdowns. When the 1st is changed, the 2nd is updated, and when the 2nd is changed, the 3rd is updated. When i change the 1st dropdown (sites), the SelectedIndexChanged fires...
2
by: Paul Lacey | last post by:
When dynamically placing dropdownlists inside a panel, the dropdownlist's SelectedIndexChanged event doesn't fire. If you take the dropdownlist out of the panel it works properly. Autopostback is...
6
by: Scott Lee | last post by:
I am displaying an ASP.Net generated form in a popup opened with window.showModalDialog. The form contains DropDownList controls. The first ddl is populated via databinding to a datatable, has...
1
by: CurlyFro | last post by:
i have a userControl (.ascx) that has a dropdownlist embedded in a datagrid. the dropdownlist autopostback is set to true. why isn't the selectedIndexChanged firing? Private Sub...
3
by: Shawn | last post by:
I have a DataGrid with only one TemplateColumn wich contains a DropDownList. After I bind the DataGrid I fill the dropDownLists with values in ItemDataBound. None of the DropDownLists contains the...
0
by: Tand35006 | last post by:
Hi, I hope some one can help with this. I have a basic webform with 2 DropDownLists and a single DataGrid. What I am trying to do is populate the first DDList from a dataset on Form_Load. I then...
5
by: Victorious1 | last post by:
Why doesn't the the SelectedIndexchanged event for my dropdownlist ever get executed? Why do I get a page not found error when I select a new item? My dropdownlist is within a form. The items...
11
by: J055 | last post by:
Hi I have a dropdown control which is constructed in another dropdown control SelectedIndexChanged event protected void ddlParamType_SelectedIndexChanged(object sender, EventArgs e) { //...
7
by: Damien | last post by:
Hi guys, I'm trying to learn ASP.NET and got a problem with DroDownList... SelectedIndexChanged doesn't fire. Maybe you'll be able to suggest something. I think it may be connected with the...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.