How to use TextChanged event of TextBox with AutoPostBack in ASP.NET

January 30, 2014 0 Comments

ASP.NET application works on AutoPostBack model, i mean, if we want to run code behind event like TextChanged event then we must set AutoPostBack property is true.
  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        Search Item<asp:TextBox ID="TextBox1" runat="server" 
            AutoPostBack="True" Width="174px" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
        <br />
        <br />
    <asp:Label ID="Label1" runat="server"></asp:Label>
Code Behind Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    protected void TextBox1_TextChanged(object sender, EventArgs e)
        string[] arr = { "jacob", "lefore", "martin" };

        foreach (string item in arr)
if (item==TextBox1 .Text)
            Label1.Text = item;



This example shows that TextChanged event occurs when user focus out from the TextBox. You can search item on TextChanged event.

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google