By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

DataGrid ItemCommand event not firing if not first page

P: n/a
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>
<asp:DropDownList 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>
</asp:DropDownList>
</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
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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>
<asp:DropDownList 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>
</asp:DropDownList>
</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

Nov 19 '05 #2

P: n/a
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.

Nov 19 '05 #3

P: n/a
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.

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.