Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DataGrid ItemCommand event not firing if not first page

Reply
Thread Tools

DataGrid ItemCommand event not firing if not first page

 
 
=?Utf-8?B?ZGFuYw==?=
Guest
Posts: n/a
 
      10-26-2005
I have a datagrid with a checkbox and dropdown list in each row. Both set
AutoPostBack to true and ItemCommand and OnSelectedIndexChanged events for
these controls works fine when DataGrid is not paged. As soon as I turn on
paging support, I no longer get events properly for any page except the
first.

1) The DataGrid displays the page numbers on the top (and bottom). Whenever
I click to go to a specific page number, the DropDownList's
OnSelectedIndexChanged gets called (which is unexpected and bad), followed by
ItemCommand( "Page" ) (which is expected and good).

2) Whenever I click on the checkbox while on any page but the first,
instead of an ItemCommand with the checkbox CommandName, the DropDownList's
OnSelectedItemChanged event gets called.

This is driving me insane. I have no code that is conditional on the current
page.

Here is the relevant page code:

<DataGrid id="dgrdTestNumbers" runat="server" AutoGenerateColumns="False"
AllowPaging="True" PageSize="40">
<PagerStyle NextPageText="Next &gt;&gt;" PrevPageText="Previous &lt;&lt;"
Position="TopAndBottom" PageButtonCount="40" Mode="NumericPages"></PagerStyle>
<Columns>
<asp:BoundColumn DataField="DESCRIPTION"
HeaderText="Type"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Day">
<ItemTemplate>
<aspropDownList id="lstTestDay" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="lstTestDay_SelectedIndexCh anged">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
</aspropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<cc1:ISCCheckboxButtonColumn AutoPostBack="True" HeaderText="RFU"
CommandName="RFU"></cc1:ISCCheckboxButtonColumn>
</Columns>
</DataGrid>


Here is the relevant code (I have stripped out error handling and other
stuff):

public class _Default : CommonBasePage
{
protected ISCTools.Web.ISCDataGrid dgrdTestNumbers;
protected System.Web.UI.WebControls.Button btnRefresh;

const int tcRFU = 5;

private void Page_Load(object sender, System.EventArgs e)
{
if ( !Page.IsPostBack )
{
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
this.dgrdTestNumbers.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dgrdTestNumbers_ItemCommand);
this.dgrdTestNumbers.PageIndexChanged += new
System.Web.UI.WebControls.DataGridPageChangedEvent Handler(this.dgrdTestNumbers_PageIndexChanged);
this.dgrdTestNumbers.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler (this.dgrdTestNumbers_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion




private void BindDataGrid()
{
DataTable data = Bus.GetRecordsFromDatabase();
dgrdTestNumbers.DataSource = data;
dgrdTestNumbers.DataKeyField = "ID";
dgrdTestNumbers.DataBind();
}


private void btnRefresh_Click(object sender, System.EventArgs e)
{
dgrdTestNumbers.CurrentPageIndex = 0;
BindDataGrid();
}


private void dgrdTestNumbers_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if ( e.CommandName == "RFU" )
{
CheckBox chkRFU = (CheckBox) e.Item.Cells[ tcRFU ].Controls[0];
int testNumberId = Convert.ToInt32( dgrdTestNumbers.DataKeys[
e.Item.ItemIndex ] );
Bus.UpdateTestNumberRFU( testNumberId, chkRFU.Checked );
BindDataGrid();
}
}


protected void lstTestDay_SelectedIndexChanged(object sender,
System.EventArgs e)
{
DropDownList lstTestDay = (DropDownList)sender;
DataGridItem item = (DataGridItem) lstTestDay.Parent.Parent;

int intDataSetIndex = item.DataSetIndex -
(dgrdTestNumbers.CurrentPageIndex * dgrdTestNumbers.PageSize);
int testNumberId = Convert.ToInt32( dgrdTestNumbers.DataKeys[
intDataSetIndex ] );

int newTestDay = Convert.ToInt32( lstTestDay.SelectedItem.Value );
Bus.UpdateTestNumberTestDay( testNumberId, newTestDay );

BindDataGrid();
}


private void dgrdTestNumbers_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if ( e.Item.ItemType != System.Web.UI.WebControls.ListItemType.Header &&
e.Item.ItemType != System.Web.UI.WebControls.ListItemType.Footer )
{
// Check the RFU checkbox based on the current record
bool rfu = Convert.ToBoolean( DataBinder.Eval(e.Item.DataItem, "RFU") );
if ( rfu )
{
CheckBox chkRFU = (CheckBox) e.Item.Cells[ tcRFU ].Controls[0];
chkRFU.Checked = true;
}

// Set the DropDownList value based on the current record
DropDownList lstTestDay = (DropDownList) e.Item.FindControl( "lstTestDay"
);
if ( lstTestDay != null )
{
int testDay = Convert.ToInt32( DataBinder.Eval( e.Item.DataItem,
"TEST_DAY" ) );
ISCTools.Web.WebUtil.SelectListItemByText( lstTestDay,
testDay.ToString() );
}
}
}

private void dgrdTestNumbers_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEvent Args e)
{
dgrdTestNumbers.CurrentPageIndex = e.NewPageIndex;
BindDataGrid();
}
}


Thanks in advance,

Dan
 
Reply With Quote
 
 
 
 
=?Utf-8?B?ZGFuYw==?=
Guest
Posts: n/a
 
      10-26-2005
The following trace of what happens when I click on a checkbox might also
help to see the problem:

Any checkbox on page 1:

Begin Init
End Init
Begin LoadViewState
End LoadViewState
Begin ProcessPostData
End ProcessPostData
Begin Page_Load
End Page_Load
Begin ProcessPostData Second Try
End ProcessPostData Second Try
Begin Raise ChangedEvents
Begin dgrdTestNumbers_ItemCommand
Begin LoadTestNumbers
End LoadTestNumbers
End dgrdTestNumbers_ItemCommand
End Raise ChangedEvents
Begin Raise PostBackEvent
End Raise PostBackEvent
Begin PreRender
End PreRender
Begin SaveViewState
End SaveViewState
Begin Render
End Render

Any checkbox on page 2:

Begin Init
End Init
Begin LoadViewState
End LoadViewState
Begin ProcessPostData
End ProcessPostData
Begin Page_Load
End Page_Load
Begin ProcessPostData Second Try
End ProcessPostData Second Try
Begin Raise ChangedEvents
Begin lstTestDay_SelectedIndexChanged
Begin LoadTestNumbers
End LoadTestNumbers
End lstTestDay_SelectedIndexChanged
End Raise ChangedEvents
Begin Raise PostBackEvent
End Raise PostBackEvent
Begin PreRender
End PreRender
Begin SaveViewState
End SaveViewState
Begin Render
End Render


"danc" wrote:

> I have a datagrid with a checkbox and dropdown list in each row. Both set
> AutoPostBack to true and ItemCommand and OnSelectedIndexChanged events for
> these controls works fine when DataGrid is not paged. As soon as I turn on
> paging support, I no longer get events properly for any page except the
> first.
>
> 1) The DataGrid displays the page numbers on the top (and bottom). Whenever
> I click to go to a specific page number, the DropDownList's
> OnSelectedIndexChanged gets called (which is unexpected and bad), followed by
> ItemCommand( "Page" ) (which is expected and good).
>
> 2) Whenever I click on the checkbox while on any page but the first,
> instead of an ItemCommand with the checkbox CommandName, the DropDownList's
> OnSelectedItemChanged event gets called.
>
> This is driving me insane. I have no code that is conditional on the current
> page.
>
> Here is the relevant page code:
>
> <DataGrid id="dgrdTestNumbers" runat="server" AutoGenerateColumns="False"
> AllowPaging="True" PageSize="40">
> <PagerStyle NextPageText="Next >>" PrevPageText="Previous <<"
> Position="TopAndBottom" PageButtonCount="40" Mode="NumericPages"></PagerStyle>
> <Columns>
> <asp:BoundColumn DataField="DESCRIPTION"
> HeaderText="Type"></asp:BoundColumn>
> <asp:TemplateColumn HeaderText="Day">
> <ItemTemplate>
> <aspropDownList id="lstTestDay" runat="server" AutoPostBack="True"
> OnSelectedIndexChanged="lstTestDay_SelectedIndexCh anged">
> <asp:ListItem Value="1">1</asp:ListItem>
> <asp:ListItem Value="2">2</asp:ListItem>
> <asp:ListItem Value="3">3</asp:ListItem>
> <asp:ListItem Value="4">4</asp:ListItem>
> </aspropDownList>
> </ItemTemplate>
> </asp:TemplateColumn>
> <cc1:ISCCheckboxButtonColumn AutoPostBack="True" HeaderText="RFU"
> CommandName="RFU"></cc1:ISCCheckboxButtonColumn>
> </Columns>
> </DataGrid>
>
>
> Here is the relevant code (I have stripped out error handling and other
> stuff):
>
> public class _Default : CommonBasePage
> {
> protected ISCTools.Web.ISCDataGrid dgrdTestNumbers;
> protected System.Web.UI.WebControls.Button btnRefresh;
>
> const int tcRFU = 5;
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> if ( !Page.IsPostBack )
> {
> }
> }
>
> #region Web Form Designer generated code
> override protected void OnInit(EventArgs e)
> {
> //
> // CODEGEN: This call is required by the ASP.NET Web Form Designer.
> //
> InitializeComponent();
> base.OnInit(e);
> }
>
> /// <summary>
> /// Required method for Designer support - do not modify
> /// the contents of this method with the code editor.
> /// </summary>
> private void InitializeComponent()
> {
> this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
> this.dgrdTestNumbers.ItemCommand += new
> System.Web.UI.WebControls.DataGridCommandEventHand ler(this.dgrdTestNumbers_ItemCommand);
> this.dgrdTestNumbers.PageIndexChanged += new
> System.Web.UI.WebControls.DataGridPageChangedEvent Handler(this.dgrdTestNumbers_PageIndexChanged);
> this.dgrdTestNumbers.ItemDataBound += new
> System.Web.UI.WebControls.DataGridItemEventHandler (this.dgrdTestNumbers_ItemDataBound);
> this.Load += new System.EventHandler(this.Page_Load);
>
> }
> #endregion
>
>
>
>
> private void BindDataGrid()
> {
> DataTable data = Bus.GetRecordsFromDatabase();
> dgrdTestNumbers.DataSource = data;
> dgrdTestNumbers.DataKeyField = "ID";
> dgrdTestNumbers.DataBind();
> }
>
>
> private void btnRefresh_Click(object sender, System.EventArgs e)
> {
> dgrdTestNumbers.CurrentPageIndex = 0;
> BindDataGrid();
> }
>
>
> private void dgrdTestNumbers_ItemCommand(object source,
> System.Web.UI.WebControls.DataGridCommandEventArgs e)
> {
> if ( e.CommandName == "RFU" )
> {
> CheckBox chkRFU = (CheckBox) e.Item.Cells[ tcRFU ].Controls[0];
> int testNumberId = Convert.ToInt32( dgrdTestNumbers.DataKeys[
> e.Item.ItemIndex ] );
> Bus.UpdateTestNumberRFU( testNumberId, chkRFU.Checked );
> BindDataGrid();
> }
> }
>
>
> protected void lstTestDay_SelectedIndexChanged(object sender,
> System.EventArgs e)
> {
> DropDownList lstTestDay = (DropDownList)sender;
> DataGridItem item = (DataGridItem) lstTestDay.Parent.Parent;
>
> int intDataSetIndex = item.DataSetIndex -
> (dgrdTestNumbers.CurrentPageIndex * dgrdTestNumbers.PageSize);
> int testNumberId = Convert.ToInt32( dgrdTestNumbers.DataKeys[
> intDataSetIndex ] );
>
> int newTestDay = Convert.ToInt32( lstTestDay.SelectedItem.Value );
> Bus.UpdateTestNumberTestDay( testNumberId, newTestDay );
>
> BindDataGrid();
> }
>
>
> private void dgrdTestNumbers_ItemDataBound(object sender,
> System.Web.UI.WebControls.DataGridItemEventArgs e)
> {
> if ( e.Item.ItemType != System.Web.UI.WebControls.ListItemType.Header &&
> e.Item.ItemType != System.Web.UI.WebControls.ListItemType.Footer )
> {
> // Check the RFU checkbox based on the current record
> bool rfu = Convert.ToBoolean( DataBinder.Eval(e.Item.DataItem, "RFU") );
> if ( rfu )
> {
> CheckBox chkRFU = (CheckBox) e.Item.Cells[ tcRFU ].Controls[0];
> chkRFU.Checked = true;
> }
>
> // Set the DropDownList value based on the current record
> DropDownList lstTestDay = (DropDownList) e.Item.FindControl( "lstTestDay"
> );
> if ( lstTestDay != null )
> {
> int testDay = Convert.ToInt32( DataBinder.Eval( e.Item.DataItem,
> "TEST_DAY" ) );
> ISCTools.Web.WebUtil.SelectListItemByText( lstTestDay,
> testDay.ToString() );
> }
> }
> }
>
> private void dgrdTestNumbers_PageIndexChanged(object source,
> System.Web.UI.WebControls.DataGridPageChangedEvent Args e)
> {
> dgrdTestNumbers.CurrentPageIndex = e.NewPageIndex;
> BindDataGrid();
> }
> }
>
>
> Thanks in advance,
>
> Dan

 
Reply With Quote
 
 
 
 
john_teague
Guest
Posts: n/a
 
      10-26-2005
Danc,
I'm not sure what is happening, but here is my initial analysis:
Looking at your trace statements, you will notice that on page 1
dgrdTestNumbers_ItemCommand is fired, while on page 2
lstTestDay_SelectedIndexChanged is fired.

This may be the cause, but I am not sure of the best fix.

 
Reply With Quote
 
=?Utf-8?B?ZGFuYw==?=
Guest
Posts: n/a
 
      10-26-2005
John,

Exactly. What I am wondering, is why is the wrong event handler being called
in the first place? I find it unlikely that I have run across a defect in
ASP.NET since this would be a big one; but I don't know what else could cause
this.

Cheers,

Dan

"john_teague" wrote:

> Danc,
> I'm not sure what is happening, but here is my initial analysis:
> Looking at your trace statements, you will notice that on page 1
> dgrdTestNumbers_ItemCommand is fired, while on page 2
> lstTestDay_SelectedIndexChanged is fired.
>
> This may be the cause, but I am not sure of the best fix.
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Event ItemCommand not firing in DataGrid (1.1) Andy ASP .Net 0 01-10-2007 12:07 AM
repeater ItemCommand not firing on first click =?Utf-8?B?Q3VydF9DIFtNVlBd?= ASP .Net 2 10-12-2005 03:18 PM
Datagrid ItemCommand Event Not Firing =?Utf-8?B?RGVlcGVzaA==?= ASP .Net 2 10-06-2005 12:46 AM
ImageButton not firing ItemCommand event for the datagrid John ASP .Net Datagrid Control 0 08-04-2005 02:13 PM
Dynamic Datagrid Creation - ItemCommand event not firing PD ASP .Net Datagrid Control 0 10-23-2003 03:48 PM



Advertisments