Saturday, August 7, 2010

Sending Email With BCC and Attachment in ASP.NET

Let’s see how to send Email with BCC and attachment with ASP.NET.

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Net;

using System.Net.Mail;

using System.IO;

public partial class sendmail : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btnmail_Click(object sender, EventArgs e)

{

{

string mAttach;

MailMessage msg = new MailMessage();

MailAddress ma = new MailAddress("from email-ID", "Name");

msg.From = ma;

if (txtTo.Text.Trim().Length != 0)

{

msg.To.Add(txtTo.Text);

}

if (txtBcc.Text.Trim().Length != 0)

{

msg.Bcc.Add(txtBcc.Text);

}

if (txtCC.Text.Trim().Length != 0)

{

msg.CC.Add(txtCC.Text);

}

Attachment at = new Attachment(FileResume.PostedFile.InputStream, FileResume.FileName);

msg.Attachments.Add(at);

msg.Subject = txtSubject.Text;

msg.Body = txtMessage.Text;

try

{

SmtpClient smpt = new SmtpClient();

smpt.Host = "Your Server Name";

smpt.Send(msg);

}

catch (Exception ex)

{

}

}

}

}

The System.Net.Mail namespace is the one which contains the classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.

SmtpClient class is the one which allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP).

Mail Message is the one that represents an e-mail message that can be sent using the SmtpClient class.

Attachment is the class that represents an attachment to an e-mail.

Monday, August 2, 2010

Convert Url String to Hyperlinks

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text;
public partial class ReplaceString : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strUrl = MakeUrl("Hello. This is my site http://www.google.com Please Visit.");
Response.Write(strUrl);
}
private static string MakeUrl(string input)
{
var work = input.Split(' ');
var output = new StringBuilder(2 * input.Length);
for (int i = 0; i <>
{
var element = work[i];
if (element.StartsWith("http://"))
{
var site = element.Replace("http://", "");
}
else if (element.StartsWith("www"))
{
var site = element.Replace("http://", "");
}
else if (element.EndsWith(".com"))
{
var site = element.Replace("http://", "");
}
output.Append(element + " ");
}
return output.ToString();
}
}

AutoComplete Extender Content Overridden Issue

While using AutocompleteExtender, we could notice where the other controls are been overridden and the autocomplete extender text being overlapped by the other controls near by.This could be overcome by calling the OnClientShown property of the AutocompleteExtender control.

<cc1:AutoCompleteExtender ID="ace" runat="server" OnClientShown="ShowOptions">

cc1:AutoCompleteExtender>

<script language="javascript" type="text/javascript">

function ShowOptions(control, args) {

control._completionListElement.style.zIndex = 10000001;

}

script>

Wednesday, July 28, 2010

Enable/Disable Button Based On Empty TextBoxes Using JavaScript

Add this script to the page

<script language="javascript" type="text/javascript">

function SetButtonStatus(sender, target)

{

if ( sender.value.length >= 12 )

document.getElementById(target).disabled = false;

else

document.getElementById(target).disabled = true;

}

script>

<asp:TextBox ID="txtText" runat="server" onkeyup="SetButtonStatus(this, 'btnButton')">asp:TextBox>

<asp:Button ID="btnButton" runat="server" Text="Button" Enabled="false" />

This JavaScript validates the textbox and enables the button only if the textbox contains 12 characters.

Set Focus to Textbox in Gridview inside Update Panel

Let’s see how to set focus to textbox inside Gridview inside Update Panel.

So the Gridview is designed something like this..

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>

<asp:GridView ID="gvattend" runat="server" CellPadding="5"

AutoGenerateColumns="false" AllowPaging="true" PageSize="50"

GridLines="none" HeaderStyle-CssClass="tableheadbg" BorderStyle="solid" RowStyle-CssClass="nestedtable" AlternatingRowStyle-CssClass="nestedtable1"

BorderColor="#a3b4a0" BorderWidth="1px" Width="100%" TabIndex="6" >

<Columns>

<asp:TemplateField HeaderText="Marks Obtained" >

<ItemTemplate>

<asp:Textbox id="txtmarksobtain" runat="server" Width="150" AutoPostBack="true" OnTextChanged=" txtmarksobtain _TextChanged" >asp:Textbox>

ItemTemplate>

asp:TemplateField>

<asp:TemplateField HeaderText="Percentage" >

<ItemTemplate>

<asp:Textbox id="txtpercentage" runat="server" Width="150" Enabled="false" >asp:Textbox>

<asp:CompareValidator ID="cmp3" runat="server" ControlToValidate="txtpercentage" Operator="DataTypeCheck" Type="Double" ErrorMessage="Enter Numeric Value Alone" Display="dynamic" ValidationGroup="subject">asp:CompareValidator>

ItemTemplate>

asp:TemplateField>

Columns>

<EmptyDataTemplate>

<table width="100%" cellpadding="5" cellspacing="5">

<tr><td align="center" valign="middle" ><span class="redcolor">No Data Foundspan> td>tr>

table>

EmptyDataTemplate>

<RowStyle CssClass="nestedtable" />

<HeaderStyle CssClass="tableheadbg" />

<AlternatingRowStyle CssClass="nestedtable1" />

asp:GridView>

ContentTemplate>

asp:UpdatePanel>

And in the Code Behind:

Protected Sub txtmarksobtain _TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtmaxmarks.TextChanged

For Each gv As GridViewRow In gvattend.Rows

Dim txtmarksobtain As TextBox = DirectCast(gv.FindControl("txtmarksobtain"), TextBox)

If txtmarksobtain.Text = "" Then

ScriptManager.RegisterStartupScript(Me, Me.GetType(), "selectAndFocus", "$get('" + txtmarksobtain.ClientID + "').focus();$get('" + txtmarksobtain.ClientID + "').select();", True)

End If

Next

End Sub

And thus focus is set to textbox inside Gridview

Saturday, July 24, 2010

Prevent Flicker from CollapsiblePanelExtender on PageLoad

Let’s see how to fix the flickering of the CollapsiblePanelExtender when collapsed is set to true (Collapsed="true")

Here is my CollapsiblePanelExtender

<cc1:CollapsiblePanelExtender ID="CollapsePanel1" runat="server" SuppressPostBack="true" CollapseControlID="btnCancel" Collapsed="true" CollapsedText="Show Time" ExpandControlID="imgPostQuestion" ExpandedText="Hide Time" TargetControlID="panelPost"> cc1:CollapsiblePanelExtender>

Your TargetPanel is here

<asp:Panel ID="panelPost" runat="server" CssClass="cpBody">

<div>

//Ur collapsible Content

div>

asp:Panel>

And now we are preventing the flickering by the CSS style and the style is as follows.

.cpBody

{

height:0px;

overflow: hidden;

}

There we found the solution for the flickering..

Convert String To Title Case in C#

Here is the code to convert any string to Title Case in C#.

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;

using System.Text;

using System.Net.Mail;

using System.Text.RegularExpressions;

public partial class lSample : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string strInsName;

strInsName = "hello world";

strInsName = strInsName.ToLower();

strInsName = System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ToTitleCase(strInsName);

Response.Write(strInsName);

}

}

First we need to convert the string to lower.. Only then the System.Globalization.CultureInfo.CurrentUICulture class will convert the string to Title Case.

Handling PostBack on Url Rewriting

So many of us stuck up with the problem of Postback during URL rewriting. During Postback the Url breaks up and throws error. Here is a solution to overcome this. We need to write this script in the last of the page (ie. In The Body Section.)

<script language="javascript">

function NotPostback(sNewFormAction)

{

if(document.layers) //The browser is Netscape 4

{

document.layers['Content'].document.forms[0].action = sNewFormAction;

}

else //It is some other browser that understands the DOM

{

document.forms[0].action = sNewFormAction;

}

}

NotPostback(document.URL);

script>

This calls the same Url on Postback too and thus it is solved..