TextBox TextChanged 事件不在网格视图内触发

发布于 2024-11-08 03:41:37 字数 6068 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从此见与不见 2024-11-15 03:41:37

我也有同样的问题。我通过将 TextBox 的 AutoPostBack 设置为 true 解决了这个问题。因此代码应该是:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文