views:

780

answers:

4

Hi

My code i want to display in a textbox

<a href="http://www.erate.co.za/CompanyProfile.aspx?ID=112"&gt;
<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?

Thanks in advanced! Etienne

A: 
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.

BFree
Its complaining about the first @ sign - Message = Character is not valid.
Etienne
That is because you are using VB, and I believe @BFree assumed it to be C#.
Cerebrus
A: 

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.

Canavar
Nope, not working
Etienne
A: 

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.

Rob Allen
+1  A: 

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()
Cerebrus
Thanks! I just had to add another " at the end of my reader.Item("ID").ToString().
Etienne
Most welcome! :-)
Cerebrus