473,385 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 3273
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.