Bug in user control rendering?

S

Sunny

Hi,

I have a usercontrol defined as:

<P>
<a id="sectionName" runat="server"></a>
<asp:placeHolder id="detailsBody" runat="server">
</asp:placeHolder>
<br>
<asp:HyperLink id="hrefTop" runat="server" NavigateUrl="#top">Top
</asp:HyperLink></P>


In the cs code I have added some public properties and a public method,
which set some of the control properties itself:

public string AnchorName
{
get {return this.sectionName.Name;}
set {this.sectionName.Name = value;}
}

//add a line in the placeholder

public void AddDetailsLine(string text)
{
HtmlGenericControl line = new HtmlGenericControl("div");
line.InnerText = text;
this.Controls.Add(line);
}


My main page is this:

<body MS_POSITIONING="FlowLayout">
<form id="Main" runat="server">
<a id="topAnchor" name="top" runat="server"></a>
<P><asp:placeholder id="Addresses" runat="server"></asp:placeholder></P>
</form>
</body>


On page load I want to add dynamically in the placeholder some of my
usercontrols. So I have:

private void Page_Load(object sender, System.EventArgs e)
{
//load the control
UC firstSec = (UC)Page.LoadControl("UC.ascx");

//setup the control
firstSec.AnchorName = "Chicago";
firstSec.AddDetailsLine("Street address");
firstSec.AddDetailsLine("City address");
firstSec.AddDetailsLine("State address");

//add to page
this.Addresses.Controls.Add(firstSec);
}


The problem is that this renders to:

<form name="Main" method="post" action="Default.aspx" id="Main">

<a id="topAnchor" name="top"></a>
<P>
<a id="_ctl2_sectionName" name="Chicago"></a>

<br>
<a id="_ctl2_hrefTop" href="#top">List</a></P>
<div>Street address</div><div>City address</div><div>State address</div>
</P>
</form>


As you can see it puts the content of the placeholder after the <a> tag,
which is not the way the control is defined.

Can someone reproduce it, or show me the mistake in my code, or possible
workaround.

Thanks
Sunny

P.S. Thanks again for reading so long post :)
 
S

Steven Cheng[MSFT]

Hi Sunny,

From the code snipet and page source you provided, if seems that you make
the UserControl's Html Source layout as below;

=========================
<P>
<a id="sectionName" runat="server"></a>
<asp:placeHolder id="detailsBody" runat="server">
</asp:placeHolder>
<br>
<asp:HyperLink id="hrefTop" runat="server" NavigateUrl="#top">Top
</asp:HyperLink></P>
========================

The "hrefTop" link is behind the "detailsBody" placeHoder control ,yes?

However, in your function for adding new detailLine (the AddDetailsLine
function)
you add the new created HtmlGenericControl into the "this.Controls"
collection as below:
=============================
public void AddDetailsLine(string text)
{
HtmlGenericControl line = new HtmlGenericControl("div");
line.InnerText = text;
this.Controls.Add(line);
}
======================================

That means to add the new created control into the bottom of the
Usercontrol rather than the placeholder. I think we shoud change it to

public void AddDetailsLine(string text)
{
HtmlGenericControl line = new HtmlGenericControl("div");
line.InnerText = text;
this.detailsBody.Controls.Add(line);
}

How do you think of this? Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

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,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top