Share Blog

Tuesday, November 03, 2015

Convert String to Title Case (Proper Case), Upper Case, Lower Case in Asp.net

To convert text to proper case, upper and lower case in asp.net open your aspx page and write the following code


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CovertCase.aspx.cs" Inherits="CovertCase" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Convert String to Title Case (Proper Case), Upper Case, Lower Case in Asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <b>Enter Text to Change: </b> <asp:TextBox ID="txtinfoDetails" runat="server" Text="hi,welcome to sunil kumar blog"/><br /><br />
<asp:Button ID="BtnTitleCase" runat="server" Text="Title Case"
            onclick="BtnTitleCase_Click"  />
<asp:Button ID="BtnLowerCase" runat="server" Text="Lower Case"
            onclick="BtnLowerCase_Click"  />
<asp:Button ID="BtnUpperCase" runat="server" Text="Upper Case"
            onclick="BtnUpperCase_Click" />
<br /><br />
<asp:Label ID="lblResult" runat="server" Font-Bold="true"></asp:Label>
    </div>
    </form>
</body>
</html>

After completion of adding namespaces you need to write the code like as shown below

using System;
using System.Threading;
using System.Globalization;

public partial class CovertCase : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BtnTitleCase_Click(object sender, EventArgs e)
    {
        CultureInfo culinfo = Thread.CurrentThread.CurrentCulture;
        TextInfo txtinfo = culinfo.TextInfo;
        lblResult.Text = txtinfo.ToTitleCase(txtinfoDetails.Text);
    }
    protected void BtnLowerCase_Click(object sender, EventArgs e)
    {
        lblResult.Text = txtinfoDetails.Text.ToLower();
    }
    protected void BtnUpperCase_Click(object sender, EventArgs e)
    {
        lblResult.Text = txtinfoDetails.Text.ToUpper();
    }
}


Demo





No comments:

Post a Comment