OnItemCreated fires twice

D

Douglas Osborne

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
 
D

Douglas Osborne

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
 
P

Prodip Saha

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top