In this article I will explain with an example, how to handle TextChanged event for TextBox inside GridView in ASP.Net using C# and VB.Net.
The TextBox inside GridView will be assigned an OnTextChanged event and when Text inside the TextBox changes, the TextChanged event handler is raised.
HTML Markup
The HTML Markup consists of an ASP.Net GridView containing two BoundField columns and a TemplateField column.
The TemplateField columns consist of a TextBox which has been assigned an OnTextChanged event handler and the AutoPostBack property is set to True.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Customer Id" ItemStyle-Width="80" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120" />
<asp:TemplateField HeaderText="Country" ItemStyle-Width="120">
<ItemTemplate>
<asp:TextBox runat="server" OnTextChanged = "OnTextChanged" Text = '<%# Eval("Country") %>' AutoPostBack = "true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding the ASP.Net GridView control
The GridView is populated with a dynamic DataTable with some dummy data inside the Page Load event.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Implementing TextChanged event of TextBox inside GridView
When the Text inside the TextBox is changed, first the TextBox is referenced and its ID is determined.
Then the updated (changed) Text of the TextBox is displayed using JavaScript Alert Message Box.
C#
protected void OnTextChanged(object sender, EventArgs e)
{
//Reference the TextBox.
TextBox textBox = sender as TextBox;
//Get the ID of the TextBox.
string id = textBox.ID;
//Display the Text of TextBox.
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + textBox.Text + "');", true);
}
VB.Net
Protected Sub OnTextChanged(sender As Object, e As EventArgs)
'Reference the TextBox.
Dim textBox As TextBox = CType(sender, TextBox)
'Get the ID of the TextBox.
Dim id As String = textBox.ID
'Display the Text of TextBox.
ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", "alert('" & textBox.Text & "');", True)
End Sub
Screenshot
Demo
Downloads