DataGrid ItemCommand event not firing if not first page

Discussion in 'ASP .Net' started by =?Utf-8?B?ZGFuYw==?=, Oct 26, 2005.

  1. 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_SelectedIndexChanged">
    <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.DataGridCommandEventHandler(this.dgrdTestNumbers_ItemCommand);
    this.dgrdTestNumbers.PageIndexChanged += new
    System.Web.UI.WebControls.DataGridPageChangedEventHandler(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.DataGridPageChangedEventArgs e)
    {
    dgrdTestNumbers.CurrentPageIndex = e.NewPageIndex;
    BindDataGrid();
    }
    }


    Thanks in advance,

    Dan
     
    =?Utf-8?B?ZGFuYw==?=, Oct 26, 2005
    #1
    1. Advertising

  2. 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_SelectedIndexChanged">
    > <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.DataGridCommandEventHandler(this.dgrdTestNumbers_ItemCommand);
    > this.dgrdTestNumbers.PageIndexChanged += new
    > System.Web.UI.WebControls.DataGridPageChangedEventHandler(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.DataGridPageChangedEventArgs e)
    > {
    > dgrdTestNumbers.CurrentPageIndex = e.NewPageIndex;
    > BindDataGrid();
    > }
    > }
    >
    >
    > Thanks in advance,
    >
    > Dan
     
    =?Utf-8?B?ZGFuYw==?=, Oct 26, 2005
    #2
    1. Advertising

  3. =?Utf-8?B?ZGFuYw==?=

    john_teague Guest

    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.
     
    john_teague, Oct 26, 2005
    #3
  4. 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.
    >
    >
     
    =?Utf-8?B?ZGFuYw==?=, Oct 26, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RGVlcGVzaA==?=

    Datagrid ItemCommand Event Not Firing

    =?Utf-8?B?RGVlcGVzaA==?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    5,836
    =?Utf-8?B?RGVlcGVzaA==?=
    Oct 6, 2005
  2. =?Utf-8?B?Q3VydF9DIFtNVlBd?=

    repeater ItemCommand not firing on first click

    =?Utf-8?B?Q3VydF9DIFtNVlBd?=, Oct 12, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,802
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 12, 2005
  3. Andy
    Replies:
    0
    Views:
    424
  4. PD
    Replies:
    0
    Views:
    388
  5. John

    ImageButton not firing ItemCommand event for the datagrid

    John, Aug 4, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    302
Loading...

Share This Page