【问题标题】:TextChanged event fires but does not run in code behindTextChanged 事件触发但不在后面的代码中运行
【发布时间】:2020-05-03 06:30:34
【问题描述】:

在我的 aspx 文件中,我有以下代码:

    <asp:PlaceHolder ID="phdHolding" runat="server">
        <asp:Label ID="lblWarning" runat="server" Text="" Visible="false" />
        <asp:PlaceHolder ID="phdInput" runat="server" Visible="false">
            <h3><asp:Label ID="lblArea" runat="server" Text="Area" /></h3>
        </asp:PlaceHolder>
    </asp:PlaceHolder>

在 (c#) 后面的代码中,我创建了一个添加到 phdInput 的表。该代码的一部分是在表格单元格中创建和填充文本框(和标签)。需要 TextBox 才能调用 TextChanged 上的函数。

     decimal amount = Convert.ToDecimal(dc.DataScalar("SELECT Item" + rownr + " FROM Companies WHERE CompanyName='" + coy + "' AND BusinessUnit='" + bu + "'"));
     TextBox tb = new TextBox(); tb.ID = "txtAmount" + rownr; tb.Width = 70; tb.Text = amount.ToString("N0");
     tb.AutoPostBack = true; tb.TextChanged += new EventHandler(UpdateAmounts);
     Label lbl = new Label(); lbl.Text= " " + trow["Unit"].ToString();
     TableCell tdamount = new TableCell(); tdamount.Controls.Add(tb); tdamount.Controls.Add(lbl); 
     tr2.Cells.Add(tdamount);

一切正常,一切都显示在页面上。但是,当文本发生更改并且您在文本框外单击或按“Enter”等时,未达到该功能(已在 VS 中检查)并且 phdInput 变为空白。

我之前使用过这个函数,以及调用它的 TextChanged 方法,没有任何问题,为什么它现在不起作用?

【问题讨论】:

  • 您能告诉我们tr2 的定义以及您在表单中添加tr2 的时间吗?
  • 对于行中的第一个单元格: int rownr = Convert.ToInt32(trow["Srl"].ToString());表行 tr2 = 新表行(); tr2.VerticalAlign = VerticalAlign.Top; TableCell tdtype = new TableCell(); tdtype.Text = trow["Type"].ToString(); tdtype.Width = 250; tdtype.Horizo​​ntalAlign = Horizo​​ntalAlign.Left; tr2.Cells.Add(tdtype);

标签: c# asp.net textchanged


【解决方案1】:

我设法重现了一个问题

phdInput 变为空白

这很可疑,如果它消失了,那么动作中肯定有PostBack。尤其是因为phdInput 属性Visible 在表单上设置为false,因此引起了怀疑。

我想表单的初始化是在Page_Load 方法中进行的,其中一行代码是将phdInput 属性Visible 设置为true。但是,phdInputPostBack 被触发后消失了,所以可能有检查

if (!IsPostBack)

在它内部进行初始化,这是正确的,每次页面重新加载时都不需要初始化它。不过

phdInput.Visible = true;

应该在检查 if (!IsPostBack) 之外,以便 phdInput 始终可见。

接下来,有东西触发了PostBack

但是,当文本发生更改并且您在文本框外部单击或 按'Enter'等功能未到达(已签入 VS) 和 phdInput 变为空白。

这听起来像TextChanged 事件有效,它只调用一些其他事件处理程序。

我希望它能帮助您找到解决方案。下面是重现问题的代码phdInput 变为空白(写得不好)。

.aspx - body 标签的内容

<form id="form1" runat="server">
    <div>
        <p>start</p>

        <asp:PlaceHolder ID="phdHolding" runat="server">
            <asp:Label ID="lblWarning" runat="server" Text="" Visible="false" />
            <asp:PlaceHolder ID="phdInput" runat="server" Visible="false" >
                <h3><asp:Label ID="lblArea" runat="server" Text="Area" /></h3>
            </asp:PlaceHolder>
        </asp:PlaceHolder>

        <p>end</p>
    </div>
</form>

背后的代码

using System;
using System.Web.UI.WebControls;

namespace WebApplication8
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    // row 1
                    int rownr = Convert.ToInt32(1);
                    TableRow tr = new TableRow();
                    tr.VerticalAlign = VerticalAlign.Top;
                    TableCell tdtype = new TableCell();
                    tdtype.Text = "table cell 1 text";
                    tdtype.Width = 250;
                    tdtype.HorizontalAlign = HorizontalAlign.Left;
                    tr.Cells.Add(tdtype);

                    decimal amount = Convert.ToDecimal(1);
                    TextBox tb = new TextBox(); tb.ID = "txtAmount" + rownr; tb.Width = 70; tb.Text = amount.ToString("N0");
                    tb.AutoPostBack = true; tb.TextChanged += new EventHandler(UpdateAmounts);
                    Label lbl = new Label(); lbl.Text = " " + "1";
                    TableCell tdamount = new TableCell(); tdamount.Controls.Add(tb); tdamount.Controls.Add(lbl);
                    tr.Cells.Add(tdamount);

                    // row 2
                    int rownr2 = Convert.ToInt32(2);
                    TableRow tr2 = new TableRow();
                    tr2.VerticalAlign = VerticalAlign.Top;
                    TableCell tdtype2 = new TableCell();
                    tdtype2.Text = "table cell 2 text";
                    tdtype2.Width = 250;
                    tdtype2.HorizontalAlign = HorizontalAlign.Left;
                    tr2.Cells.Add(tdtype2);

                    decimal amount2 = Convert.ToDecimal(2);
                    TextBox tb2 = new TextBox(); tb2.ID = "txtAmount" + rownr2; tb2.Width = 70; tb2.Text = amount2.ToString("N0");
                    tb2.AutoPostBack = true; tb2.TextChanged += new EventHandler(UpdateAmounts);
                    Label lbl2 = new Label(); lbl2.Text = " " + "2";
                    TableCell tdamount2 = new TableCell(); tdamount2.Controls.Add(tb2); tdamount2.Controls.Add(lbl2);
                    tr2.Cells.Add(tdamount2);

                    // table
                    var table = new Table();
                    table.Rows.Add(tr);
                    table.Rows.Add(tr2);
                    phdInput.Controls.Add(table);

                    lblWarning.Text = "some warning";
                    lblArea.Text = "some area text";

                    lblWarning.Visible = true;
                    phdInput.Visible = true;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void UpdateAmounts(object sender, EventArgs e)
        {

        }
    }
}

页面打开时

文本框的文本改变后 - 任意一个

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多