DataGrid with DropDownLists not responding to OnSelectedIndexChanged

G

G-Fit

Hello group,

I have a TemplateColumn containing a DropDownList in my DataGrid. I added
an OnSelectedIndexChanged property in that DropDownList, but I can see in
debug mode that changing the selection will not call my function defined in
OnSelectedIndexChanged (the page reloads, I didn't forget
AutoPostBack=true).
Is this normal ? Is there a way around it ?

Karine
 
J

Jeffrey Tan[MSFT]

Hi Karine,

Thank you for posting in the community!

Based on my understanding, you use dropdownlist TemplateColumn in your
datagrid control, then you add SelectedIndexChanged event handler, but it
does not fire.

================================================
Actually, I think this problem may occur, if you did not judge the
Page.IsPostBack property, then re-bind the datagrid each postback.
Doing this, your entire datagrid wil be re-filled in each postback, then
the dropdownlist's SelectedIndexChanged event will be lost. To workaround
this problem, you may just do the databinding in the first initial time,
like this:

<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 160px; POSITION:
absolute; TOP: 40px"
runat="server" Width="424px" Height="288px">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:DropDownList ID="ddl" Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="SelectedIndexChangedEventHandler">
<asp:ListItem Value="a">a</asp:ListItem>
<asp:ListItem Value="b">b</asp:ListItem>
<asp:ListItem Value="c">c</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
SqlDataAdapter adapter=new SqlDataAdapter("select * from
jobs","server=localhost;database=pubs;uid=sa;pwd=");
DataSet ds=new DataSet();
adapter.Fill(ds);
}
}

protected void SelectedIndexChangedEventHandler(object sender,
System.EventArgs e)
{
this.Response.Write("abcde");
}

Note: I use Sql Server's default "jobs" table in "pubs" database.

=====================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

G-Fit

Hi Karine,
Thank you for posting in the community!

Based on my understanding, you use dropdownlist TemplateColumn in your
datagrid control, then you add SelectedIndexChanged event handler, but it
does not fire.

================================================
Actually, I think this problem may occur, if you did not judge the
Page.IsPostBack property, then re-bind the datagrid each postback.
Doing this, your entire datagrid wil be re-filled in each postback, then
the dropdownlist's SelectedIndexChanged event will be lost.
That was it. I throwed my ASP.NET book at the developer's head ;) it's a
mistake he makes so often I should have checked this first...

Karine
 
G

G-Fit

Another question :

When I change the selected index of my DropDownList, I would like to know
which row of the DataGrid it was in (in short, I would like to get the
DataKey corresponding to that row). Is this possible ?

Karine
 
G

G-Fit

I did the following, which works, but isn't there an easier/nicer solution ?
This one is a bit heavy...

public void SelectedIndexChanged(Object sender, System.EventArgs e)
{
int key=0;
for (int i=0; i<dg.Items.Count; i++)
if ((DropDownList)dg.Items.Cells[2].Controls[1] == sender)
key = int.Parse(dg.DataKeys.ToString());
[...]
}
 
J

Jeffrey Tan[MSFT]

Hi G-Fit,

Thanks very much for your feedback.

I am glad my reply makes sense to you. :)

For your further concern, I think the simplest way to get the DropDownList
is using the "sender" parameter to get the DropDownList, then use the
Parent property to get the DataGridItem object(Which is the row of the
datagrid that dropdownlist is in), at last, you can use
DataGridItem.ItemIndex to get the index number.

Note, DropDownList's Parent is TableCell object, and the TableCell's Parent
is DataGridItem object, so you should use DropDownList.Parent.Parent.

Do like this:

protected void SelectedIndexChangedEventHandler(object sender,
System.EventArgs e)
{
DropDownList ddl=(DropDownList)sender;
DataGridItem dgi=(DataGridItem)ddl.Parent.Parent;

this.Response.Write(dgi.ItemIndex.ToString());
}

To see the server side control tree in datagrid, please enable trace, then
the output will write out the control tree structure. To enable trace, add
Trace="true" in Page directive. Like this:
<%@ Page Trace="true" language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="datagriddropdownlistcolumn.WebForm1" %>

=============================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

G-Fit

For your further concern, I think the simplest way to get the DropDownList
is using the "sender" parameter to get the DropDownList, then use the
Parent property to get the DataGridItem object(Which is the row of the
datagrid that dropdownlist is in), at last, you can use
DataGridItem.ItemIndex to get the index number.

Note, DropDownList's Parent is TableCell object, and the TableCell's Parent
is DataGridItem object, so you should use DropDownList.Parent.Parent.

Works great, thanks.
Karine
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top