DropDownList.SelectedIndexChanged Not Consistent


J

jnoody

The problem I am having is with the SelectedIndexChanged 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
DropDownListWithCommandEvent 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 ObjectDataSource. In the
ItemTemplate of the DataList is where I have my
DropDownListWithCommandEvent. So for every row in the DataList, there
will be a corresponding DropDownListWithCommandEvent.

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

Markup:

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

<%@ Register Assembly="SIS.Controls, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=e2b1a6988a92c70c"
Namespace="SIS.Controls" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:DataList ID="GroupDataList" DataKeyField="SERVER_GROUP_ID"
DataSourceID="GroupDataSource" OnItemCommand="Navigate" runat="server"
OnItemDataBound="GroupDataList_ItemDataBound">
<ItemTemplate>
<asp:Label ID="Label1" Text='<%# Eval("NAME") %>'
runat="server"></asp:Label><cc1:DropDownListWithCommandEvent
ID="GroupMemberDropDownList" AutoPostBack="true"
DataValueField="SERVER_ARRAY_ID" DataTextField="SCHOOL_NAME"
runat="server"></cc1:DropDownListWithCommandEvent>
<asp:Button ID="GoButton" Text="Go" CommandArgument='<%#
Eval("SERVER_GROUP_ID") %>' CommandName="Button" runat="server" />
</ItemTemplate>
</asp:DataList>
<asp:ObjectDataSource ID="GroupDataSource" runat="server"
DeleteMethod="DeleteServerGroup"
InsertMethod="AddServerGroup"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetServerGroups"
TypeName="ServerGroupBLL" UpdateMethod="UpdateServerGroup">
<DeleteParameters>
<asp:parameter Name="serverGroupID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="serverGroupID" Type="Int32" />
<asp:parameter Name="groupName" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="groupName" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>
</asp:Content>

CodeBehind:

using System;
using SIS.Controls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin : System.Web.UI.Page
{
protected void Navigate(object sender, DataListCommandEventArgs
e)
{
DropDownListWithCommandEvent ddl =
(DropDownListWithCommandEvent)e.Item.FindControl("GroupMemberDropDownList");

}

protected void GroupDataList_ItemDataBound(object sender,
DataListItemEventArgs e)
{
SIS.Controls.DropDownListWithCommandEvent ddl =
(SIS.Controls.DropDownListWithCommandEvent)e.Item.FindControl("GroupMemberDropDownList");
ServerGroupMembersBLL memberBLL = new
ServerGroupMembersBLL();
int groupMemberID =
(int)GroupDataList.DataKeys[e.Item.ItemIndex];
ddl.Items.Clear();
ddl.Items.Add("[SELECT YOUR SCHOOL]");
ddl.AppendDataBoundItems = true;
ddl.DataSource =
memberBLL.GetServerGroupMembersByGroup(groupMemberID);
UpdateDropDowns(ddl);
}

protected void UpdateDropDowns(DropDownListWithCommandEvent
ddl)
{
ddl.DataBind();
}
}
}

Custom Control class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SIS.Controls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:DropDownListWithCommandEvent
runat=server></{0}:DropDownListWithCommandEvent>")]
public class DropDownListWithCommandEvent : DropDownList
{
public event CommandEventHandler Command;

protected void OnCommand(CommandEventArgs e)
{
if (this.Command != null)
{
Command(this, e);
}
RaiseBubbleEvent(this, e);
}

protected override void OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged(e);
this.OnCommand(new CommandEventArgs("DropDown", null));
}
}
}


Thanks in advance for any help.
 
Ad

Advertisements

J

jnoody

I have some more information from some debugging.

The reason the SelectedIndexChanged 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 SelectedIndexChanged 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?

The problem I am having is with the SelectedIndexChanged 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
DropDownListWithCommandEvent 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 ObjectDataSource. In the
ItemTemplate of the DataList is where I have my
DropDownListWithCommandEvent. So for every row in the DataList, there
will be a corresponding DropDownListWithCommandEvent.

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

Markup:

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

<%@ Register Assembly="SIS.Controls, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=e2b1a6988a92c70c"
Namespace="SIS.Controls" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:DataList ID="GroupDataList" DataKeyField="SERVER_GROUP_ID"
DataSourceID="GroupDataSource" OnItemCommand="Navigate" runat="server"
OnItemDataBound="GroupDataList_ItemDataBound">
<ItemTemplate>
<asp:Label ID="Label1" Text='<%# Eval("NAME") %>'
runat="server"></asp:Label><cc1:DropDownListWithCommandEvent
ID="GroupMemberDropDownList" AutoPostBack="true"
DataValueField="SERVER_ARRAY_ID" DataTextField="SCHOOL_NAME"
runat="server"></cc1:DropDownListWithCommandEvent>
<asp:Button ID="GoButton" Text="Go" CommandArgument='<%#
Eval("SERVER_GROUP_ID") %>' CommandName="Button" runat="server" />
</ItemTemplate>
</asp:DataList>
<asp:ObjectDataSource ID="GroupDataSource" runat="server"
DeleteMethod="DeleteServerGroup"
InsertMethod="AddServerGroup"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetServerGroups"
TypeName="ServerGroupBLL" UpdateMethod="UpdateServerGroup">
<DeleteParameters>
<asp:parameter Name="serverGroupID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="serverGroupID" Type="Int32" />
<asp:parameter Name="groupName" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="groupName" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>
</asp:Content>

CodeBehind:

using System;
using SIS.Controls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin : System.Web.UI.Page
{
protected void Navigate(object sender, DataListCommandEventArgs
e)
{
DropDownListWithCommandEvent ddl =
(DropDownListWithCommandEvent)e.Item.FindControl("GroupMemberDropDownList");

}

protected void GroupDataList_ItemDataBound(object sender,
DataListItemEventArgs e)
{
SIS.Controls.DropDownListWithCommandEvent ddl =
(SIS.Controls.DropDownListWithCommandEvent)e.Item.FindControl("GroupMemberDropDownList");
ServerGroupMembersBLL memberBLL = new
ServerGroupMembersBLL();
int groupMemberID =
(int)GroupDataList.DataKeys[e.Item.ItemIndex];
ddl.Items.Clear();
ddl.Items.Add("[SELECT YOUR SCHOOL]");
ddl.AppendDataBoundItems = true;
ddl.DataSource =
memberBLL.GetServerGroupMembersByGroup(groupMemberID);
UpdateDropDowns(ddl);
}

protected void UpdateDropDowns(DropDownListWithCommandEvent
ddl)
{
ddl.DataBind();
}
}
}

Custom Control class:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SIS.Controls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:DropDownListWithCommandEvent
runat=server></{0}:DropDownListWithCommandEvent>")]
public class DropDownListWithCommandEvent : DropDownList
{
public event CommandEventHandler Command;

protected void OnCommand(CommandEventArgs e)
{
if (this.Command != null)
{
Command(this, e);
}
RaiseBubbleEvent(this, e);
}

protected override void OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged(e);
this.OnCommand(new CommandEventArgs("DropDown", null));
}
}
}


Thanks in advance for any help.
 
Ad

Advertisements

J

jnoody

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.
 

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

Top