Change Page Title Dynamically and Clear TextBox Value With JavaScript

Introduction

This article helps you with different type of solution like changing the page title dynamically and clearing a TextBox value using "jQuery" and "JavaScript" in ASP.NET. Here I explain each and every topic separately.

  • Change Page Title Dynamically with JavaScript in ASP.NET
     

Here I will explain how to change or set a page title dynamically with JavaScript in ASP.NET.

In the following script I simply use a JavaScript function with the name "changeTitle()" to change the title of the "Default.aspx" page.

Example

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Welcome</title>

<script type="text/javascript">

    function changeTitle() {

        document.title = 'New Title';

        }

</script>

 </head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>

   

    </div>

    </form>

</body>

</html>


Now I call the "changeTitle()" function above on the button click to do that  I simply double-clicked on the ASP button and wrote the specified code.

protected
void Button1_Click(object sender, EventArgs e)

{

    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "call me", "changeTitle()", true);

}

Output

Before the button click the title looks like:

before-button-click.jpg
 

After the button click:

after-button click.jpg 

  • Clear TextBox value with jQuery in ASP.NET

Here I will explain how to clear a TextBox value using jQuery.

The following is the jQuery code to clear all TextBox values:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

    <!-- This script For a All textboxes-->

<script type="text/javascript" lang="js">

    $(function () {

        $('#Button1').click(function () {

            $('input[type=text]').each(function () {

                $(this).val('');

            })

        })

    })

jQuery code to clear specific TextBox values:

  <script type="text/javascript" lang="js">

    $(function () {

        $('#Button2').click(function () {

           $('#TextBox1').val('');

        })

    })

</script>

Now I write the complete example to do both of the preceding (clear specific and clear all textbox values) things.

Here, we have two ASP textboxes and two buttons, "Button1" clears all TextBox values and "Button2" clears a specific (in other words "TextBox1") value.

"Default.aspx"

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Welcome</title>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
 

    <!-- This script For a All textboxes-->
 

<script type="text/javascript" lang="js">

    $(function () {

        $('#Button1').click(function () {

            $('input[type=text]').each(function () {

                $(this).val('');

            })

        })

    })

</script>

 

    <!-- This script For a  TextBox1-->
 

    <script type="text/javascript" lang="js">

    $(function () {

        $('#Button2').click(function () {

           $('#TextBox1').val('');

        })

    })

</script>

      <!-- For a specific  textbox-->

 </head>

<body style="top: 28px; left: 3px; position: absolute; height: 26px; width: 828px">

    <form id="form1" runat="server">

        <p>

        <asp:Button ID="Button1" runat="server" Text="Clear All Textboxes Value" style="top: 139px; left: 5px; position: absolute; height: 29px; width: 121px" BackColor="#FF9933"/>

   

            <asp:Label ID="Label1" runat="server" BackColor="#CCFF66" style="top: 16px; left: 22px; position: absolute; height: 19px; width: 101px" Text="TextBox 1"></asp:Label>

   

            <asp:TextBox ID="TextBox1" runat="server" style="top: 5px; left: 165px; position: absolute; height: 31px; width: 128px"></asp:TextBox>

        </p>

    <div>

        <br />

        <br />

        <br />

   

    </div>

        <p>

   

            <asp:TextBox ID="TextBox2" runat="server" style="top: 70px; left: 165px; position: absolute; height: 31px; width: 128px"></asp:TextBox>

        </p>

        <p>

            <asp:Label ID="Label2" runat="server" BackColor="#CCFF66" style="left: 22px; position: absolute; height: 19px; width: 101px; top: 81px" Text="TextBox 2"></asp:Label>

        <asp:Button ID="Button2" runat="server" Text="Clear Textbox1 Value" style="top: 138px; left: 150px; position: absolute; height: 29px; width: 137px" BackColor="#FF9933"/>

   

        </p>

    </form>

    </body>

</html>


Output

When you run the "Default.aspx" page then it provides two functionalities.

The first one, using "Button1", clears all the text box values.

clear-all-textbox-value.gif

The second one, using "Button2", clears all specific TextBox values, in other words it only clears the "TextBox1" value.

clear-specific-textbox.gif