SgDotNet
Singapore Professional .NET User Group -For Cool Developers

Two of the same controls on the same page, but half working...

rated by 0 users
This post has 17 Replies | 2 Followers

Top 10 Contributor
Posts 2,870
kitkai Posted: 09-09-2005 9:45 PM

I have two paging controls on the same page. It's quite wierd to me that both don't work in different place. Let me explain...

1 2 >>

Take the above as an example... 1 is a label, 2 and >> is a linkbutton, which has the command argument set to 1. 2 is created dymanically (depending of the number of next pages there are)

If I click 2 on the paging control at the top, the event is captured, but the argument is empty.

If I click 2 on the paging control at the bottom, the event is not captured, and I can't test the argument.

On the second click, both controls work perfectly... Any idea y?

Best Regards, Kit Kai, MVP (SharePoint Portal Server)

Top 10 Contributor
Posts 2,257
Custom paging controls? What does Trace have to "say"?

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 50 Contributor
Posts 47
This is interesting matter.Quite hard to imagine without seeing the code. Can you show your code-behind code here? Thanks :)!
- Alvin Chooi, Malaysia - Microsoft ASP.NET™ Enthusiast - URL: http://forums.asp.net Blog: http://alvinzc.blogspot.com
Top 10 Contributor
Posts 2,870

Here is the code... very long...

#region Overridden Procedures

protected override void CreateChildControls()

{

this.CreateControlCollection();

base.CreateChildControls ();

Literal oLiteral;

this.tblPaging = new System.Web.UI.HtmlControls.HtmlTable();

this.tblPaging.CellPadding = 0;

this.tblPaging.CellSpacing = 0;

this.tblPaging.Border = 0;

this.tblPaging.Width = "100%";

this.tblPaging.Attributes["Class"] = this.CssClass;

this.trRow = new System.Web.UI.HtmlControls.HtmlTableRow();

this.tdCell = new System.Web.UI.HtmlControls.HtmlTableCell();

this.tdCell.Align = "Left";

oLiteral = new Literal();

oLiteral.Text = "Page ";

this.tdCell.Controls.Add(oLiteral);

this.litCurrent = new Literal();

this.tdCell.Controls.Add(this.litCurrent);

oLiteral = new Literal();

oLiteral.Text = " of ";

this.tdCell.Controls.Add(oLiteral);

this.litTotalPage = new Literal();

this.tdCell.Controls.Add(this.litTotalPage);

oLiteral = new Literal();

oLiteral.Text = " (";

this.tdCell.Controls.Add(oLiteral);

this.litTotalRecords = new Literal();

this.tdCell.Controls.Add(this.litTotalRecords);

oLiteral = new Literal();

oLiteral.Text = " items)";

this.tdCell.Controls.Add(oLiteral);

this.trRow.Cells.Add(this.tdCell);

this.tdCell = new System.Web.UI.HtmlControls.HtmlTableCell();

this.tdCell.Align = "Right";

this.btnFirst = new LinkButton();

this.btnFirst.Text = "<<";

this.btnFirst.CssClass = this.PageItemCssClass;

this.btnFirst.ToolTip = "Page 1";

this.btnFirst.CausesValidation = false;

this.btnFirst.Click += new System.EventHandler(this.btnFirst_Click);

this.tdCell.Controls.Add(this.btnFirst);

this.phPreviousPaging = new PlaceHolder();

this.CreatePreviousLinkButtons();

this.tdCell.Controls.Add(phPreviousPaging);

this.litCurrentPage = new Literal();

this.tdCell.Controls.Add(this.litCurrentPage);

this.phNextPaging = new PlaceHolder();

this.CreateNextLinkButtons();

this.tdCell.Controls.Add(this.phNextPaging);

this.btnLast = new LinkButton();

this.btnLast.Text = ">>";

this.btnLast.CssClass = this.PageItemCssClass;

this.btnLast.CausesValidation = false;

this.btnLast.Click += new System.EventHandler(this.btnLast_Click);

this.tdCell.Controls.Add(this.btnLast);

this.trRow.Cells.Add(this.tdCell);

this.tblPaging.Rows.Add(this.trRow);

base.Controls.Add(this.tblPaging);

this.UpdateUI();

}

protected override void OnLoad(EventArgs e)

{

this.EnsureChildControls();

base.OnLoad (e);

if (this.Page.IsPostBack)

{

}

else

{

this._Dirty = true;

this.UpdateUI();

}

}

protected override void OnPreRender(EventArgs e)

{

if (this._Dirty)

{

this.UpdateUI();

}

base.OnPreRender (e);

}

#endregion

#region Custom Events

public event PagingChangedEventHandler PagingChanged;

#endregion

#region Custom Procedures

#region UI Procedures

private void CreateNextLinkButtons()

{

if (this.phNextPaging == null)

{

return;

}

this.phNextPaging.Controls.Clear();

for (int Counter = 0; Counter < this.NoNextPage; Counter++)

{

LinkButton oLinkButton;

oLinkButton = new LinkButton();

oLinkButton.CausesValidation = false;

oLinkButton.CssClass = this.PageItemCssClass;

oLinkButton.Command += new CommandEventHandler(btnPage_Command);

this.phNextPaging.Controls.Add(oLinkButton);

}

}

private void CreatePreviousLinkButtons()

{

if (this.phPreviousPaging == null)

{

return;

}

this.phPreviousPaging.Controls.Clear();

for (int Counter = 0; Counter < this.NoPreviousPage; Counter++)

{

LinkButton oLinkButton;

oLinkButton = new LinkButton();

oLinkButton.CausesValidation = false;

oLinkButton.CssClass = this.PageItemCssClass;

oLinkButton.Command += new CommandEventHandler(btnPage_Command);

this.phPreviousPaging.Controls.Add(oLinkButton);

}

}

public void UpdateUI()

{

this.EnsureChildControls();

this.btnFirst.Visible = (this.TotalRecords != 0);

this.btnLast.Visible = (this.TotalRecords != 0);

this.litTotalRecords.Text = this.TotalRecords.ToString();

int LastPage;

if (this.PageSize == 0)

{

LastPage = 0;

}

else

{

LastPage = Convert.ToInt32(System.Math.Floor(Convert.ToDouble(this.TotalRecords) / Convert.ToDouble(this.PageSize)));

if (this.TotalRecords % this.PageSize > 0)

{

LastPage++;

}

}

this.btnLast.ToolTip = "Page " + LastPage.ToString();

this.litTotalPage.Text = LastPage.ToString();

this.litCurrent.Text = (this.CurrentPage + 1).ToString();

this.litCurrentPage.Text = (this.CurrentPage + 1).ToString();

if (this.CurrentPage == 0)

{

this.btnFirst.Visible = false;

}

else

{

this.btnFirst.Visible = true;

}

if (this.CurrentPage == LastPage - 1 || this.TotalRecords == 0)

{

this.btnLast.Visible = false;

}

else

{

this.btnLast.Visible = true;

}

//Previous Page Controls

for (int Counter = 0; Counter < this.NoPreviousPage; Counter++)

{

int CurrentPage;

CurrentPage = this.CurrentPage - this.NoPreviousPage + Counter;

if (CurrentPage < 0)

{

((LinkButton) this.phPreviousPaging.Controls[Counter]).Visible = false;

}

else

{

LinkButton oLinkButton = ((LinkButton) this.phPreviousPaging.Controls[Counter]);

oLinkButton.Visible = true;

oLinkButton.Text = (CurrentPage + 1).ToString();

oLinkButton.ToolTip = "Page " + (CurrentPage + 1).ToString();

oLinkButton.CommandArgument = (CurrentPage).ToString();

}

}

//Next Page Controls

for (int Counter = 0; Counter < this.NoNextPage; Counter++)

{

int CurrentPage;

CurrentPage = this.CurrentPage + Counter + 1;

if (CurrentPage > (LastPage - 1))

{

((LinkButton) this.phNextPaging.Controls[Counter]).Visible = false;

}

else

{

LinkButton oLinkButton = ((LinkButton) this.phNextPaging.Controls[Counter]);

oLinkButton.Visible = true;

oLinkButton.Text = (CurrentPage + 1).ToString();

oLinkButton.ToolTip = "Page " + (CurrentPage + 1).ToString();

oLinkButton.CommandArgument = (CurrentPage).ToString();

}

}

}

#endregion

#endregion

#region Custom Properties

[Category("Appearance"), DefaultValue("")]

public string PageItemCssClass

{

get

{

if (ViewState[Properties.PageItemCssClass] == null)

{

ViewState[Properties.PageItemCssClass] = String.Empty;

}

return (string)ViewState[Properties.PageItemCssClass];

}

set

{

if (ViewState[Properties.PageItemCssClass] == null || (string)ViewState[Properties.PageItemCssClass] != value)

{

ViewState[Properties.PageItemCssClass] = value;

if (this.btnFirst != null)

{

this.btnFirst.CssClass = value;

}

if (this.btnLast != null)

{

this.btnLast.CssClass = value;

}

if (this.phNextPaging != null)

{

foreach (LinkButton oLinkButton in this.phNextPaging.Controls)

{

oLinkButton.CssClass = value;

}

}

if (this.phPreviousPaging != null)

{

foreach (LinkButton oLinkButton in this.phPreviousPaging.Controls)

{

oLinkButton.CssClass = value;

}

}

}

}

}

[Category("Information"), DefaultValue(0)]

public int CurrentPage

{

get

{

if (ViewState[Properties.CurrentPage] == null)

{

ViewState[Properties.CurrentPage] = 0;

}

return ((int)ViewState[Properties.CurrentPage]) ;

}

set

{

if (ViewState[Properties.CurrentPage] == null || (int)ViewState[Properties.CurrentPage] != value)

{

this._Dirty = true;

ViewState[Properties.CurrentPage] = value;

}

}

}

[Category("Appearance"), DefaultValue(5)]

public int NoNextPage

{

get

{

if (ViewState[Properties.NoNextPage] == null)

{

ViewState[Properties.NoNextPage] = 5;

CreateNextLinkButtons();

}

return (int)ViewState[Properties.NoNextPage];

}

set

{

if (ViewState[Properties.NoNextPage] == null || (int)ViewState[Properties.NoNextPage] != value)

{

this._Dirty = true;

ViewState[Properties.NoNextPage] = value;

CreateNextLinkButtons();

}

}

}

[Category("Appearance"), DefaultValue(10)]

public int PageSize

{

get

{

if (ViewState[Properties.PageSize] == null)

{

ViewState[Properties.PageSize] = 10;

}

return (int)ViewState[Properties.PageSize];

}

set

{

if (ViewState[Properties.PageSize] == null || (int)ViewState[Properties.PageSize] != value)

{

this._Dirty = true;

ViewState[Properties.PageSize] = value;

UpdateUI();

}

}

}

[Category("Appearance"), DefaultValue(2)]

public int NoPreviousPage

{

get

{

if (ViewState[Properties.NoPreviousPage] == null)

{

ViewState[Properties.NoPreviousPage] = 2;

CreatePreviousLinkButtons();

}

return (int)ViewState[Properties.NoPreviousPage];

}

set

{

if (ViewState[Properties.NoPreviousPage] == null || (int)ViewState[Properties.NoPreviousPage] != value)

{

this._Dirty = true;

ViewState[Properties.NoPreviousPage] = value;

CreatePreviousLinkButtons();

}

}

}

[Category("Information"), DefaultValue(0)]

public decimal TotalRecords

{

get

{

if (ViewState[Properties.TotalRecords] == null)

{

ViewState[Properties.TotalRecords] = (decimal) 0;

}

return (decimal)ViewState[Properties.TotalRecords];

}

set

{

if (ViewState[Properties.TotalRecords] == null || (decimal) ViewState[Properties.TotalRecords] != value)

{

this._Dirty = true;

ViewState[Properties.TotalRecords] = value;

}

}

}

#endregion

#region Event Handlers

private void btnFirst_Click(object sender, System.EventArgs e)

{

if (this.PagingChanged != null)

{

this.PagingChanged(this, new PagingChangedEventArgs(1));

}

}

private void btnLast_Click(object sender, System.EventArgs e)

{

if (this.PagingChanged != null)

{

this.PagingChanged(this, new PagingChangedEventArgs(int.Parse(this.litTotalPage.Text)));

}

}

private void btnPage_Command(object sender, CommandEventArgs e)

{

if (this.PagingChanged != null)

{

Page.Trace.Write("Page Argument: " + (string)e.CommandArgument);

this.PagingChanged(this, new PagingChangedEventArgs(int.Parse((String) e.CommandArgument)));

}

}

#endregion

Best Regards, Kit Kai, MVP (SharePoint Portal Server)

Top 10 Contributor
Posts 2,870

 icelava wrote:
Custom paging controls? What does Trace have to "say"?

Icelava, go to the admin url for the event management system. Manage Events has enough info for paging to be used... Trace is enabled there...

Best Regards, Kit Kai, MVP (SharePoint Portal Server)

Top 10 Contributor
Posts 2,257
I'm trying to update my local copy of your Events project after the namespace renames, and TSVN is complaining after downloading the new source code.

Error: Checksum mismatch while reading representation: 
Error:    expected:  799369d16cd60689d86108bb5511aae5 
Error:      actual:  f853814f2d9371d2896968eedfac7deb
 

You seen a similar problem? It won't go away.

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 10 Contributor
Posts 2,870
Yes... I have this problem too... This is the problem which I wanted to ping you for last week. Any idea why it happened?

Best Regards, Kit Kai, MVP (SharePoint Portal Server)

Top 10 Contributor
Posts 2,257
 icelava wrote:
Error: Checksum mismatch while reading representation: 
Error:    expected:  799369d16cd60689d86108bb5511aae5 
Error:      actual:  f853814f2d9371d2896968eedfac7deb
As just performed, SVN is not running v1.2.3 and the Events repository had the Bin directories deleted (which one was corrupted causing the error). We're good to go now.

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 10 Contributor
Posts 2,257
I suppose the control is SgDotNet.WebSite.Controls.Paging control from the Web site repository?

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 10 Contributor
Posts 2,870
Yup :)

Best Regards, Kit Kai, MVP (SharePoint Portal Server)

Top 10 Contributor
Posts 2,257
I was spending yesterday reading up Nikhil Kothari's guidance on building Composite controls and found some lapses in the Paging control:
  1. No implementing of the INamingContainer as a composite control. INamingContainer is used to ensure child controls remain uniquely identifiable in a page possessing two or more of the same control type.
  2. Use of too many Literals in CreateChildControl(). Not to say it cannot be done, but it only clutters up the control tree hierarchy. The advice is to write directly to the HtmlTextWriter in the Render() method.
  3. As above, no use of the Render() method at all.
  4. Doesn't override the Controls property.
There're actually some number of other behaviours that is not implemented properly or at all. I can't say for sure if any of these actually contributes to the funny result you experience, because there're substantial issues with state management (view state) that I have not read about in previous chapters prior to this one. Looks like a lot of step-tracing to be conducted on this control.

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 10 Contributor
Posts 2,870

I'm not sure about the INamingContainer part, have to check.

As for point 2 and 3, I remember that if you override createchildcontrol, you should not be overriding render method, because the default render method will render the control based on the controls collection property. If you override render method, you should not be overriding createchildcontrol... right? 

Best Regards, Kit Kai, MVP (SharePoint Portal Server)

Top 10 Contributor
Posts 2,257
I do not know where you read such a ruling, but CreateChildControls() and Render() are not mutually exclusive devices. They achieve their own purposes in the scheme of building a control and outputting its final "visual" contents.

CreateChildControls() lets the parent container instantiate other existing control types under its own hierarchy, so it can make use of their functionality without itself performing the same chores.

Render() throws out how the control, with all its children, looks like in final HTML. The bits and pieces necessary to the control but not part of any child can (should) be rendered via the methods of the HtmlTextWriter.

********************

Anyway, the more I read and then peruse the code of the Paging control, the more I am thrown off balance by the flow of the logic:
  1. What is the purpose of Page 0? I don't believe it is an array that is being dealt with here. So seeing "Page 0 of 2" on first appearance and subsequent loss of ability to return to that (first) page is confusing.
  2. The defaults for NoPreviousPage and NoNextPage are 2 and 5 respectively, which are created regardless of knowledge of actual number of items and page size, and then gets "corrected" later in PreRender()/UpdateUI() phase. Shouldn't the number of LinkButtons before/after be created as according tothe more reliable information?

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 10 Contributor
Posts 2,257
I placed one or two of the Paging controls into a bare page, giving them fictitious number of items and page size, and just by clicking on the number "1" link button, it computes to page 5. Hmmm I think it may be easier to use Community Server's Pager control instead.....

The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral

Top 10 Contributor
Posts 2,870

 icelava wrote:
I do not know where you read such a ruling, but CreateChildControls() and Render() are not mutually exclusive devices. They achieve their own purposes in the scheme of building a control and outputting its final "visual" contents.

So how do I add html tags in between those controls that i add to the control collections?


********************

 icelava wrote:

  1. What is the purpose of Page 0? I don't believe it is an array that is being dealt with here. So seeing "Page 0 of 2" on first appearance and subsequent loss of ability to return to that (first) page is confusing.

At first I made the page index start from 1, but later, I think datagrid's currentpageindex starts from 0 right? So I change it to start from 0, to standardise.

 icelava wrote:

  1. The defaults for NoPreviousPage and NoNextPage are 2 and 5 respectively, which are created regardless of knowledge of actual number of items and page size, and then gets "corrected" later in PreRender()/UpdateUI() phase. Shouldn't the number of LinkButtons before/after be created as according tothe more reliable information?

NoPreviousPage and NoNextPage is used to determine how many previous pages you want to show and how many next pages you want to show. When the control first initialise, I create the linkbuttons in the control hierarchy first, just in case the code didn't assign a value to the property after initialisation. But if someone who might use the control sets one of the two property, I clear the control hierarchy, and recreate those controls based on the two property.

These two properties are independent of the number of items and page size, because if the number of items is less than the page size, i just hide them. It actually makes things simplier as I do not need to keep checking if the controls are created when the number of items or the currentpage changes, which is often when you add records or nagivate between records. The two properties and the page size should remain static after initialisation.

 

Best Regards, Kit Kai, MVP (SharePoint Portal Server)

Page 1 of 2 (18 items) 1 2 Next > | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems