TextBox TextChanged 事件不在网格视图内触发
我在 UpdatePanel 中有一个数据绑定网格视图。
模板字段中有一些文本框,它们是使用 for 循环动态创建的。 我有一个与每个文本框关联的 textChange 事件,但该事件没有被触发。 请帮我解决一下。
这是 ASP 代码:
<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>
这是后端代码
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'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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也有同样的问题。我通过将 TextBox 的 AutoPostBack 设置为 true 解决了这个问题。因此代码应该是:
I had the same problem. I resolved it by setting the
AutoPostBack
of the TextBox to true. Therefore the code should be: