Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > DataGrid with DropDownLists not responding to OnSelectedIndexChanged

Reply
Thread Tools

DataGrid with DropDownLists not responding to OnSelectedIndexChanged

 
 
G-Fit
Guest
Posts: n/a
 
      03-02-2004
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


 
Reply With Quote
 
 
 
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      03-03-2004

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:

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

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.

 
Reply With Quote
 
 
 
 
G-Fit
Guest
Posts: n/a
 
      03-03-2004
> 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


 
Reply With Quote
 
G-Fit
Guest
Posts: n/a
 
      03-03-2004
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


 
Reply With Quote
 
G-Fit
Guest
Posts: n/a
 
      03-03-2004
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[i].Cells[2].Controls[1] == sender)
key = int.Parse(dg.DataKeys[i].ToString());
[...]
}


 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      03-04-2004

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.

 
Reply With Quote
 
G-Fit
Guest
Posts: n/a
 
      03-04-2004
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid Footer DropDownList OnSelectedIndexChanged ASP .Net 0 11-08-2007 02:08 PM
Datagrid - Dropdown OnSelectedIndexChanged Andy ASP .Net 1 09-28-2005 05:55 PM
OnSelectedIndexChanged event does not work with the selection on the checkbox ( on the cell of Datagrid control ) ? bienwell ASP .Net 2 03-09-2005 02:33 PM
how do i access a dropdownlists selected value in a datagrid edititemtemplate column from the selectedindexchanged event of another dropdownlist in a datagrid edititemtemplate column Dave M ASP .Net Datagrid Control 0 12-14-2004 11:53 AM
OnSelectedIndexChanged for a RadioButtonList in a DataGrid DotNetGruven ASP .Net 6 02-03-2004 03:58 PM



Advertisments