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
ataList 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
ropDownListWithCommandEvent
ID="GroupMemberDropDownList" AutoPostBack="true"
DataValueField="SERVER_ARRAY_ID" DataTextField="SCHOOL_NAME"
runat="server"></cc1
ropDownListWithCommandEvent>
<asp:Button ID="GoButton" Text="Go" CommandArgument='<%#
Eval("SERVER_GROUP_ID") %>' CommandName="Button" runat="server" />
</ItemTemplate>
</asp
ataList>
<asp:ObjectDataSource ID="GroupDataSource" runat="server"
DeleteMethod="DeleteServerGroup"
InsertMethod="AddServerGroup"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetServerGroups"
TypeName="ServerGroupBLL" UpdateMethod="UpdateServerGroup">
<DeleteParameters>
<asp
arameter Name="serverGroupID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp
arameter Name="serverGroupID" Type="Int32" />
<asp
arameter Name="groupName" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp
arameter 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}
ropDownListWithCommandEvent
runat=server></{0}
ropDownListWithCommandEvent>")]
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.
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
![Big Grin :D :D](/styles/default/custom/smilies/grin.gif)
DataSourceID="GroupDataSource" OnItemCommand="Navigate" runat="server"
OnItemDataBound="GroupDataList_ItemDataBound">
<ItemTemplate>
<asp:Label ID="Label1" Text='<%# Eval("NAME") %>'
runat="server"></asp:Label><cc1
![Big Grin :D :D](/styles/default/custom/smilies/grin.gif)
ID="GroupMemberDropDownList" AutoPostBack="true"
DataValueField="SERVER_ARRAY_ID" DataTextField="SCHOOL_NAME"
runat="server"></cc1
![Big Grin :D :D](/styles/default/custom/smilies/grin.gif)
<asp:Button ID="GoButton" Text="Go" CommandArgument='<%#
Eval("SERVER_GROUP_ID") %>' CommandName="Button" runat="server" />
</ItemTemplate>
</asp
![Big Grin :D :D](/styles/default/custom/smilies/grin.gif)
<asp:ObjectDataSource ID="GroupDataSource" runat="server"
DeleteMethod="DeleteServerGroup"
InsertMethod="AddServerGroup"
OldValuesParameterFormatString="original_{0}"
SelectMethod="GetServerGroups"
TypeName="ServerGroupBLL" UpdateMethod="UpdateServerGroup">
<DeleteParameters>
<asp
![Stick Out Tongue :P :P](/styles/default/custom/smilies/tongue.gif)
</DeleteParameters>
<UpdateParameters>
<asp
![Stick Out Tongue :P :P](/styles/default/custom/smilies/tongue.gif)
<asp
![Stick Out Tongue :P :P](/styles/default/custom/smilies/tongue.gif)
</UpdateParameters>
<InsertParameters>
<asp
![Stick Out Tongue :P :P](/styles/default/custom/smilies/tongue.gif)
</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}
![Big Grin :D :D](/styles/default/custom/smilies/grin.gif)
runat=server></{0}
![Big Grin :D :D](/styles/default/custom/smilies/grin.gif)
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.