TextBox TextChanged Events not firing within a gridview
I've got a databound grid view within a UpdatePanel.
There are textboxes in the template fields which are dynamically created with a for loop. I have a textChange event associated with each textbox, but the event isn't being fired. Please help me with it.
Here's the ASP code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<table width="100%">
<%--table for the grid view of buttons--%>
<tr>
<td align="center">
<asp:GridView ID="gvJV" runat="server" AutoGenerateColumns="False"
Height="100%" Width="749px" >
<Columns>
<asp:TemplateField HeaderText="Account">
<ItemTemplate>
<ajaxToolkit:ComboBox ID="AccountId" runat="server" AutoPostBack="false">
</ajaxToolkit:ComboBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="AccountId"
Display="Dynamic" ErrorMessage="Select Account" ForeColor="Red" InitialValue="-1">*</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnSelect" runat="server" ImageUrl="~/images/icons/edit.png" AutoPostBack="false"
ImageAlign="AbsMiddle" OnClientClick='hdCallerRowID.value = this.parentElement.parentElement.rowIndex-1;' />
<ajaxToolkit:ModalPopupExtender ID="gv_ModalPopupExtender" runat="server" TargetControlID="btnSelect"
PopupControlID="pnlSelectCOA" CancelControlID="btnCancel" BackgroundCssClass="modalBackground">
</ajaxToolkit:ModalPopupExtender>
</ItemTemplate>
开发者_如何学运维 </asp:TemplateField>
<asp:TemplateField HeaderText="Memo">
<ItemTemplate>
<asp:TextBox ID="txtMemo" runat="server" BorderStyle="None" AutoPostBack="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Debit">
<ItemTemplate>
<asp:TextBox ID="txtDebit" runat="server" AutoPostBack="false" OnTextChanged="txtDebit_textChanged" comm></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Credit">
<ItemTemplate>
<asp:TextBox ID="txtCredit" AutoPostBack="false" EnableViewState="true" runat="server" OnTextChanged="txtDebit_textChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</asp:UpdatePanel>
and here's the code at back end
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ERP.Controller;
using System.Data;
using AjaxControlToolkit;
using MERP.WebUI.Code;
using ERP.Properties;
namespace MERP.WebUI.Account.GL
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtCalendar_CalendarExtender.SelectedDate = DateTime.Now.Date;
BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.Add("Dummy");
for (int i = 0; i < 10; i++)
dt.Rows.Add("");
dt.AcceptChanges();
gvJV.DataSource = dt;
gvJV.DataBind();
}
private void SaveForm()
{
int debitTotal = 0;
int creditTotal = 0;
foreach (GridViewRow gvRow in gvJV.Rows)
{
TextBox txtDebit = (TextBox)gvRow.FindControl("txtDebit");
if (txtDebit.Text != string.Empty)
debitTotal += Convert.ToInt32(txtDebit.Text.Trim());
TextBox txtCredit = (TextBox)gvRow.FindControl("txtCredit");
if (txtCredit.Text != string.Empty)
creditTotal += Convert.ToInt32(txtCredit.Text.Trim());
}
if (debitTotal != creditTotal)
((Authenticated)Master).SetMessage("NOT EQUAL");
else
((Authenticated)Master).SetMessage("done successfully");
}
protected void gvJV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ComboBox AccountId = (ComboBox)e.Row.FindControl("AccountId");
Common.BindAccounts(AccountId);
TextBox txtMemo = (TextBox)e.Row.FindControl("txtMemo");
}
}
protected void txtDebit_textChanged(object sender, EventArgs e)
{
GridViewRow row = ((GridViewRow)((TextBox)sender).NamingContainer);
//NamingContainer return the container that the control sits in
TextBox other = (TextBox)row.FindControl("txtCredit");
other.Text = "";
}
protected void btnCancel_Click(object sender, EventArgs e)
{
}
protected void btnSave_Click(object sender, EventArgs e)
{
SaveForm();
}
}
}
I had the same problem. I resolved it by setting the AutoPostBack
of the TextBox to true. Therefore the code should be:
<ItemTemplate>
<asp:TextBox ID="txtMemo" runat="server" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
精彩评论