You type an ID into an ASP.NET TextBox, expect your C# handler to run, and nothing happens. Before changing your database query or rewriting the page, check how the control sends its value to the server.
This guide explains the common causes of an ASP.NET WebForms TextChanged event not firing and provides a complete example you can adapt.
Applies to: ASP.NET WebForms on .NET Framework. This example uses an ASP.NET Web Application project, not ASP.NET Core, MVC or Blazor.
Quick fix: enable AutoPostBack and wire the handler
Set AutoPostBack="true" and connect OnTextChanged to the correct C# method. Change the text and press Tab to move focus away from the field. AutoPostBack normally submits the changed value when the TextBox loses focus; it does not call your server handler on every keystroke.
With AutoPostBack disabled, the changed value can still raise TextChanged during a later postback, such as a button click. Without a postback, the server has no new request to process. See Microsoft's AutoPostBack documentation.
Complete ASPX example
Create TextChangedDemo.aspx in your WebForms application. Match the namespace and class name to your project.
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="TextChangedDemo.aspx.cs"
Inherits="HumaBlog.TextChangedDemo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>TextChanged Demo</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="LblEntityID" runat="server"
AssociatedControlID="TxtEntityID" Text="Entity ID:" />
<asp:TextBox ID="TxtEntityID" runat="server"
AutoPostBack="true" CausesValidation="false"
OnTextChanged="TxtEntityID_TextChanged" />
<asp:Label ID="LblMessage" runat="server" />
</form>
</body>
</html>
C# code-behind
Add the following to TextChangedDemo.aspx.cs. Visual Studio generates the designer declarations for the server controls in a Web Application project.
using System;
using System.Globalization;
using System.Web.UI;
namespace HumaBlog
{
public partial class TextChangedDemo : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LblMessage.Text = "Enter an entity ID and press Tab.";
}
}
protected void TxtEntityID_TextChanged(object sender, EventArgs e)
{
long entityId;
if (!long.TryParse(TxtEntityID.Text.Trim(), out entityId)
|| entityId <= 0)
{
LblMessage.Text = "Please enter a valid positive entity ID.";
return;
}
LblMessage.Text = "TextChanged fired. Entity ID: "
+ entityId.ToString(CultureInfo.InvariantCulture);
}
}
}
The example accepts a positive numeric ID and displays confirmation. It deliberately performs no database lookup, so you can confirm the event works before adding application logic.
Check that the event handler is connected
The ASPX attribute OnTextChanged="TxtEntityID_TextChanged" must match the method name exactly. The handler takes object sender and EventArgs e.
AutoEventWireup="true" handles conventional page events such as Page_Load; do not rely on it to connect an arbitrary TextBox handler. Use the declarative event attribute shown above, or subscribe explicitly in C#—avoid doing both.
Do not overwrite user input in Page_Load
A common mistake is assigning a default value on every request:
protected void Page_Load(object sender, EventArgs e)
{
TxtEntityID.Text = "1000";
}
On a postback, this assignment can replace the submitted text before your change handler reads it. That can make the event appear broken even when it runs. Put initial values inside if (!IsPostBack) instead:
if (!IsPostBack)
{
TxtEntityID.Text = "1000";
}
Microsoft's page life-cycle overview explains why request timing matters. The IsPostBack property distinguishes an initial page request from a postback.
Test with a genuinely changed value
TextChanged is a change event, not a focus event. Enter a different value from the one previously rendered. Typing the same value again or simply clicking out of an unchanged field should not be used as your test.
Also distinguish a user edit from assigning Text in server code. Programmatic assignment is not a substitute for the posted user-change workflow. Refer to Microsoft's TextChanged reference.
Check JavaScript, validation and UpdatePanel behavior
- No network request: Open browser Developer Tools, change the value and press Tab. Check the Console for JavaScript errors and the Network tab for a postback.
- Validation: The demonstration uses CausesValidation="false" so unrelated validators do not interfere with this field's postback. In production, choose validation settings appropriate to the action and still validate server-side input.
- UpdatePanel: An asynchronous postback still runs the server page life cycle. If the handler runs but the label stays unchanged, check which panel contains the label and its update settings.
- Dynamic controls: Recreate a dynamic TextBox on each request, preferably during Init, with the same ID and parent container. Attach its handler when creating it.
How to verify the fix
- Set a breakpoint inside TxtEntityID_TextChanged.
- Load the page and enter
123. - Press Tab. The handler should run and the label should show the ID.
- Change the value to
456and repeat. - Enter
abcto check the validation message.
If the breakpoint is reached, event wiring works. Continue debugging any database lookup or binding code inside the handler separately.
Frequently asked questions
Does TextChanged run while I type?
Not on each keystroke in this setup. For immediate browser feedback, use a JavaScript input handler. Add a server request only when the feature requires it.
Should I disable ViewState to fix it?
Do not change ViewState as a first troubleshooting step. Start with event wiring, a changed value, the postback request and assignments in Page_Load.
Can I query SQL Server inside the handler?
Yes. After validating the ID, use a parameterized query and check the user's access to the requested record. Keep the event test simple before connecting it to the database.
Final check
For most pages, start with three checks: the TextBox posts back, OnTextChanged points to the correct method, and Page_Load preserves submitted input. The small example above helps isolate the event before you add the rest of your application logic.

0 Comments :
Post a Comment