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.
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