TextBox TextChanged事件未在gridview中触发

时间:2011-05-18 08:14:29

标签: c# asp.net gridview 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();
        }

    }
}

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。我通过将TextBox的AutoPostBack设置为true来解决它。因此代码应该是:

<ItemTemplate>
    <asp:TextBox ID="txtMemo" runat="server" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>