OnItemCreated fires twice

Discussion in 'ASP .Net Datagrid Control' started by Douglas Osborne, Jun 29, 2004.

  1. All,

    On the initial load of my grid, the OnItemCreated event fires once. However,
    when any other grid event fires - sorting, paging - the OnItemCreated event
    fires twice. How can I prevent the double firing? How do I need to handle
    the event processing - can I prevent the second OnItemCreated from being
    invoked?

    TIA,
    Doug
     
    Douglas Osborne, Jun 29, 2004
    #1
    1. Advertising

  2. All,

    It may be because of have an OnItemCreated in the grid, and a similar event on my web page - here's the one on the grid

    protected override void OnItemCreated(System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    //* Create the message and seperator for the pager control.
    if ( e.Item.ItemType == ListItemType.Pager )
    {
    //* Variables for use in calculating the available items.
    int lowerItemNumber = 0;
    int upperItemNumber = 0;

    //* Build the pager message part.
    //* If this grid uses built-in paging mechanisms, the information needed to create the label will be available
    //* internally. Otherwise, we must examine the externally available properties, (LowerItemNumber, UpperItemNumber,
    //* and TotalItemCount)

    Label lblPagerMessage = new Label();
    lblPagerMessage.CssClass = "Text9pt";

    //* Make sure the user set the appropriate properties.
    if (this.Items.Count > 0 )
    {
    lowerItemNumber = (1 + (this.PageSize * this.CurrentPageIndex));
    upperItemNumber = ((this.PageSize * this.CurrentPageIndex) + this.Items.Count);
    int TP = this.VirtualItemCount/this.PageSize;
    if ( (TP * this.PageSize) != this.VirtualItemCount )
    {
    TP += 1;
    }
    lblPagerMessage.Text = String.Format("Displaying {5} records Per Page - Display Records {0} to {1} of {2} | Page {3} of {4} | ",
    lowerItemNumber,
    ((this.PageSize * this.CurrentPageIndex) + this.Items.Count),
    this.VirtualItemCount,
    this.CurrentPageIndex + 1,
    TP,
    this.PageSize);
    }
    else
    {
    lblPagerMessage.Text = this.emptyMessage + " | ";
    }

    //* Get the controls within the pager into an array then clear the pager out.
    ArrayList aControls = new ArrayList();
    foreach ( Control c in e.Item.Cells[0].Controls )
    {
    aControls.Add(c);
    }
    e.Item.Cells[0].Controls.Clear();

    //* Re-insert controls including message label and item seperators.
    e.Item.Cells[0].Controls.Add(lblPagerMessage);
    for ( int i=0; i<aControls.Count; i++)
    {
    Control c = (Control)aControls;
    //* If we are between the "Previous" and the "Last" buttons, or
    //* if this control uses numbers as well, insert pipes between each link.
    if (i == aControls.Count - 2)
    {
    Label oLine = new Label();
    oLine.Text = " | ";
    oLine.CssClass = "Text9pt";
    e.Item.Cells[0].Controls.Add(oLine);
    }

    //* Make the Next and Previous buttons
    if (c is LinkButton || c is Label)
    {
    ((WebControl)c).CssClass = "Text9ptBlue";
    }
    e.Item.Cells[0].Controls.Add(c);
    }
    }
    base.OnItemCreated(e);
    }
    Here's the one on the web page:
    public void grdNetwork_People_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem )
    {
    e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#C0C0C0';this.style.cursor='hand'");
    e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='" + (e.Item.ItemType == ListItemType.Item ? "#FFFFE0" : "#FFFFFF") + "';");

    if ( e.Item.DataItem != null )
    {
    //e.Item.Cells[0].ToString();
    string PK = ((DataRowView)(e.Item.DataItem)).Row.ItemArray[0].ToString();

    e.Item.Attributes.Add("onclick", "ViewService('" + PK + "')");
    }
    }
    }

    How can I interleave the two - and not have the ListItemType.Pager item be gnerated more than once? Can I - or is this just how the events bubble up?

    Doug
    "Douglas Osborne" <Doug_period_Osborne_at_CardioNet_period_com> wrote in message news:<>...
    > All,
    >
    > On the initial load of my grid, the OnItemCreated event fires once.
    > However, when any other grid event fires - sorting, paging - the
    > OnItemCreated event fires twice. How can I prevent the double firing?
    > How do I need to handle the event processing - can I prevent the
    > second OnItemCreated from being invoked?
    >
    > TIA,
    > Doug
    >
    >


    "Douglas Osborne" <Doug_period_Osborne_at_CardioNet_period_com> wrote in message news:...
    > All,
    >
    > On the initial load of my grid, the OnItemCreated event fires once. However,
    > when any other grid event fires - sorting, paging - the OnItemCreated event
    > fires twice. How can I prevent the double firing? How do I need to handle
    > the event processing - can I prevent the second OnItemCreated from being
    > invoked?
    >
    > TIA,
    > Doug
    >
    >
     
    Douglas Osborne, Jun 29, 2004
    #2
    1. Advertising

  3. Douglas Osborne

    Prodip Saha Guest

    As a hits....
    You probably have the same event method assigned in the .aspx file. If you set the SortCommand in the aspx file and you implement the same event in the code behind file(.cs) the event will fire twice.

    Prodip
    "Douglas Osborne" <Doug_period_Osborne_at_CardioNet_period_com> wrote in message news:...
    All,

    It may be because of have an OnItemCreated in the grid, and a similar event on my web page - here's the one on the grid

    protected override void OnItemCreated(System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    //* Create the message and seperator for the pager control.
    if ( e.Item.ItemType == ListItemType.Pager )
    {
    //* Variables for use in calculating the available items.
    int lowerItemNumber = 0;
    int upperItemNumber = 0;

    //* Build the pager message part.
    //* If this grid uses built-in paging mechanisms, the information needed to create the label will be available
    //* internally. Otherwise, we must examine the externally available properties, (LowerItemNumber, UpperItemNumber,
    //* and TotalItemCount)

    Label lblPagerMessage = new Label();
    lblPagerMessage.CssClass = "Text9pt";

    //* Make sure the user set the appropriate properties.
    if (this.Items.Count > 0 )
    {
    lowerItemNumber = (1 + (this.PageSize * this.CurrentPageIndex));
    upperItemNumber = ((this.PageSize * this.CurrentPageIndex) + this.Items.Count);
    int TP = this.VirtualItemCount/this.PageSize;
    if ( (TP * this.PageSize) != this.VirtualItemCount )
    {
    TP += 1;
    }
    lblPagerMessage.Text = String.Format("Displaying {5} records Per Page - Display Records {0} to {1} of {2} | Page {3} of {4} | ",
    lowerItemNumber,
    ((this.PageSize * this.CurrentPageIndex) + this.Items.Count),
    this.VirtualItemCount,
    this.CurrentPageIndex + 1,
    TP,
    this.PageSize);
    }
    else
    {
    lblPagerMessage.Text = this.emptyMessage + " | ";
    }

    //* Get the controls within the pager into an array then clear the pager out.
    ArrayList aControls = new ArrayList();
    foreach ( Control c in e.Item.Cells[0].Controls )
    {
    aControls.Add(c);
    }
    e.Item.Cells[0].Controls.Clear();

    //* Re-insert controls including message label and item seperators.
    e.Item.Cells[0].Controls.Add(lblPagerMessage);
    for ( int i=0; i<aControls.Count; i++)
    {
    Control c = (Control)aControls;
    //* If we are between the "Previous" and the "Last" buttons, or
    //* if this control uses numbers as well, insert pipes between each link.
    if (i == aControls.Count - 2)
    {
    Label oLine = new Label();
    oLine.Text = " | ";
    oLine.CssClass = "Text9pt";
    e.Item.Cells[0].Controls.Add(oLine);
    }

    //* Make the Next and Previous buttons
    if (c is LinkButton || c is Label)
    {
    ((WebControl)c).CssClass = "Text9ptBlue";
    }
    e.Item.Cells[0].Controls.Add(c);
    }
    }
    base.OnItemCreated(e);
    }
    Here's the one on the web page:
    public void grdNetwork_People_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem )
    {
    e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#C0C0C0';this.style.cursor='hand'");
    e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='" + (e.Item.ItemType == ListItemType.Item ? "#FFFFE0" : "#FFFFFF") + "';");

    if ( e.Item.DataItem != null )
    {
    //e.Item.Cells[0].ToString();
    string PK = ((DataRowView)(e.Item.DataItem)).Row.ItemArray[0].ToString();

    e.Item.Attributes.Add("onclick", "ViewService('" + PK + "')");
    }
    }
    }

    How can I interleave the two - and not have the ListItemType.Pager item be gnerated more than once? Can I - or is this just how the events bubble up?

    Doug
    "Douglas Osborne" <Doug_period_Osborne_at_CardioNet_period_com> wrote in message news:<>...
    > All,
    >
    > On the initial load of my grid, the OnItemCreated event fires once.
    > However, when any other grid event fires - sorting, paging - the
    > OnItemCreated event fires twice. How can I prevent the double firing?
    > How do I need to handle the event processing - can I prevent the
    > second OnItemCreated from being invoked?
    >
    > TIA,
    > Doug
    >
    >


    "Douglas Osborne" <Doug_period_Osborne_at_CardioNet_period_com> wrote in message news:...
    > All,
    >
    > On the initial load of my grid, the OnItemCreated event fires once. However,
    > when any other grid event fires - sorting, paging - the OnItemCreated event
    > fires twice. How can I prevent the double firing? How do I need to handle
    > the event processing - can I prevent the second OnItemCreated from being
    > invoked?
    >
    > TIA,
    > Doug
    >
    >
     
    Prodip Saha, Jun 30, 2004
    #3
    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. Bob Barrows

    Re: Page Load Event Fires Twice

    Bob Barrows, Jul 9, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    412
    Bob Barrows
    Jul 9, 2003
  2. Steve C. Orr, MCSD

    Re: Page Load Event Fires Twice

    Steve C. Orr, MCSD, Jul 9, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    589
    Wray Smallwood
    Jul 10, 2003
  3. Jonas Nilsson

    ItemCommand Event fires twice

    Jonas Nilsson, Aug 1, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,093
    Jonas Nilsson
    Aug 1, 2003
  4. H Branyan

    Page_Load fires twice

    H Branyan, Oct 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    686
    H Branyan
    Oct 22, 2003
  5. Replies:
    3
    Views:
    1,008
Loading...

Share This Page