DropDownList.SelectedIndexChanged Not Consistent

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jnoody@gmail.com

    #1

    DropDownList.SelectedIndexChanged Not Consistent

    The problem I am having is with the SelectedIndexCh anged event not
    always firing or the SelectedIndex property not being correct when the
    event does fire. The code is below, but here are some details first.

    The DropDownList is actually a custom control called
    DropDownListWit hCommandEvent that inherits from DropDownList. The
    reason I have created this is to create a DropDownList that will bubble
    a Command event to the containing DataList (code below).

    So I have a DataList that is bound to an ObjectDataSourc e. In the
    ItemTemplate of the DataList is where I have my
    DropDownListWit hCommandEvent. So for every row in the DataList, there
    will be a corresponding DropDownListWit hCommandEvent.

    Can anyone figure out why the SelectedIndexCh anged is not firing
    properly always and why the page or view state is losing track of the
    selected index for each DropDownListWit hCommand event?

    Markup:

    <%@ Page Language="C#" MasterPageFile= "~/PSPortal.master "
    AutoEventWireup ="true" CodeFile="~/Admin.aspx.cs" Inherits="Admin "
    Title="" %>

    <%@ Register Assembly="SIS.C ontrols, Version=1.0.0.0 , Culture=neutral ,
    PublicKeyToken= e2b1a6988a92c70 c"
    Namespace="SIS. Controls" TagPrefix="cc1" %>
    <asp:Content ID="Content1" ContentPlaceHol derID="ContentP laceHolder1"
    Runat="Server">
    <asp:DataList ID="GroupDataLi st" DataKeyField="S ERVER_GROUP_ID"
    DataSourceID="G roupDataSource" OnItemCommand=" Navigate" runat="server"
    OnItemDataBound ="GroupDataList _ItemDataBound" >
    <ItemTemplate >
    <asp:Label ID="Label1" Text='<%# Eval("NAME") %>'
    runat="server"> </asp:Label><cc1: DropDownListWit hCommandEvent
    ID="GroupMember DropDownList" AutoPostBack="t rue"
    DataValueField= "SERVER_ARRAY_I D" DataTextField=" SCHOOL_NAME"
    runat="server"> </cc1:DropDownLis tWithCommandEve nt>
    <asp:Button ID="GoButton" Text="Go" CommandArgument ='<%#
    Eval("SERVER_GR OUP_ID") %>' CommandName="Bu tton" runat="server" />
    </ItemTemplate>
    </asp:DataList>
    <asp:ObjectData Source ID="GroupDataSo urce" runat="server"
    DeleteMethod="D eleteServerGrou p"
    InsertMethod="A ddServerGroup"
    OldValuesParame terFormatString ="original_{ 0}"
    SelectMethod="G etServerGroups"
    TypeName="Serve rGroupBLL" UpdateMethod="U pdateServerGrou p">
    <DeleteParamete rs>
    <asp:Paramete r Name="serverGro upID" Type="Int32" />
    </DeleteParameter s>
    <UpdateParamete rs>
    <asp:Paramete r Name="serverGro upID" Type="Int32" />
    <asp:Paramete r Name="groupName " Type="String" />
    </UpdateParameter s>
    <InsertParamete rs>
    <asp:Paramete r Name="groupName " Type="String" />
    </InsertParameter s>
    </asp:ObjectDataS ource>
    </asp:Content>

    CodeBehind:

    using System;
    using SIS.Controls;
    using System.Data;
    using System.Configur ation;
    using System.Collecti ons;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Web.UI.H tmlControls;

    public partial class Admin : System.Web.UI.P age
    {
    protected void Navigate(object sender, DataListCommand EventArgs
    e)
    {
    DropDownListWit hCommandEvent ddl =
    (DropDownListWi thCommandEvent) e.Item.FindCont rol("GroupMembe rDropDownList") ;

    }

    protected void GroupDataList_I temDataBound(ob ject sender,
    DataListItemEve ntArgs e)
    {
    SIS.Controls.Dr opDownListWithC ommandEvent ddl =
    (SIS.Controls.D ropDownListWith CommandEvent)e. Item.FindContro l("GroupMemberD ropDownList");
    ServerGroupMemb ersBLL memberBLL = new
    ServerGroupMemb ersBLL();
    int groupMemberID =
    (int)GroupDataL ist.DataKeys[e.Item.ItemInde x];
    ddl.Items.Clear ();
    ddl.Items.Add("[SELECT YOUR SCHOOL]");
    ddl.AppendDataB oundItems = true;
    ddl.DataSource =
    memberBLL.GetSe rverGroupMember sByGroup(groupM emberID);
    UpdateDropDowns (ddl);
    }

    protected void UpdateDropDowns (DropDownListWi thCommandEvent
    ddl)
    {
    ddl.DataBind();
    }
    }
    }

    Custom Control class:

    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.W ebControls;

    namespace SIS.Controls
    {
    [DefaultProperty ("Text")]
    [ToolboxData("<{ 0}:DropDownList WithCommandEven t
    runat=server></{0}:DropDownLis tWithCommandEve nt>")]
    public class DropDownListWit hCommandEvent : DropDownList
    {
    public event CommandEventHan dler Command;

    protected void OnCommand(Comma ndEventArgs e)
    {
    if (this.Command != null)
    {
    Command(this, e);
    }
    RaiseBubbleEven t(this, e);
    }

    protected override void OnSelectedIndex Changed(EventAr gs e)
    {
    base.OnSelected IndexChanged(e) ;
    this.OnCommand( new CommandEventArg s("DropDown", null));
    }
    }
    }


    Thanks in advance for any help.

  • jnoody@gmail.com

    #2
    Re: DropDownList.Se lectedIndexChan ged Not Consistent

    I have some more information from some debugging.

    The reason the SelectedIndexCh anged event is not firing is because the
    dropdown is passing the wrong index.

    It turns out that my page actually redirects to another application
    after discovering the SelectedIndexCh anged event. When the user clicks
    back, this poses a problem associated with the fact that the dropdown
    is still set to the last choice they made, but the viewstate shows it's
    previous state as the same as the most recent postback.

    BUT, after selecting a new option from the dropdown, the index still
    shows the same as the last index chosen, no matter what is actually
    selected!

    Why would the drop-down not post what the user had selected, regardless
    of viewstate?

    jnoody@gmail.co m wrote:
    The problem I am having is with the SelectedIndexCh anged event not
    always firing or the SelectedIndex property not being correct when the
    event does fire. The code is below, but here are some details first.
    >
    The DropDownList is actually a custom control called
    DropDownListWit hCommandEvent that inherits from DropDownList. The
    reason I have created this is to create a DropDownList that will bubble
    a Command event to the containing DataList (code below).
    >
    So I have a DataList that is bound to an ObjectDataSourc e. In the
    ItemTemplate of the DataList is where I have my
    DropDownListWit hCommandEvent. So for every row in the DataList, there
    will be a corresponding DropDownListWit hCommandEvent.
    >
    Can anyone figure out why the SelectedIndexCh anged is not firing
    properly always and why the page or view state is losing track of the
    selected index for each DropDownListWit hCommand event?
    >
    Markup:
    >
    <%@ Page Language="C#" MasterPageFile= "~/PSPortal.master "
    AutoEventWireup ="true" CodeFile="~/Admin.aspx.cs" Inherits="Admin "
    Title="" %>
    >
    <%@ Register Assembly="SIS.C ontrols, Version=1.0.0.0 , Culture=neutral ,
    PublicKeyToken= e2b1a6988a92c70 c"
    Namespace="SIS. Controls" TagPrefix="cc1" %>
    <asp:Content ID="Content1" ContentPlaceHol derID="ContentP laceHolder1"
    Runat="Server">
    <asp:DataList ID="GroupDataLi st" DataKeyField="S ERVER_GROUP_ID"
    DataSourceID="G roupDataSource" OnItemCommand=" Navigate" runat="server"
    OnItemDataBound ="GroupDataList _ItemDataBound" >
    <ItemTemplate >
    <asp:Label ID="Label1" Text='<%# Eval("NAME") %>'
    runat="server"> </asp:Label><cc1: DropDownListWit hCommandEvent
    ID="GroupMember DropDownList" AutoPostBack="t rue"
    DataValueField= "SERVER_ARRAY_I D" DataTextField=" SCHOOL_NAME"
    runat="server"> </cc1:DropDownLis tWithCommandEve nt>
    <asp:Button ID="GoButton" Text="Go" CommandArgument ='<%#
    Eval("SERVER_GR OUP_ID") %>' CommandName="Bu tton" runat="server" />
    </ItemTemplate>
    </asp:DataList>
    <asp:ObjectData Source ID="GroupDataSo urce" runat="server"
    DeleteMethod="D eleteServerGrou p"
    InsertMethod="A ddServerGroup"
    OldValuesParame terFormatString ="original_{ 0}"
    SelectMethod="G etServerGroups"
    TypeName="Serve rGroupBLL" UpdateMethod="U pdateServerGrou p">
    <DeleteParamete rs>
    <asp:Paramete r Name="serverGro upID" Type="Int32" />
    </DeleteParameter s>
    <UpdateParamete rs>
    <asp:Paramete r Name="serverGro upID" Type="Int32" />
    <asp:Paramete r Name="groupName " Type="String" />
    </UpdateParameter s>
    <InsertParamete rs>
    <asp:Paramete r Name="groupName " Type="String" />
    </InsertParameter s>
    </asp:ObjectDataS ource>
    </asp:Content>
    >
    CodeBehind:
    >
    using System;
    using SIS.Controls;
    using System.Data;
    using System.Configur ation;
    using System.Collecti ons;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Web.UI.H tmlControls;
    >
    public partial class Admin : System.Web.UI.P age
    {
    protected void Navigate(object sender, DataListCommand EventArgs
    e)
    {
    DropDownListWit hCommandEvent ddl =
    (DropDownListWi thCommandEvent) e.Item.FindCont rol("GroupMembe rDropDownList") ;
    >
    }
    >
    protected void GroupDataList_I temDataBound(ob ject sender,
    DataListItemEve ntArgs e)
    {
    SIS.Controls.Dr opDownListWithC ommandEvent ddl =
    (SIS.Controls.D ropDownListWith CommandEvent)e. Item.FindContro l("GroupMemberD ropDownList");
    ServerGroupMemb ersBLL memberBLL = new
    ServerGroupMemb ersBLL();
    int groupMemberID =
    (int)GroupDataL ist.DataKeys[e.Item.ItemInde x];
    ddl.Items.Clear ();
    ddl.Items.Add("[SELECT YOUR SCHOOL]");
    ddl.AppendDataB oundItems = true;
    ddl.DataSource =
    memberBLL.GetSe rverGroupMember sByGroup(groupM emberID);
    UpdateDropDowns (ddl);
    }
    >
    protected void UpdateDropDowns (DropDownListWi thCommandEvent
    ddl)
    {
    ddl.DataBind();
    }
    }
    }
    >
    Custom Control class:
    >
    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    >
    namespace SIS.Controls
    {
    [DefaultProperty ("Text")]
    [ToolboxData("<{ 0}:DropDownList WithCommandEven t
    runat=server></{0}:DropDownLis tWithCommandEve nt>")]
    public class DropDownListWit hCommandEvent : DropDownList
    {
    public event CommandEventHan dler Command;
    >
    protected void OnCommand(Comma ndEventArgs e)
    {
    if (this.Command != null)
    {
    Command(this, e);
    }
    RaiseBubbleEven t(this, e);
    }
    >
    protected override void OnSelectedIndex Changed(EventAr gs e)
    {
    base.OnSelected IndexChanged(e) ;
    this.OnCommand( new CommandEventArg s("DropDown", null));
    }
    }
    }
    >
    >
    Thanks in advance for any help.

    Comment

    • jnoody@gmail.com

      #3
      Re: DropDownList.Se lectedIndexChan ged Not Consistent

      I disabled viewstate on the DataList and the dropdowns and tried
      tracking selected value changes myself by stored the selected indices
      in the Session state and checking on every postback to see if they had
      changed and I'm basically having the same issue. If I selected a
      choice from the dropdown, it causes a redirect. If i hit the back
      button and choose a different value from the drop-down, it just posts
      the previous index.

      So if I chose index 1 from the drop-down on the first load and then
      clicked back after getting redirected, and then I chose index 9, it
      still posts index 1.

      Comment

      Working...