473,732 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding server controls programmaticall y(Noob Question) missinghyperlin k text

I'm attempting to programmaticall y build up some HTML in the code
behind. Like so:

// Create Hyperlink
HyperLink link = new HyperLink();
link.NavigateUr l = "nice cat";
link.Text = "Cats Sleeping";

//Create Image
Image img = new Image();
img.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jp g";

//Add image nested in the hyperlink
link.Controls.A dd(img);

//Add the link with it's controls to a placeholder
PlaceHolder1.Co ntrols.Add(link );

When I run the code I'm expecting an picture of a cat with the text
'Cats Sleeping' all enclosed in anchor tags. But instead all I get is
the image but no text. What am I doing wrong?
Jan 16 '08 #1
4 4117
What's probably happening is the image is replacing the text since they
technically want to fill the same spot. Instead of setting the text property
of the link, try doing something like creating the text as a literal then
adding the literal to the link's control collection.

Literal myText = new Literal();
myText.Text = "Cats Sleeping";
link.Controls.A dd(myText);
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

<al****@gmail.c omwrote in message
news:f0******** *************** ***********@v4g 2000hsf.googleg roups.com...
I'm attempting to programmaticall y build up some HTML in the code
behind. Like so:

// Create Hyperlink
HyperLink link = new HyperLink();
link.NavigateUr l = "nice cat";
link.Text = "Cats Sleeping";

//Create Image
Image img = new Image();
img.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jp g";

//Add image nested in the hyperlink
link.Controls.A dd(img);

//Add the link with it's controls to a placeholder
PlaceHolder1.Co ntrols.Add(link );

When I run the code I'm expecting an picture of a cat with the text
'Cats Sleeping' all enclosed in anchor tags. But instead all I get is
the image but no text. What am I doing wrong?
Jan 16 '08 #2
Hi,

You would need to make the Hyperlink control as the container to show both
the text and image control in the Hyperlink control because this is the
default behavior of the Hyperlink control to replace the text with the image.

protected void Page_Load(objec t sender, EventArgs e)
{
// Create Hyperlink
HyperLink link = new HyperLink();
link.NavigateUr l = "nice cat";
//link.Text = "Cats Sleeping";
//link.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jp g";

Label lb = new Label();
lb.Text = "Cats Sleeping";

link.Controls.A dd(lb);
//Create Image
Image img = new Image();
img.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jp g";

//Add image nested in the hyperlink
link.Controls.A dd(img);

//Add the link with it's controls to a placeholder
PlaceHolder1.Co ntrols.Add(link );

}

Regards,
Manish
www.ComponentOne.com

"Mark Fitzpatrick" wrote:
What's probably happening is the image is replacing the text since they
technically want to fill the same spot. Instead of setting the text property
of the link, try doing something like creating the text as a literal then
adding the literal to the link's control collection.

Literal myText = new Literal();
myText.Text = "Cats Sleeping";
link.Controls.A dd(myText);
--
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression

<al****@gmail.c omwrote in message
news:f0******** *************** ***********@v4g 2000hsf.googleg roups.com...
I'm attempting to programmaticall y build up some HTML in the code
behind. Like so:

// Create Hyperlink
HyperLink link = new HyperLink();
link.NavigateUr l = "nice cat";
link.Text = "Cats Sleeping";

//Create Image
Image img = new Image();
img.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jp g";

//Add image nested in the hyperlink
link.Controls.A dd(img);

//Add the link with it's controls to a placeholder
PlaceHolder1.Co ntrols.Add(link );

When I run the code I'm expecting an picture of a cat with the text
'Cats Sleeping' all enclosed in anchor tags. But instead all I get is
the image but no text. What am I doing wrong?
Jan 16 '08 #3
On 16 Січ, 07:07, alu...@gmail.co m wrote:
I'm attempting to programmaticall y build up some HTML in the code
behind. Like so:

// Create Hyperlink
HyperLink link = new HyperLink();
link.NavigateUr l = "nice cat";
link.Text = "Cats Sleeping";

//Create Image
Image img = new Image();
img.ImageUrl = "http://www.cats.org.uk/images/lhstop_home2.jp g";

//Add image nested in the hyperlink
link.Controls.A dd(img);

//Add the link with it's controls to a placeholder
PlaceHolder1.Co ntrols.Add(link );

When I run the code I'm expecting an picture of a cat with the text
'Cats Sleeping' all enclosed in anchor tags. But instead all I get is
the image but no text.


Try this:

HyperLink link = new HyperLink();
link.NavigateUr l = "nice cat";
link.Text = string.Format(" <img src='{0}'><br>{ 1}", "http://
www.cats.org.uk/images/lhstop_home2.jp g", "Cats Sleeping");
PlaceHolder1.Co ntrols.Add(link );

Regards,
Mykola
http://marss.co.ua - Casual ideas for web development
Jan 16 '08 #4
Thanks for both your great answers, that's excatly what's been
happening. And thanks for the bit of code Manish I'll use it. Many
thanks Again
Jan 16 '08 #5

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

Similar topics

4
3094
by: V. Jenks | last post by:
What seems like a simple thing is apparently not so straightforward? I have a datalist. Inside of that datalist is an <itemtemplate> secion which contains other server controls such as a label, a radiobuttonlist, etc. I'm driving myself insane trying to figure out how to get
2
4855
by: D Sheldon | last post by:
I am creating a server control that adds web controls (i.e. textboxes, etc) to a form. I use HtmlTable to build the table and insert the controls. Now I want to add validators to the textbox. Here is the code that I am using bool lastnamerequired=false public bool LastNameRequire get{return lastnamerequired; set{lastnamerequired = value; if(lastnamerequired
3
2066
by: Andla Rand | last post by:
Hi, I would appreciate some directions on how to replace text as server controls. I'm currently reading text from a database and would like to build server logic based on specific information retrieved from the server. Here is some sample code that I'm currently playing with: string repl=spn.InnerHtml;
4
1975
by: clintonG | last post by:
Technically speaking, this issue is not about modifying the HTML generated by server controls but preceding the HTML generated by server controls with an HTML control generated on the basis of the type and the context of the server control itself. Clear as mud? :-) Consider the following server control... <asp:textbox id="MemberEmail" runat="server" ></asp:textbox> TextBox renders at run-time as an HTML control... <input...
2
4570
by: Patrick | last post by:
I want to define a set of web-form templates in XML and render the equivalent web-form with ASP.NET, then process any input server controls on the form. Reading the XML file from Page_load is easy, but 1) How do I set about dynamically creating user controls (like TextBox, TextArea) --- simply Declare and initialised (new) the user controls?? How do I "place" it graphically on the form. Ideally, I want them to lay out in a table, one...
14
2366
by: Joe | last post by:
Hello All: I am trying to dynamically populate a web page with literal content and controls (textboxes and checkboxes (and eventually two buttons - the buttons do not appear in the code yet). I read an xml file and, using the values retrieved from it, determine what text should be displayed and which controls should be rendered and - most importantly - where those controls should be rendered. The ultimate goal is to have some text...
22
2183
by: Mr Newbie | last post by:
I was thinking about developing a workflow application yesterday and was musing over the different approaches than one could take in restricting specific actions on a ticket( Form ) at any said stage. One approach I have used on other systems is to prevent the action buttons appearing. For example, if one did not have the Role of Administrator, one would be prevented from deleting a ticket not created by oneself. However, it did occur...
4
1399
by: z f | last post by:
hi, tough one? for me it is currently. i have a user control that contains other controls like text boxes. in the client i need to dynamically add the user control using DHTML. i achive this by having the control hidden and grabbing it's innerHTML to a new container in the HTML. i also change controls ID suffix in order to be able to track added controls like in old HTML.
4
1343
by: sean_mcad | last post by:
We are starting to standardize what we have been doing with our web applications and I have a “cost-benefit” question when it comes to some server controls. Main two examples is Plain text vs. <asp:label /and <divvs <asp:panelsfor current static information. In my view if we go ahead and make all things static into server controls anyway then we have the advantage of things like localization, skins and
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9445
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9306
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.