Saturday, April 18, 2009

Sending Formatted emails

There are more than one ways to achieve same goal. If you are required to send out email that looks like a fully formatted html page.
a simple approach will be to put the html code in string at codebehind appending the more html tags completing the layout.

something like :
Dim emailBody As StringBuilder = New StringBuilder
emailBody.Append("<tr><td style='font-size:10pt' align='right'><strong> EMail:</strong></td><td style='font-size:12pt' align='left'> " & txtEmail.Text & "</td></tr>")
emailBody.Append("<tr><td style='font-size:10pt' align='right'><strong> Comments:</strong></td><td style='font-size:12pt' align='left'> " & txtComments.Text & "</td></tr>")

sometime things might get messed up.
there is a major drawback in this approach you can't move to the design view in this aproach.

Another approach can be that if you design the template on aspx page design view the fill up the template with data and get the html code for that at runtime and send it in email.
for this you are required to use htmltextwriter.
following steps will do the job:
  1. Place your template in panel, keep its visiblity false.
  2. when email is required to be sent fill up the template with the required information.
  3. use the following code.

Dim stringWriter As New System.IO.StringWriter()

Dim htmlWriter As New System.Web.UI.HtmlTextWriter(stringWriter)

pnlTemplate.RenderControl(htmlWriter)

dim emailbodyhtml = stringWriter.ToString().Trim()

You will be using emailbodyhtml in the email something like

Dim msg As MailMessage = New MailMessage()

msg.Body = emailbodyhtml

msg.IsBodyHtml = True

Important: for some controls especially datacontrols you might get exception like

Control 'controlname' of type 'control type' must be placed inside a form tag with runat=server.

to solve this we are required to override the servercontrol verifictaion

Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

Return

End Sub

hope this could be helpful.

Tuesday, April 14, 2009

Fixing Ajax Model Popup layout

There is a document type tag in our asp.net pages which we normally donot even consider as important by default VS2005 gererates this like:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd

The purpose of this tag is to obey a specific DTD for the document, simply use only valid tags in a valid hirarchy.
As i mentioned earlier VS2005 by default generates html 1.0, some time it is required to use more recent versions like html4.0 for more detail Why use HTML 4.0? .
so we change document type tag to this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



Now coming to the topic if one use a ModalPopup on a asp.net content page for example we have a master page

1- common.Master {with document type html 4.0}

2-default.aspx (content page inheriting common.master)



when one use modal popup the asp.net ajax control on default .aspx, you might face the issue that

the" pop up that was supposed to overlap the original page with fading effect on the original pge" appears on the
content area only i.e only covers the contenttemplate of mastermage.

in order to fix this issue just change the document type in master page back to html 1.0.

resource: http://forums.asp.net/t/1013301.aspx


Friday, April 10, 2009

Need to access printer directly?

some time there is a requirement of printing a hard copy of content on web pages,"window.print()" function works but only for the page which is being currently viewed.
If you want to print that data which not on the webpage then what's the way!
when i got stuck into this situation, i started looking for a solution at google.com
one of the approaches i came across is that to use System.Drawing.Printing.PrintDocument is the options .... if your application is on a Intranet and application users have access to the printer that is directly connected to the application server right!
check this article
A .NET Text Printing Class... That Works! by Karl Moore
"In simple words If you want to implement this on website that is hosted by a web hosting so, that not the solution for you"

What if
your requirnment is to print the content at the printer attached to the client PC then i found information about the microsoft activeX control nemed RSClientPrint, check this for issues.
Its basically a part of Microsoft sql server reporting service but can be used in custom application.


Saturday, April 4, 2009

Indentation in list item text

While working with dropdownlist control once i was required to dispaly indented text for specific list items, in many of my attempts to achieve this i failed the continueing in search of solution i found this and really a very simple one:

ddl.Add(New ListItem(Server.HtmlDecode("nbsp;nbsp;") & "Maincat1","Maincat1"))
ddl.Add(New ListItem("subcat1-1"))
ddl.Add(New ListItem("subcat1-2"))
ddl.Add(New ListItem(Server.HtmlDecode("nbsp;nbsp;") & "Maincat2","Maincat2"))
ddl.Add(New ListItem("subcat2-1"))
ddl.Add(New ListItem("subcat2-2"))

*ddl is the name dropdownlist on my aspx page.

browser caps

Preventing caching of webpage

working on dynamic web pages you may come across a situation where one might start cursing the
web browser back button, because the the obsolete (in that context) page data might become a headache for you.
when i started finding solution to such issue to me java script was the first choice for me, just clearing the browser history using history.go(-1), but for more better approaches i got information about the use of meta tag for that purpose.

the two types are PRAGMA NO-CACHE and cache-control

as all meta tags are required to added in header
i.e
<html>
<head>
meta tags here!
</head>
</html>

try this :
<meta equiv="PRAGMA" content="NO-CACHE">
or
use with:
<meta equiv="PRAGMA" content="NO-CACHE">
<meta equiv="Expires" content="-1">


if doesn't work try the cache control tag
<meta equiv="CACHE-CONTROL" content="NO-CACHE">
if things don't come your way this will surely work for asp.net pages:

Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1))
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.Cache.SetNoStore()

Resources:
http://support.microsoft.com/kb/222064
Caching Tutorial
ASP.Net forum post