Composite control: child attributes not rendered

A

Anig

I'm writing a composite control inheriting from WebControl and
INamingContainer. I'm wrote the control using the composition approach, i.e.
I haven't overrided the render method(). The control is very simple. It has 4
linkbutton and a label. Have defined the LabelCssClass property, just a
wrapper on MyLabel.CssClass. The problem is that the class attribute for the
label is not rendered in the html. Can anyone help me please? It's urgent!!!
Thanks in advance.
 
T

Teemu Keiski

Hi,

post the code. How have you implemented the LabelCssClass property? Do you
call EnsureChildControls in it?
 
A

Anig

[ToolboxData("<{0}:pager runat=server></{0}:pager>")]
public class Pager : WebControl, INamingContainer
{
private LinkButton CmdFirst;
private LinkButton CmdLast;
private LinkButton CmdNext;
private LinkButton CmdPrev;
private Label LabelPage;

private static readonly string TotalPagesViewstateKey="TotalPages" ;
private static readonly string CurrentPageViewstateKey="CurrentPage" ;
[Description("Current page value"),Category("Behavior"), DefaultValue(1)]
public int CurrentPage
{
get
{
object obj=this.ViewState[Pager.CurrentPageViewstateKey];
return ((obj != null) ? ((int) obj) : 1);
}
set
{
this.ViewState[Pager.CurrentPageViewstateKey] = value;
}
}

[Description("CSS Class name applied to the label page"),
Category("Appearance")]
public string LabelPageCssClass
{
get
{
this.EnsureChildControls();
return this.LabelPage.CssClass;
}
set
{
this.EnsureChildControls();
this.LabelPage.CssClass = value;
}
}

[Description("The text to be shown for the control"),
Category("Appearance"),
DefaultValue("Pagina <B>{0}</B> di <B>{1}</B>")]
public string Text
{
get
{
this.EnsureChildControls() ;
return LabelPage.Text ;
}
set
{
this.EnsureChildControls() ;
LabelPage.Text = value ;
}
}

[Description("Total pages value"),
DefaultValue(1), Category("Behavior")]
public int TotalPages
{
get
{
object obj = this.ViewState[Pager.TotalPagesViewstateKey];
return ((obj != null) ? ((int) obj) : 1);
}
set
{
this.ViewState[Pager.TotalPagesViewstateKey] = value;
}
}

#region Methods section
protected override void CreateChildControls()
{
this.CmdFirst = new LinkButton();
this.CmdNext = new LinkButton();
this.CmdPrev = new LinkButton();
this.CmdLast = new LinkButton();
this.LabelPage = new Label();
this.CmdFirst.Text = "<<";

this.CmdPrev.Text = "<";
this.CmdNext.Text = ">";
this.CmdLast.Text = ">>";
this.LabelPage.Text = "Pagina <B>{0}</B> di <B>{1}</B>" ;

this.UpdateLabel();

this.Controls.Add(this.CmdFirst);
this.Controls.Add(this.CmdPrev);
this.Controls.Add(this.LabelPage);
this.Controls.Add(this.CmdNext);
this.Controls.Add(this.CmdLast);
}

private void UpdateLabel()
{
this.LabelPage.Text = string.Format(this.Text, this.CurrentPage,
this.TotalPages);
}
}
 
T

Teemu Keiski

When I use it on the Page (without changing a line of your code)

<ctr:pager ID="MyPager" LabelPageCssClass="testclass" runat="server" />



it renders like this in IE:

<span id="MyPager"><a
href="javascript:__doPostBack('MyPager$ctl00','')"><<</a><a
href="javascript:__doPostBack('MyPager$ctl01','')"><</a><span
class="testclass">Pagina <B>1</B> di <B>1</B></span><a
href="javascript:__doPostBack('MyPager$ctl03','')">></a><a
href="javascript:__doPostBack('MyPager$ctl04','')">>></a></span>

which seems to work fine for me (note the testclass being outputted)

Are you possible checking it with other browser than IE (and we are speaking
of ASP.NET v1 here)?
 
A

Anig

Thank you for reply....


I'm using ASP.NET 1.1

..
..
..

<ctr:pager id="Pager1" runat="server" LabelPageCssClass="testclass"
ButtonsCssClass="LinkButton"></replysytelwebcontrols:pager></P>


In I.E. the rendered html is:

<span id="Pager1" ButtonsCssClass="LinkButton"><a
href="javascript:__doPostBack('Pager1$_ctl0','')"><<</a><a
href="javascript:__doPostBack('Pager1$_ctl1','')"><</a>Pagina <B>1</B> di
<B>1</B><a href="javascript:__doPostBack('Pager1$_ctl3','')">></a><a
href="javascript:__doPostBack('Pager1$_ctl4','')">>></a></span>


Note the second <span> tag is missing! And the "testclass" NOT being outputted


Best regards

G.Z.
 
A

Anig

I'm sorry, actually the code is a bit more complex, see below.
Anyway the "class" attribute for the label is not rendered in the html.

Best regards,

G.Z.



using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
using System.ComponentModel;

using System.IO;

namespace Anig.Web.UI.WebControls
{
[Designer("Anig.Web.UI.WebControls.PagerDesigner"),
ToolboxData("<{0}:pager runat=server></{0}:pager>"),
PersistChildren(true)]

public class Pager : WebControl, INamingContainer
{
private LinkButton CmdFirst;
private LinkButton CmdLast;
private LinkButton CmdNext;
private LinkButton CmdPrev;
private Label LabelPage;

private static readonly string TotalPagesViewstateKey = "TotalPages" ;
private static readonly string CurrentPageViewstateKey = "CurrentPage" ;

public delegate void FirstPageClickedHandler(object sender, EventArgs e);
public delegate void LastPageClickedHandler(object sender, EventArgs e);
public delegate void NextPageClickedHandler(object sender, EventArgs e);
public delegate void PrevPageClickedHandler(object sender, EventArgs e);

// Events
public event FirstPageClickedHandler FirstPageClicked;
public event LastPageClickedHandler LastPageClicked;
public event NextPageClickedHandler NextPageClicked;
public event PrevPageClickedHandler PrevPageClicked;

// Methods
private void CmdFirst_Click(object sender, EventArgs e)
{
this.OnFirstPageClicked(e);
}

private void CmdLast_Click(object sender, EventArgs e)
{
this.OnLastPageClicked(e);
}

private void CmdNext_Click(object sender, EventArgs e)
{
this.OnNextPageClicked(e);
}

private void CmdPrev_Click(object sender, EventArgs e)
{
this.OnPrevPageClicked(e);
}

protected virtual void OnFirstPageClicked(EventArgs e)
{
if (this.FirstPageClicked != null)
{
this.FirstPageClicked(this, e);
}
}

protected virtual void OnLastPageClicked(EventArgs e)
{
if (this.LastPageClicked != null)
{
this.LastPageClicked(this, e);
}
}

protected virtual void OnNextPageClicked(EventArgs e)
{
if (this.NextPageClicked != null)
{
this.NextPageClicked(this, e);
}
}

protected virtual void OnPrevPageClicked(EventArgs e)
{
if (this.PrevPageClicked != null)
{
this.PrevPageClicked(this, e);
}
}

// Properties
[Category("Behavior"),
DefaultValue(true),
Description("Enabled state of first-page link button")]
public bool ButtonFirstEnabled
{
get
{
this.EnsureChildControls();
return this.CmdFirst.Enabled;
}
set
{
this.EnsureChildControls();
this.CmdFirst.Enabled = value;
}
}

[Description("Enabled state of last-page link button"),
DefaultValue(true),
Category("Behavior")]
public bool ButtonLastEnabled
{
get
{
this.EnsureChildControls();
return this.CmdLast.Enabled;
}
set
{
this.EnsureChildControls();
this.CmdLast.Enabled = value;
}
}

[Description("Enabled state of next-page link button"),
DefaultValue(true),
Category("Behavior")]
public bool ButtonNextEnabled
{
get
{
this.EnsureChildControls();
return this.CmdNext.Enabled;
}
set
{
this.EnsureChildControls();
this.CmdNext.Enabled = value;
}
}

[Category("Behavior"),
DefaultValue(true),
Description("Enabled state of previous-page link button")]
public bool ButtonPrevEnabled
{
get
{
this.EnsureChildControls();
return this.CmdPrev.Enabled;
}
set
{
this.EnsureChildControls();
this.CmdPrev.Enabled = value;
}
}

[Category("Appearance"),
Description("CSS Class name applied to the navigation buttons")]
public string ButtonsCssClass
{
get
{
this.EnsureChildControls();
return this.CmdFirst.CssClass;
}
set
{
this.EnsureChildControls();
this.CmdFirst.CssClass = value;
this.CmdPrev.CssClass = value;
this.CmdNext.CssClass = value;
this.CmdLast.CssClass = value;
}
}

[Description("Current page value"),
Category("Behavior"), DefaultValue(1)]
public int CurrentPage
{
get
{
object obj = this.ViewState[Pager.CurrentPageViewstateKey];
return ((obj != null) ? ((int) obj) : 1);
}
set
{
this.ViewState[Pager.CurrentPageViewstateKey] = value;
}
}

[Description("CSS Class name applied to the label page"),
Category("Appearance")]
public string LabelPageCssClass
{
get
{
this.EnsureChildControls();
return this.LabelPage.CssClass;
}
set
{
this.EnsureChildControls();
this.LabelPage.CssClass = value;
}
}

[Description("The text to be shown for the control"),
Category("Appearance"),
DefaultValue("Pagina <B>{0}</B> di <B>{1}</B>")]
public string Text
{
get
{
this.EnsureChildControls() ;
return LabelPage.Text ;
}
set
{
LabelPage.Text = value ;
this.EnsureChildControls() ;
}
}

[Description("Total pages value"),
DefaultValue(1), Category("Behavior")]
public int TotalPages
{
get
{
object obj = this.ViewState[Pager.TotalPagesViewstateKey];
return ((obj != null) ? ((int) obj) : 1);
}
set
{
this.ViewState[Pager.TotalPagesViewstateKey] = value;
}
}

// Methods section
protected override void CreateChildControls()
{
this.CmdFirst = new LinkButton();
this.CmdNext = new LinkButton();
this.CmdPrev = new LinkButton();
this.CmdLast = new LinkButton();
this.LabelPage = new Label();

this.CmdFirst.Text = "<<";
this.CmdFirst.Click += new EventHandler(this.CmdFirst_Click);
this.CmdPrev.Text = "<";
this.CmdPrev.Click += new EventHandler(this.CmdPrev_Click);
this.CmdNext.Text = ">";
this.CmdNext.Click += new EventHandler(this.CmdNext_Click);
this.CmdLast.Text = ">>";
this.CmdLast.Click += new EventHandler(this.CmdLast_Click);
this.LabelPage.Text = "Pagina <B>{0}</B> di <B>{1}</B>" ;

this.UpdateLabel();

this.Controls.Add(this.CmdFirst);
this.Controls.Add(this.CmdPrev);
this.Controls.Add(this.LabelPage);
this.Controls.Add(this.CmdNext);
this.Controls.Add(this.CmdLast);
}

private void UpdateLabel()
{
this.LabelPage.Text = string.Format(this.Text, this.CurrentPage,
this.TotalPages);
}


}

internal class PagerDesigner : ControlDesigner
{

// Fields
protected Pager pager;

// Methods
public override void Initialize(IComponent component)
{
if (component is Pager)
{
base.Initialize(component);
this.pager = (Pager) component;
}
}

}
}
 
A

Anig

I'm sorry, actually the code is a bit more complex, see below full code.

Here is how I use the control in asp web page:

<anigwebcontrols:pager id="Pager1" runat="server"
LabelPageCssClass="MyLabel"
ButtonsCssClass="LinkButton"></anigwebcontrols:pager>


Anyway the "class" attribute for the label is missing in the rendered html.

<span id="Pager1"><a class="LinkButton"
href="javascript:__doPostBack('Pager1$_ctl0','')"><<</a><a class="LinkButton"
href="javascript:__doPostBack('Pager1$_ctl1','')"><</a>Pagina <B>1</B> di
<B>1</B><a class="LinkButton"
href="javascript:__doPostBack('Pager1$_ctl3','')">></a><a class="LinkButton"
href="javascript:__doPostBack('Pager1$_ctl4','')">>></a></span>


Best regards

Anig



using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
using System.ComponentModel;

using System.IO;

namespace Anig.Web.UI.WebControls
{
[Designer("Anig.Web.UI.WebControls.PagerDesigner"),
ToolboxData("<{0}:pager runat=server></{0}:pager>"),
PersistChildren(true)]

public class Pager : WebControl, INamingContainer
{
private LinkButton CmdFirst;
private LinkButton CmdLast;
private LinkButton CmdNext;
private LinkButton CmdPrev;
private Label LabelPage;

private static readonly string TotalPagesViewstateKey = "TotalPages" ;
private static readonly string CurrentPageViewstateKey = "CurrentPage" ;

public delegate void FirstPageClickedHandler(object sender, EventArgs e);
public delegate void LastPageClickedHandler(object sender, EventArgs e);
public delegate void NextPageClickedHandler(object sender, EventArgs e);
public delegate void PrevPageClickedHandler(object sender, EventArgs e);

// Events
public event FirstPageClickedHandler FirstPageClicked;
public event LastPageClickedHandler LastPageClicked;
public event NextPageClickedHandler NextPageClicked;
public event PrevPageClickedHandler PrevPageClicked;

// Methods
private void CmdFirst_Click(object sender, EventArgs e)
{
this.OnFirstPageClicked(e);
}

private void CmdLast_Click(object sender, EventArgs e)
{
this.OnLastPageClicked(e);
}

private void CmdNext_Click(object sender, EventArgs e)
{
this.OnNextPageClicked(e);
}

private void CmdPrev_Click(object sender, EventArgs e)
{
this.OnPrevPageClicked(e);
}

protected virtual void OnFirstPageClicked(EventArgs e)
{
if (this.FirstPageClicked != null)
{
this.FirstPageClicked(this, e);
}
}

protected virtual void OnLastPageClicked(EventArgs e)
{
if (this.LastPageClicked != null)
{
this.LastPageClicked(this, e);
}
}

protected virtual void OnNextPageClicked(EventArgs e)
{
if (this.NextPageClicked != null)
{
this.NextPageClicked(this, e);
}
}

protected virtual void OnPrevPageClicked(EventArgs e)
{
if (this.PrevPageClicked != null)
{
this.PrevPageClicked(this, e);
}
}

// Properties
[Category("Behavior"),
DefaultValue(true),
Description("Enabled state of first-page link button")]
public bool ButtonFirstEnabled
{
get
{
this.EnsureChildControls();
return this.CmdFirst.Enabled;
}
set
{
this.EnsureChildControls();
this.CmdFirst.Enabled = value;
}
}

[Description("Enabled state of last-page link button"),
DefaultValue(true),
Category("Behavior")]
public bool ButtonLastEnabled
{
get
{
this.EnsureChildControls();
return this.CmdLast.Enabled;
}
set
{
this.EnsureChildControls();
this.CmdLast.Enabled = value;
}
}

[Description("Enabled state of next-page link button"),
DefaultValue(true),
Category("Behavior")]
public bool ButtonNextEnabled
{
get
{
this.EnsureChildControls();
return this.CmdNext.Enabled;
}
set
{
this.EnsureChildControls();
this.CmdNext.Enabled = value;
}
}

[Category("Behavior"),
DefaultValue(true),
Description("Enabled state of previous-page link button")]
public bool ButtonPrevEnabled
{
get
{
this.EnsureChildControls();
return this.CmdPrev.Enabled;
}
set
{
this.EnsureChildControls();
this.CmdPrev.Enabled = value;
}
}

[Category("Appearance"),
Description("CSS Class name applied to the navigation buttons")]
public string ButtonsCssClass
{
get
{
this.EnsureChildControls();
return this.CmdFirst.CssClass;
}
set
{
this.EnsureChildControls();
this.CmdFirst.CssClass = value;
this.CmdPrev.CssClass = value;
this.CmdNext.CssClass = value;
this.CmdLast.CssClass = value;
}
}

[Description("Current page value"),
Category("Behavior"), DefaultValue(1)]
public int CurrentPage
{
get
{
object obj = this.ViewState[Pager.CurrentPageViewstateKey];
return ((obj != null) ? ((int) obj) : 1);
}
set
{
this.ViewState[Pager.CurrentPageViewstateKey] = value;
}
}

[Description("CSS Class name applied to the label page"),
Category("Appearance")]
public string LabelPageCssClass
{
get
{
this.EnsureChildControls();
return this.LabelPage.CssClass;
}
set
{
this.EnsureChildControls();
this.LabelPage.CssClass = value;
}
}

[Description("The text to be shown for the control"),
Category("Appearance"),
DefaultValue("Pagina <B>{0}</B> di <B>{1}</B>")]
public string Text
{
get
{
this.EnsureChildControls() ;
return LabelPage.Text ;
}
set
{
LabelPage.Text = value ;
this.EnsureChildControls() ;
}
}

[Description("Total pages value"),
DefaultValue(1), Category("Behavior")]
public int TotalPages
{
get
{
object obj = this.ViewState[Pager.TotalPagesViewstateKey];
return ((obj != null) ? ((int) obj) : 1);
}
set
{
this.ViewState[Pager.TotalPagesViewstateKey] = value;
}
}

// Methods section
protected override void CreateChildControls()
{
this.CmdFirst = new LinkButton();
this.CmdNext = new LinkButton();
this.CmdPrev = new LinkButton();
this.CmdLast = new LinkButton();
this.LabelPage = new Label();

this.CmdFirst.Text = "<<";
this.CmdFirst.Click += new EventHandler(this.CmdFirst_Click);
this.CmdPrev.Text = "<";
this.CmdPrev.Click += new EventHandler(this.CmdPrev_Click);
this.CmdNext.Text = ">";
this.CmdNext.Click += new EventHandler(this.CmdNext_Click);
this.CmdLast.Text = ">>";
this.CmdLast.Click += new EventHandler(this.CmdLast_Click);
this.LabelPage.Text = "Pagina <B>{0}</B> di <B>{1}</B>" ;

this.UpdateLabel();

this.Controls.Add(this.CmdFirst);
this.Controls.Add(this.CmdPrev);
this.Controls.Add(this.LabelPage);
this.Controls.Add(this.CmdNext);
this.Controls.Add(this.CmdLast);
}

private void UpdateLabel()
{
this.LabelPage.Text = string.Format(this.Text, this.CurrentPage,
this.TotalPages);
}


}

internal class PagerDesigner : ControlDesigner
{

// Fields
protected Pager pager;

// Methods
public override void Initialize(IComponent component)
{
if (component is Pager)
{
base.Initialize(component);
this.pager = (Pager) component;
}
}

}
}
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top