Etienne
Etienne

Reputation: 7191

How to set a hyperlink to a text box in code. - with other HTML tags

My code i want to display in a textbox

<a href="http://www.erate.co.za/CompanyProfile.aspx?ID=112">
<img   src="http://www.erate.co.za/CompanyAdd.bmp" alt="Go rate us on www.eRate.co.za" 
border="0" style="width: 136px; height: 88px" /></a>

But i get the ID from a Reader like this

reader.Item("ID").ToString

Now i want to set txtCode.text to this but it does not work

txtCode.Text = "<a href="http://www.erate.co.za/CompanyProfile.aspx?ID=" +  
reader.Item("ID").ToString + ">
<img  src="http://www.erate.co.za/CompanyAdd.bmp" alt="Go rate us on www.eRate.co.za"
border="0" style="width: 136px; height: 88px" /></a>"

How would i do this?

Etienne

Upvotes: 2

Views: 6947

Answers (4)

Rob Allen
Rob Allen

Reputation: 17709

You can combine your image and link into an asp:Imagebutton instead of a plain old HTML anchor tag and set the link address dynamically from the code behind on the onClick handler similar to this:

.aspx:

<asp:ImageButton ImageUrl="http://www.erate.co.za/CompanyAdd.bmp" onClick="imgBtnClick" id="ibRateUs" runat="server" />

.cs:

public void imgBtnClick(Object sender, System.EventArgs e)
{
    String url = "http://www.erate.co.za/CompanyProfile.aspx?ID=" + reader.Item("Desc_Work").ToString;
    Response.Redirect(url, false);
}

same backend code in VB:

public sub imgBtnClick(sender as Object, e as System.EventArgs)
    Dim url as String = "http://www.erate.co.za/CompanyProfile.aspx?ID=" + reader.Item("Desc_Work").ToString
    Response.Redirect(url, false)
end sub

You may need to pass your ID as a command argument to the ImageButton if it is out of scope for the click handler.

This setup might be more readable and flexible down the line than replacing the text in a textbox.

Hope this helps.

Upvotes: 0

Canavar
Canavar

Reputation: 48088

I think your problem is about quotation marks, try this in VB.NET :

txtCode.Text = "<a href='http://www.erate.co.za/CompanyProfile.aspx?ID=" 
& reader.Item("ID").ToString 
& "><img  src='http://www.erate.co.za/CompanyAdd.bmp' alt='Go rate us on www.eRate.co.za' border='0' style='width: 136px; height: 88px' /></a>"

escape all quotation marks with : \", or use single quotation mark instead of double quotation mark.

Upvotes: 0

Cerebrus
Cerebrus

Reputation: 25775

In VB, quote characters are escaped with another quote:

txtCode.Text = "<a href=""http://www.erate.co.za/CompanyProfile.aspx?ID=" & reader.Item("ID").ToString() & ">"
txtCode.Text &= "<img src=""http://www.erate.co.za/CompanyAdd.bmp"" alt=""Go rate us on www.eRate.co.za"" border=""0"" style=""width: 136px; height: 88px"" /></a>"

How do I know it's VB and not C#?

Because this line works for you:

reader.Item("ID").ToString

In C#, it would have to be:

reader.Item["ID"].ToString()

Upvotes: 2

BFree
BFree

Reputation: 103742

txtCode.Text = @"<a href=""http://www.erate.co.za/CompanyProfile.aspx?ID=" +  
reader.Item("Desc_Work").ToString() + @">
<img  src=""http://www.erate.co.za/CompanyAdd.bmp"" alt=""Go rate us on www.eRate.co.za""
border=""0"" style=""width: 136px; height: 88px"" /></a>";

Try that, see if that works.

Upvotes: 0

Related Questions