Word counter desktop application in asp net c sharp

ASP.NET is a powerful programming language that allows developers to create dynamic web applications. One common task in web development is creating a word counter desktop application. In this article, we will explore how to build a word counter application using ASP.NET and C#.

To begin, let's set up our ASP.NET project. Open Visual and create a new ASP.NET Web Application project. Choose the and make sure to select the ASP.NET Core version. Once the project is created, we can start building our word counter application.

First, let's create a simple user interface for our application. We will have a text area where the user can input their text, and a button to trigger the word count. Here's the HTML code for our user interface:

User Interface



Next, let's add some code to handle the button event and count the words. We will use the jQuery library to simplify our code. Here's the JavaScript code:

JavaScript Code


$(document).ready(function() {
    $("#countButton").click(function() {
        var text = $("#textInput").val();
        var wordCount = text.trim().split(/s+/).length;
        alert("Word count: " + wordCount);
    });
});

In the above code, we retrieve the text from the text area, trim any leading or , split the text into an array of words using regular expressions, and finally count the number of words in the array.

Now that we have our user interface and JavaScript code ready, let's it with our ASP.NET project. Create a new ASP.NET Web Form (.aspx) file and add the code:

ASP.NET Code


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

<!DOCTYPE html>
<html >
<head runat="server">
    <title>Word Counter</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="Scripts/wordCounter.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <textarea id="textInput" rows="10" cols="50"></textarea>
            <br />
            <button id="countButton">Count Words</button>
        </div>
    </form>
</body>
</html>

In the above code, we include the necessary ASP.NET markup and reference the jQuery library and our JavaScript code.

Finally, let's implement the server-side logic to handle the button click event. Create a new C# code-behind file for our ASP.NET Web Form and add the following code:

Server-side Code


using System;
using System.Web.UI;

namespace WordCounter
{
    public partial class WordCounter : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            countButton.Click += CountButton_Click;
        }

         void CountButton_Click(object sender, EventArgs e)
        {
            string text = textInput.Value;
            int wordCount = text.Trim().Split(new[] { ' ', 't', 'r', 'n' }, StringSplitOptions.RemoveEmptyEntries).Length;
            ClientScript.RegisterStartupScript(GetType(), "alert", $"alert('Word count: {wordCount}');");
        }
    }
}

In the above code, we handle the button click event by retrieving the text from the text area, trimming any leading or trailing spaces, splitting the text into an array of words using whitespace characters, and finally counting the number of words. We then use the ClientScript.RegisterStartupScript method to display an alert with the word count.

That's it! We have successfully built a word counter desktop application using ASP.NET and C#. By combining client-side JavaScript code with server-side C# code, we can create dynamic and interactive web applications.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents