Archive

Archive for August, 2010

Display Alert Message With Javascript

August 24, 2010 Leave a comment

Hi all,

try this example to Display Alert Message With Javascript

1) Create a new Website Project

2) Add new Class in App_Code folder and name it “Alert.cs”


using System.Web;
using System.Text;
using System.Web.UI;

/// <summary>
/// A JavaScript alert
/// </summary>
public static class Alert
{
/// <summary>
/// Shows a client-side JavaScript alert in the browser.
/// </summary>
/// <param name="message">The message to appear in the alert.</param>
public static void Show(string message)
{
   // Cleans the message to allow single quotation marks
   string cleanMessage = message.Replace("'", "\\'");
   string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";
   // Gets the executing web page
   Page page = HttpContext.Current.CurrentHandler as Page;
   // Checks if the handler is a Page and that the script isn't allready on the Page
   if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
   {
      page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);
   }
}   
}

to use it

Alert.Show("Any Message here.....");

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net, Javascript

How to Check UserName Availability using ASP.Net

August 20, 2010 Leave a comment

Hi all,

in previous posts i provide many examples how to check username Availability

Check UserName Availability with JQuery using Memebership

JQuery to Check User Name Availability from Database

Check UserName Availability with AJAX

Check UserName Availablity

Check UserName Availability with Membership

Check UserName Availability

and now i’m going to provide another way to check username Availability using AJAX with Membership

<asp:ScriptManager ID="sm" runat="server" />
<asp:UpdatePanel runat="server" ID="up1">
  <ContentTemplate>
    Username: <asp:TextBox runat="server" id="Username"
    AutoPostBack="true" OnTextChanged="Username_Changed" />
    <div runat="server" id="UserAvailability"></div><br />
  </ContentTemplate>
</asp:UpdatePanel>

and in code behind :-

protected void Username_Changed(object sender, EventArgs e)
{
  if (Membership.GetUser(Username.Text) != null)
  {
    UserAvailability.InnerText = "Username taken, sorry.";
    UserAvailability.Attributes.Add("class", "taken");
  }
  else
  {
    UserAvailability.InnerText = "Username available!";
    UserAvailability.Attributes.Add("class", "available");
  }
}

and the css


body
{
  font-family: Verdana, Sans-Serif;
  font-size: 10pt;
}

label, input
{
  height: 14px;
  margin-bottom: 5px;
}

label
{
  float: left;
  text-align: right;
  margin-right: 20px;
  width: 75px;
}

input
{
  float: left;
}

br
{
  clear: both;
}

#UserAvailability
{
  padding-left: 22px;
  margin-left: 5px;
  float: left;
  background-position: left;
  background-repeat: no-repeat;
}

Hope this helps

Good Luck

Categories: AJAX, ASP.Net

Find Folder Size

August 20, 2010 Leave a comment

Hi all

try this Example to Find Folder Size using C#

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(FindFolderSize(new DirectoryInfo(Server.MapPath("~")), UnitType.KB, 0).ToString() + " KB");
    Response.Write("<br />");
    Response.Write(FindFolderSize(new DirectoryInfo(Server.MapPath("~")), UnitType.MB, 2).ToString() + " MB");
    Response.Write("<br />");
    Response.Write(FindFolderSize(new DirectoryInfo(Server.MapPath("~")), UnitType.GB, 5).ToString() + " GB");
}
 
public enum UnitType { KB = 1, MB = 2, GB = 3 }
/// <summary>
/// Find foler size
/// </summary>
/// <param name="d">Target folder</param>
/// <param name="u">Unit type [KB, MB, GB]</param>
/// <param name="r">Number to digits to round up</param>
/// <returns></returns>
public double FindFolderSize(DirectoryInfo d, UnitType u, int r)
{
    double divider = Math.Pow(1024, (int)u);
    double size = 0;
    foreach (FileInfo f in d.GetFiles())
        size += Convert.ToDouble(f.Length) / divider;
    foreach (DirectoryInfo c in d.GetDirectories())
        size += this.FindFolderSize(c, u, r);
    return Math.Round(size, r);
}

Hope this helps

Good Luck.

Categories: ASP.Net

Handle DropdownList event in Repeater and Gridview control

August 19, 2010 1 comment

Hi

try this example to handle DropdownList event in Repeater and Gridview control :-

With Repeater Control :-

<asp:Repeater ID="myRepeater" runat="server">
 <ItemTemplate>
  <asp:DropDownList ID="myDropdown" runat="server" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="myDropdown_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
 </asp:Repeater>

In code behind :-

protected void myDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
    // Get the drop down instance as follows
    DropDownList myDropdown = sender as DropDownList;
    // Get the current item index
    int currentItemIndex = ((RepeaterItem)myDropdown.NamingContainer).ItemIndex;
}

With Gridview Control :-

<asp:TemplateField HeaderText="Country">
   <ItemTemplate>
      <asp:DropDownList ID="ddl_country" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_country_SelectedIndexChanged">
      </asp:DropDownList>
   </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State">
   <ItemTemplate>
      <asp:DropDownList ID="ddl_State" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_State_SelectedIndexChanged">
      </asp:DropDownList>
   </ItemTemplate>
</asp:TemplateField>

and in code behind :-

protected void ddl_country_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow grow = (GridViewRow)((Control)sender).NamingContainer;
    DropDownList ddl_country = (DropDownList)grow.FindControl("ddl_country");
    DropDownList ddl_state = (DropDownList)grow.FindControl("ddl_state");
    //-- Bind State DropdownList here
}

Hope this helps

Good Luck

Categories: ASP.Net

Send Email using WebService

August 2, 2010 1 comment

Hi

try this example to Send Email using Web Service

You can check this post Send Email using Gmail Account

1) Open VS2008 and Create a new Website

2)  Add new Web Page and add Three Html input controls of Type Text and ScriptManager Control from ToolBox

<div>
    <asp:ScriptManager ID="ScriptManager" runat="server"
       EnablePageMethods="true" />
   <fieldset id="ContactFieldset">
       <label>
           Your Name
           <input type="text" id="NameTextBox" /></label>
       <label>
           Email Address
           <input type="text" id="EmailTextBox" /></label>
       <label>
           Your Message
           <textarea id="MessageTextBox"></textarea></label>
       <button onclick="SendForm();">
           Send</button>
   </fieldset>
    </div>

3) Add this Javascript code :-


    <script type="text/javascript">
        function SendForm() {
            var name = $get("NameTextBox").value;
            var email = $get("EmailTextBox").value;
            var message = $get("MessageTextBox").value;

            PageMethods.SendForm(name, email, message,OnSucceeded, OnFailed);
        }

        function OnSucceeded() {
            // Dispaly "thank you."
            $get("ContactFieldset").innerHTML = "<p>Thank you!</p>";
        }

        function OnFailed(error) {
            // Alert user to the error.
            alert(error.get_message());
        }
    </script>

4) In code behind add the following code :-


[WebMethod]
    public static void SendForm(string name, string email, string message)
    {
        if (string.IsNullOrEmpty(name))
        {
            throw new Exception("You must supply a name.");
        }

        System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        if (!regex.IsMatch(email))
        {
            throw new Exception("You must Enter Valid Email.");
        }

        if (string.IsNullOrEmpty(email))
        {
            throw new Exception("You must supply an email address.");
        }

        if (string.IsNullOrEmpty(message))
        {
            throw new Exception("Please provide a message to send.");
        }

        // If we get this far we know that they entered enough data, so
        // here is where you would send the email or whatever you wanted
        // to do 🙂
        try
        {
            string mailServerName = "smtp.gmail.com";
            MailMessage messages = new MailMessage(email, <a href="mailto:yourmail@gmail.com">yourmail@gmail.com</a>, "Test", message);
            messages.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = mailServerName;
            smtp.Host = "smtp.gmail.com";
            smtp.EnableSsl = true;
            smtp.UseDefaultCredentials = true;
            System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
            NetworkCred.UserName = <a href="mailto:yourmail@gmail.com">yourmail@gmail.com</a>;
            NetworkCred.Password = "password";
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(messages);
        }
        catch
        {
            throw new Exception("Fail in Sending Email.");
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

Don’t Forget to use this namespace

using System.Web.Services;
using System.Net.Mail;

Hope this helps

Good Luck.

Categories: ASP.Net