472,805 Members | 860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

DropDownList.SelectedIndexChanged Not Consistent

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(groupMember ID);
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.

Nov 20 '06 #1
2 3230
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?

jn****@gmail.com wrote:
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(groupMember ID);
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.
Nov 20 '06 #2
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.

Nov 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Donal | last post by:
I have 3 related dropdowns. When the 1st is changed, the 2nd is updated, and when the 2nd is changed, the 3rd is updated. When i change the 1st dropdown (sites), the SelectedIndexChanged fires...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
2
by: Shiju Poyilil | last post by:
Hi ! I have a requirement wherein i am binding a datalist which contains a label (Caption for the field) and some literal hidden fields and a dropdown list. When I am binding to the datalist.....
0
by: Tand35006 | last post by:
Hi, I hope some one can help with this. I have a basic webform with 2 DropDownLists and a single DataGrid. What I am trying to do is populate the first DDList from a dataset on Form_Load. I then...
1
by: sergeyr3 | last post by:
Hey guys, I am new here, so i hope this works out: I have a datagrid which I populate with data from XML file. In EditItemTemplate I have a dropdownlist. How do I fire myDataGrid_UpdateCommand...
4
by: David | last post by:
Hi all, I am doing this in a web page... I have a dropdownlist that autopostback. This sets me a filter criteria for items to display in a datalist. In the datalist, I have edit...
11
by: Santosh | last post by:
Dear all , i am writting following code. if(Page.IsPostBack==false) { try { BindSectionDropDownlist();
5
by: Victorious1 | last post by:
Why doesn't the the SelectedIndexchanged event for my dropdownlist ever get executed? Why do I get a page not found error when I select a new item? My dropdownlist is within a form. The items...
5
by: revbart | last post by:
Yep, that's me. I'll bet I've read a hundred articles somewhere or another, but I just can't get the thing to work. I'm working on a custom solution. One of the major UIs includes a calendar-style...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.