473,387 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Bug in user control rendering?

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 :)
Nov 18 '05 #1
2 1135
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

Nov 18 '05 #2
Thanks Steven,

I was thinking that it is my mistake :)

Thanks for the help.

Sunny
Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: cksj | last post by:
I have webforms that contain a web user control (ascx). The control is a page header for all of the webforms. In one of the webforms, the data in the web user control is dependent on the webform....
5
by: Marcel Gelijk | last post by:
Hi, I am trying to create a User Control that is located in a seperate class library. The User Control contains a textbox and a button. The page generates an exception when it tries to access...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
1
by: Alan Mendelevich | last post by:
Hi, I'm trying to render control stored in ascx file to a string. I succeed with the main part but events (at least OnLoad) doesn't fire (or aren't automatically wired to Page_Load() method in...
3
by: Sridhar | last post by:
Hi, I am having a trivial problem which is not that important but I would like to know if I am missing something. I have created a user control. When I drag the user control into one of the web...
1
by: Paul | last post by:
Is there a special way to reference a Stylesheet (.css) in a usercontrol? I have a usercontrol in an aspx page.The aspx page itself has a Header Div, within which I put my UserControl.ascx. In the...
5
by: paul.hester | last post by:
Hi all, I have a custom control with an overridden Render method. Inside this method I'm rendering each control in its collection using their RenderControl method. However, I'm running into a...
0
by: yohaas | last post by:
I am dynamically adding user control to a page using Controls.Add(control). It seems that the Page_Load as well as the other page rendering events (Init, PreRender etc...) get fired twice. I...
3
by: Webmills | last post by:
Hi all I have a repeater control containing a web user control within its' item template field. Is it possible to pass through a data field into the web user control, such that this is used...
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.