I am using string builder class to display an image but the image is not coming
My code is
StringBuilder sb1 = new StringBuilder();
sb1.AppendLine("<tr>");
string url = "<img src='~/images/youtube.png'/>";
sb1.AppendLine("<td style='padding-top:3px;border:solid 1px orange;text-
align:center'><a href='http://www.linkedin.com/groups/
'><img src=url style='height:55px;width:220px;border-style:solid; padding-
left:50px/></a></td>");
sb1.AppendLine("</tr>");
This is a much better way, not messing with any of the strings, much cleaner.
Server side:
Table tbl = new Table();
TableRow tr = new TableRow();
TableCell td = new TableCell();
td.Controls.Add(new Image
{
ImageUrl = "http://www.your-site.com/content/images/pic.jpg"
});
tr.Cells.Add(td);
tbl.Rows.Add(tr);
ph.Controls.Add(tbl);
aspx page:
<asp:PlaceHolder runat="server" ID="ph" />
I thing the problem is with in the line
sb1.AppendLine("<td style='padding-top:3px;border:solid 1px orange;text-
align:center'><a href='http://www.linkedin.com/groups/QuickMove-Core-Business-Solutions-
Moving-3791901'><img src=url style='height:55px;width:220px;border-style:solid; padding-
left:50px/></a></td>");
use it like this
sb1.AppendLine("<td style='padding-top:3px;border:solid 1px orange;text-
align:center'><a href='http://www.linkedin.com/groups/QuickMove-Core-Business-Solutions-
Moving-3791901'><img src="+url+" style='height:55px;width:220px;border-style:solid; padding-
left:50px/></a></td>");
Edit
change
string url = "<img src='~/images/youtube.png'/>";
to
string url = "/images/youtube.png";
You have to assign this html to your page element. You need server accessible html element to assign the html to it. You can make a table server accessible by assigning id to it and setting runat = "server".
In html
<table id="tbl" runat="server" >
</table>
In Code bahind
tbl.InnerHTML = sb1.ToString();
Related
I have an html with asp C# backend application, where I would like to display a string in the format "data:image/bmp;base64," + base64 encoded string inside an iframe.
The server code is generating the string:
using (MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Bmp);
stream.Position = 0;
content = Convert.ToBase64String(stream.ToArray()); // base64EncodedString
}
and the front-end iframe should display it upon button press:
<form method="POST" runat="server">
<asp:Button runat="server" id="button1" class="btn" Text="Next"/>
<iframe id="iframe" runat="server" class="iframe"></iframe>
</form>
<script>
window.onload = function () {
document.getElementById("<%=iframe.ClientID %>").src="data:image/bmp;base64,"+"<%=content %>";
}
</script>
The content field is populated and shared correctly (as can be displayed in an alert), for example;
"data:image/bmp;base64,Qk1aAAAAAAAAAEIAAAAoAAAABgAAAAYAAAABAAQAAAAAAAAAAADEDgAAxA4AAAMAAAADAAAAAAAA/wAA//8A2P//IAEgAAASAAABIAEAEgASACABIAAAEgAA" but the iframe remains blank.
Can you suggest what causes it and how to fix that?
thanks!
I have tried using different models of front-back communication including json, ajax,etc. I have also tried to update the iframe src from the asp server side, but as the content variable is correct, I assume the issue is with the front-end.
Well, is the goal that you "must" use a iframe, or do you care?
I mean, why not just drop in a image control, or even a "div", and shove the image string into that div or image control?
So, lets drop a button, and then say pull a row from a database that has one column with a binary image. By converting that binary image to a base64 image string, we are free to shove that string into the image.
So, say this markup:
<asp:Button ID="Button1" runat="server" Text="Get Image" OnClick="Button1_Click" />
<br />
<h3>My Image control</h3>
<asp:Image ID="Image1" runat="server" Width="350px" />
<br />
<h3>My div</h3>
<div id="mydiv" runat="server">
</div>
And then the code behind:
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
string strSQL =
"SELECT ID, MyImage FROM Fighters WHERE ID = 4";
using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
{
conn.Open();
DataTable rstData = new DataTable();
rstData.Load(cmdSQL.ExecuteReader());
string PicString =
#"Data:Image/png;base64," + Convert.ToBase64String((byte[])rstData.Rows[0]["MyImage"]);
Image1.ImageUrl = PicString; // shove into image control
Image MyImage = new Image();
MyImage.Width = 200;
MyImage.ImageUrl = PicString;
mydiv.Controls.Add(MyImage); // add image to "div"
}
}
}
So, now when we click the button, we get this:
So, it not all that clear if you need/want a iFrame or not???
Using ASP.NET...
I have an email button on a popup extender and would like to use the inner html of another aspx page to use for the body, then edit a couple tags before it is sent. Is this possible?
Basically I'm using the .Net.Mail.MailMessage to create an HtmlBody and want to grab the html of another page (without actually rendering the page) as opposed to recreating it in a string.
You will need to create an instance of the Page that you want the html from. After that you can use the Page's Render method (see below example about this being protected) to have it generate the html that would normally be sent to the browser to a HtmlTextWriter.
When you create the HtmlTextWriter, you can use a combination of StringBuilders and TextWriters in order to get the html from the HtmlTextWriter. Something like this.
StringBuilder SB = new StringBuilder();
StringWriter SW = new StringWriter(SB);
HtmlTextWriter htmlTW = new HtmlTextWriter(SW);
MyPage page = new MyPage(); // your page here
page.RenderHtml( htmlTW ); //You need to create this method in your page, see below
string html = SB.ToString();
So the only problem is that Page.Render is protected. You will need to expose a method in your Page class that is public that calls the Render method.
Something like this should work for that method
public void RenderHtml( HtmlTextWriter htmlTextWriter )
{
Render( htmlTextWriter );
}
Hope this helps.
First of all creating html emails doesn't allow for complicated html (internal,external styling, and other controls) see...
Html Email Guidline.
So what I did was use the following to create an html string for emailing...
1) Create a simple .htm file as a template, formatting everything in standard html tables (not asp:tables), and other standard html tags.
2) Only use in-line styling if necessary.
3) Then create replacable text words or phrases to replace. You can also use a comment to replace text or for adding to a table see markup below
<span style="font-size: 16px;">TodaysDate</span>
<table id="PeopleTable" border="1" cellpadding="3" cellspacing="0" width="720">
<tr style="font-weight: bold;">
<td width="100">
First Name
</td>
<td width="100">
Last Name
</td>
<td width="100">
Phone Number
</td>
</tr>
<!--AddRowsHere-->
</table>
4) Then from a button in code behind you need to get the .htm page as string and replace the text with values you want
Using sr As New System.IO.StreamReader(Server.MapPath("PeoplePage.htm"))
GetHtml = sr.ReadToEnd
End Using
GetHtml = Replace(GetHtml, "TodaysDate", Now.ToShortDateString)
5) Return this string to the .Net.Mail.MailMessage.Body and send it out as you normally would. Make sure to set MailMesage.IsBodyHtml = True
You could also create the entire string using StringBuilder without using a template.
If you put the content of your email into a server control, you can render it as a string.
public static string GetRenderedHtml(this Control control)
{
StringBuilder sbHtml = new StringBuilder();
using (StringWriter stringWriter = new StringWriter(sbHtml))
using (HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter))
{
control.RenderControl(textWriter);
}
return sbHtml.ToString();
}
If you have any editable controls (TextBox, DropDownList, etc), you'll need to replace them with Labels. See the following post for a full example and explanation.
http://jrummell.com/send-a-completed-form-email-without-a-stringbuilder
What is the server side control used to add an H4 to the markup from the code behind?
var h4 = new HtmlGenericControl("h4");
h4.InnerHtml = "Heading Text";
parentControl.Controls.Add(h4);
I recommend creating an HtmlGenericControl in your code-behind. The benefit of these over Literals is that they are proper HtmlControls, with the ability for you to programmatically set and modify properties such as InnerHtml, CssClass, Style etc.
HtmlGenericControl myH4 = new HtmlGenericControl("h4")
{
ID = "myH4",
InnerHtml = "Your Heading Here"
});
yourContainerControl.Controls.Add(myH4);
You could use an asp:Literal control - this just writes out the exact text that you set it to.
E.g:
Dim myLiteral as Literal = new Literal()
myLiteral.Text = "<h4>My Heading</h4>"
Then add your Literal to the page.
There is nothing like a <asp:H4 /> control. However, you can add any HTML element to a page via HtmlGenericControl type in your code behind.
For example, to create it:
HtmlGenericControl headerControl = new HtmlGenericControl(HtmlTextWriterTag.H4.ToString());
headerControl.ID = "myHeader";
headerControl.InnerHtml = "Hello World";
placeHolder.Controls.Add(headerControl);
To access it from code behind later:
HtmlGenericControl headerControl = FindControl("myHeader") as HtmlGenericControl;
I have a table on my ASP.net page something like this:
<table runat="server" id="resultsTable"></table>
I dynamically add content to the table, and it works just fine. However, I want to get the HTML of the table once I've added the dynamic content, i.e. something like this (formatting isn't important, I've just added it)
<table runat="server" id="resultsTable">
<tr>
<td>Hello!</td>
</tr>
<tr>
<td>Goodbye!</td>
</tr>
</table>
I need the result as a string. Obviously I could do some looping and build my own table with the data, but I'd prefer to not do that if at all possible.
Initially I though to just use the InnerHtml or InnerText methods, but these are not supported on the HtmlTable class.
So what if we use the Render method? Something like this (take from Anatoly Lubarsky)?
public string RenderControl(Control ctrl)
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}
This method could obviously be cleaned up to handle closing the writers, etc.
Since your table is a server control, you may use its RenderControl method to obtain the render result:
public static string GetRenderResult(Control control) {
using(StringWriter sw = new StringWriter(CultureInfo.InvariantCulture)) {
using(HtmlTextWriter writer = new HtmlTextWriter(sw))
control.RenderControl(writer);
sw.WriteLine();
return sw.ToString();
}
}
There is a couple of way I can think of how to do this. The easy would be to surround the table in a . Then on your vb/C# side simply call hold.innerhtml and it will return a string.
I used the following in the OnLoad method of my page to convert your table to a string of HTML:
string html;
using (var writer = new StringWriter())
using (var xmlwriter = new HtmlTextWriter(writer))
{
this.resultsTable.RenderControl(xmlwriter);
html = writer.ToString();
}
Hmm. I'd javascript the table markup to some hidden field (which is a server control) before the form gets posted.
<div id="tablediv">
<table>...</table>
</div>
javascript:
var html = document.getElementById('tablediv').innerHTML;
document.getElementById('hfTableHtml').value = html;
EDIT: And yes, I'd worry about the request validation that is going to happen! You'd have to disable it or substitute those markup elements w/ something else before storing it into the hidden field
Look into the HTMLAgilityPack (several SO posts reference it.)
I have a td that I want to inject with a server image control (asp.net) using innerHTML = "". The webcontrol's toString is giving the type.
Is there a way to extract the generated from the server control?
Or, is there a different solution...?
Thanks
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
img.RenderControl(new HtmlTextWriter(writer));
td.InnerHtml = sb.ToString();
or the more obvious
td.Controls.Add(img);
You can use the RenderControl method to get the output HTML.
Ex:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
class Program
{
static void Main()
{
var img = new Image();
var hw = new HtmlTextWriter(Console.Out);
img.RenderControl(hw);
hw.Dispose();
}
}
Output:
<img src="" style="border-width:0px;" />
The first part of your question looks like you're asking how to inject an image at runtime into a table cell.
If the table cell is part of your ASP.NET page, you could do something like:
<td id="imageCell" runat="server"/>
In your code behind:
Image img = new Image();
img.ImageUrl = "mypic.jpg";
imageCell.Controls.Add(img);