DataGrid with DropDownLists not responding to OnSelectedIndexChanged

Discussion in 'ASP .Net Datagrid Control' started by G-Fit, Mar 2, 2004.

  1. G-Fit

    G-Fit Guest

    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
    G-Fit, Mar 2, 2004
    #1
    1. Advertising

  2. 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.
    Jeffrey Tan[MSFT], Mar 3, 2004
    #2
    1. Advertising

  3. G-Fit

    G-Fit Guest

    > 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-Fit, Mar 3, 2004
    #3
  4. G-Fit

    G-Fit Guest

    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-Fit, Mar 3, 2004
    #4
  5. G-Fit

    G-Fit Guest

    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());
    [...]
    }
    G-Fit, Mar 3, 2004
    #5
  6. 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.
    Jeffrey Tan[MSFT], Mar 4, 2004
    #6
  7. G-Fit

    G-Fit Guest

    > 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
    G-Fit, Mar 4, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Julie Barnet

    OnSelectedIndexChanged not firing

    Julie Barnet, Aug 20, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    8,980
    Yusuf D M
    Aug 21, 2003
  2. DotNetGruven
    Replies:
    6
    Views:
    4,319
    DotNetGruven
    Feb 3, 2004
  3. Alexander Reichman

    Dropdownlist onselectedIndexChanged event not fires

    Alexander Reichman, Oct 17, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    23,315
  4. Andy
    Replies:
    1
    Views:
    3,954
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Sep 28, 2005
  5. Guest
    Replies:
    0
    Views:
    446
    Guest
    Nov 8, 2007
Loading...

Share This Page