Google Search

Google
 

Saturday, December 8, 2007

Sending Email with ASP.NET 2.0

In ASP.Net 2.0 use System.Net.Mail class.

System.Net.Mail is the namespace used to send email if you are using the 2.0 (or higher) .NET Framework.

Unlike System.Web.Mail, which was introduced in the 1.0 Framework, it is not built upon the CDO/CDOSYS libraries. Instead it is written from the ground up without any interop. Thus, it is not dependant upon other COM libraries. System.Net.Mail introduces brand new classes for creating and sending email.

Although some functionality has been removed, the new System.Net.Mail namespace is much more versatile than the older CDO dependant System.Web.Mail.

Dim message As MailMessage = New MailMessage()
message.From = New MailAddress("from@server.com", "From Name")

message.ReplyTo = New MailAddress("reply@server.com", "ReplyTo Name")

message.To.Add(New MailAddress("to1@server.com", "Name One"))
message.To.Add(New MailAddress("to2@server.com", "Name Two"))
message.CC.Add(New MailAddress("cc@server.com", "CC Name"))
message.Bcc.Add(New MailAddress("bcc@server.com", "BCC Name"))

message.Subject = "Subject"
message.Body = "This is a plain text"
message.IsBodyHtml = False

'Priority property can have next values: Low, Normal and High.
message.Priority = MailPriority.Normal

Dim emailClient As New SmtpClient("mail.server.com")
emailClient.Send(message)

Thursday, December 6, 2007

Dynamically Adding Meta Tags in ASP.NET 2.0

Custom Adding Title Tag :
Me.Header.Title = "Title Of Page Here"

Custom Adding Meta Tag :
Dim metaDescription As New HtmlMeta()
metaDescription.Name = "description"
metaDescription.Content = "A description of the page here."
Me.Header.Controls.Add(metaDescription)

Custom Adding Style :
Dim styles As New HtmlGenericControl("style")
styles.Attributes.Add("type", "text/css")
styles.InnerText = "p { font-weight: bold; }"
Me.Header.Controls.Add(styles)

Dim style As New Style()
style.ForeColor = System.Drawing.Color.Navy
style.BackColor = System.Drawing.Color.LightGray
Me.Header.StyleSheet.CreateStyleRule(style, Nothing, "body")

Custom Adding Css Style:
Dim cssLink As New HtmlLink()
cssLink.Href = "styles.css"
cssLink.Attributes.Add("rel", "Stylesheet")
cssLink.Attributes.Add("type", "text/css")
Me.Header.Controls.Add(cssLink)

Custom Adding Java Script:
Dim javaScript As New HtmlGenericControl("script")
javaScript.Attributes.Add("type", "text/javascript")
javaScript.InnerText = "alert('Hello World!');"
Me.Header.Controls.Add(javaScript)

All the above code should be used within page Page_Load event

Sunday, December 2, 2007

Find ASP.NET Child Controls

To find out child control from parent control we use "$" acts as the delimiter.

Syntax for find out child control from parent control is parentControlID$childControlID.

Here, I give example to DefaultFocus attribute of the < form > element to set the focus to a TextBox nested within a FormView control.

< form id="form1" runat="server" DefaultFocus="vwTest$txtName" >
< div >
< asp:FormView ID="vwTest" runat="server" >
< ItemTemplate >
Name:
< asp:TextBox ID="txtName" runat="server"
Text='<%# Eval("FirstName") + " " + Eval("LastName") %>' / >
< /ItemTemplate >
< /asp:FormView >
< /div >
< /form >

Notice that the DefaultFocus attribute refers to the parent control first (the FormView) and then to the child control (the TextBox) using the vwTest$txtName syntax.

Find ASP.NET Child Controls

To find out child control from parent control we use "$" acts as the delimiter.

Syntax for find out child control from parent control is parentControlID$childControlID.

Here, I give example to DefaultFocus attribute of the < form > element to set the focus to a TextBox nested within a FormView control.

< form id="form1" runat="server" DefaultFocus="vwTest$txtName" >
< div >
< asp:FormView ID="vwTest" runat="server" >
< ItemTemplate >
Name:
< asp:TextBox ID="txtName" runat="server"
Text='<%# Eval("FirstName") + " " + Eval("LastName") %>' / >
< /ItemTemplate >
< /asp:FormView >
< /div >
< /form >

Notice that the DefaultFocus attribute refers to the parent control first (the FormView) and then to the child control (the TextBox) using the vwTest$txtName syntax.

Thursday, November 29, 2007

ASP.NET HyperLink & Mailto In Details View

First Try Out this below Hyperlink and watch Out Put :

< asp:HyperLink NavigateUrl="mailto:" Text=’< %# Bind(”Email”) % >‘ runat=”server” ID=”hlEmail”/ >

This leaves a hyperlink but with a blank mailto - i.e. test@techtoolblog.com


So I assume if I stick in the bind (or eval) text to the Navigate URL we should be good to go:

< asp:HyperLink NavigateUrl=’mailto:< %# Bind(”Email”) % >‘ Text=’< %# Bind(”Email”) % >‘ runat=”server” ID=”hlEmail”/ >


This gives me a link that is NOT clickable.

My next guess is that this has something to do with formatting string, sure enough it is: This is the solution that ended up working for me:


< asp:HyperLink NavigateUrl=’< %# Bind(”Email”, “mailto:{0}”) % >‘ Text=’< %# Bind(”Email”) % >‘ runat=”server” ID=”hlEmail”/ >

Useful Tips for Master Page Control access in Java Csript

When we use master page in ASP.Net it will append the Content Placeholder Id with the ID of the contained controls.i.e when there is a textbox with ID “txtAccountNumber” it will be rendered as “ctl00_ContentPlaceHolder1_txtAccountNumber” where “ContentPlaceHolder1” is the ID of the Content Placeholder of the Master Page. This makes us to use the ID “ctl00_ContentPlaceHolder1_txtAccountNumber” whenever we want to access it in JScript. We can access the textbox in Jscript like,

document.getElementById(' ctl00_ContentPlaceHolder1_txtAccountNumber ') ;

But the above scenario will leads to script error if we unknowingly change the ID of the ContentPlaceholder in master Page. So whenever we change the ContentPlaceholder ID we need to revisit all the Jscript code and change the corresponding ID’s. To prevent this rework we can register a hidden control from code behind which in turn will hold the client ID of the control.

Page.RegisterHiddenField("hdnAccountNoTextID", txtAccountNumber.ClientID);

Now we can make use of this hidden variable to get the client ID of the textbox in Jscript.

//gets the Client ID of the txtAccountNumber textbox
var AccountNoTextID = document.getElementById('hdnAccountNoTextID').value;

//Access the txtAccountNumber textbox
var AccountNoText = document.getElementById('hdnAccountNoTextID');

By doing this we can prevent the rework even if someone changes the ContentPlaceholder ID in future our script will be executing without any error.
The same scenario applies when we use a control inside user control.

Friday, August 31, 2007

MaxLength in TextBox control in ASP.Net

When I was use TextBOx with TextMode "MultiLine" and MaxLength "10".I can enter more than 10 characters. SO I use Java Script for that and it solve my problem.

JavaScript like this :

< script language="javascript" >
function limitCharsLength(Object, MaxLen)
{
return (Object.value.length <= MaxLen-1);
}
< /script >


and TextBox like this :

< asp:TextBox ID="TextBox1" runat="server" Height="173px" TextMode="MultiLine"
Width="369px" onkeypress="javascript: return limitCharsLength(this,5);"
onblur="javascript: limitCharsLength(this, 5)"
onchange="javascript : return limitCharsLength(this, 5)" >< /asp:TextBox >