Google Search

Google
 

Tuesday, January 22, 2008

DateTime Formatting in GridView,DetailView,FormView

In Asp.Net 2.0 direct datetime formatting for bound column not possible.

To format column into datatime you have to set HtmlEncode="False" property of BoundField.


The Reason behind for this is bydefault data is Html Encoded that prevents to format date.So, set HtmlEncode to False.

Saturday, January 5, 2008

File Download dialog box

Code to open a "save as.." file download dialog box in asp.net 2.
Here,I create a vb.net function for to open file download dialog box.

Use System.IO class for file system. Function argument accepts a File virtual path not physical path.

This code for only .txt file but you also use this code for more extension file.For that change only ContentType.

.htm,.html => "text/HTML"
.txt => "text/plain"
.doc,.rtf => "Application/msword"
.csv,.xls => "Application/x-msexcel"
.pdf =>"Application/pdf"


Function DisplayDownloadDialog(ByVal PathVirtual As String)

Dim strPhysicalPath As String
Dim objFileInfo As System.IO.FileInfo
Try
strPhysicalPath = Server.MapPath(PathVirtual)
'exit if file does not exist
If Not System.IO.File.Exists(strPhysicalPath) _
Then Exit Function
objFileInfo = New System.IO.FileInfo(strPhysicalPath)

Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
'Add Headers to enable dialog display
Response.AddHeader("Content-Disposition", "attachment; filename=" & _
objFileInfo.Name)
Response.AddHeader("Content-Length", objFileInfo.Length.ToString())

Response.ContentType = "Text / plain"

Response.WriteFile(objFileInfo.FullName)


Catch
'on exception take no action
'you can implement differently
Finally

Response.End()

End Try
End Function

Wednesday, December 19, 2007

ASP.NET Ajax Control (TextBoxWatermarkExtender,ValidatorCalloutExtender)

Here I demonstrate how to use AJAX Control TextBoxWatermarkExtender and ValidatorCalloutExtender in ASP.NET.


< asp:TextBox ID="txtFname" runat="server"/ >
< asp:RequiredFieldValidator ID="rfvFname" runat="server" ErrorMessage="Enter First Name"
ControlToValidate="txtFname" Display="None" ValidationGroup="vgCheck"/ >
< ajaxToolkit:TextBoxWatermarkExtender ID="twFname" runat="server" TargetControlID="txtFname"
WatermarkText="Enter First Name"/ >
< ajaxToolkit:ValidatorCalloutExtender ID="vcFname" runat="server" TargetControlID="rfvFname"/ >
< br / >
< asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="vgCheck" / >


TextBoxWatermarkExtender : This Ajax control attached to Textbox control to get WaterMark behavior. When Textbox control is empty it display Watermark text.To attach TextBoxWatermarkExtender to TextBox set TargetControlId of TextBoxWatermarkExtender to particular TextBox.

Here,I set TargetControlID of twFname to txtFname and set WatermarkText for txtFname.
You can also use Css Class with watermark contol.

ValidatorCalloutExtender : This Ajax control enhances the functionality of existing ASP.NET validators. To use this control, add an input field and a validator control as you normally would. Then add the ValidatorCallout and set its TargetControlID property to reference the validator control.

Here, I use rfvFname RequiredFieldValidator control to validate txtFname TextBox control. To use ValidatorCalloutExtender I set TargetControlID to rfvFname RequiredFieldValidator.

So,both control TextBoxWatermarkExtender,ValidatorCalloutExtender ASP.NET AJAX Control is very easy to understand and use to it.

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.