【问题标题】:Text Changed Event Not Firing文本更改事件未触发
【发布时间】:2013-12-26 05:48:24
【问题描述】:

我有 2 个下拉列表、1 个单选按钮和 1 个文本框以及一个按钮。我试图在下拉菜单中禁用按钮,单选按钮未与空文本框一起选择。我能够禁用下拉和单选按钮的按钮并将消息显示为“无效选择”,为此我已经编写了有关选定索引更改事件甚至按钮单击事件的代码,并且它工作正常。但是当文本框为空时,我无法禁用按钮。仅当我在文本框中键入内容以及尝试单击按钮时,才希望启用此按钮,当文本框为空时,我需要显示一条消息,说“请输入评论”。我也尝试过 TextBox 的 Text Changed Event,但它没有触发。请任何人告诉我如何使用标志将所有这些放在按钮单击事件中。

注意:单击按钮时会显示 2 条错误消息。这应该与分配标志一起循环。

到目前为止,我已经尝试过了,

按钮点击代码:

protected void BtnSave_Click(object sender, EventArgs e)
    {
        if (DrpForYear.SelectedItem.Text == "Please Select" || DrpForMonth.SelectedItem.Text == "Please Select" || RadView.SelectedItem.Text == "")
        {
            LblErr.Text = "Invalid Selection!!!";
            LblErr.Visible = true;
            BtnSave.Enabled = false;
            BtnSave.BackColor = Color.Gray;
            BtnSave.ForeColor = Color.Red;
        }

        else
            {
                DTO objc = new DTO();

                int Flag = 0;

                LblLogdInUsername.Text = Session["Username"].ToString();
                objc.LogdInUsername = LblLogdInUsername.Text;

                objc.DateTime = DateTime.Now;

                objc.Comments = Server.HtmlEncode(this.TxtComments.Text);

                objc.Company = LblCompany.Text;

                LblName.Text = Session["Name"].ToString();
                objc.Name = LblName.Text;

                objc.Year = DrpForYear.SelectedItem.Text;

                objc.Month = DrpForMonth.SelectedItem.Text;

                objc.ViewPreference = RadView.SelectedItem.Text;


                int X = obj.InsertButtonComment(objc);

                if (X >= 0)
                {
                    Flag = 1;
                }

                else
                {
                    Flag = 0;
                }

                if (Flag == 1)
                {
                    LblSuccess.Visible = true;
                    LblSuccess.Text = "Comment Saved";
                    LblErr.Visible = false;
                    BtnSave.Enabled = true;
                }
                else
                {
                    LblErr.Visible = true;
                    LblErr.Text = "Failed To Save Comment!!!";
                    LblSuccess.Visible = false;
                }

                objc.LogdInUsername = Convert.ToString(Session["LogdInUsername"]);
                DataSet GrdVC = obj.GetButtonComment(objc);
                DataView GrdViewC = new DataView();
                GrdViewC.Table = GrdVC.Tables[0];
                gvData.DataSource = GrdViewC;
                gvData.DataBind();

                TxtComments.Text = "";
                DrpForYear.ClearSelection();
                DrpForMonth.ClearSelection();
                RadView.Text = "";
         }
    }

DDL 选定索引代码:

    protected void DrpForYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DrpForYear.SelectedItem.Text == "Please Select")
        {
            LblErr.Text = "Invalid Selection!!!";
            LblErr.Visible = true;
            BtnSave.Enabled = false;
            BtnSave.BackColor = Color.Gray;
            BtnSave.ForeColor = Color.Red;
        }

        else
        {
            BtnSave.Enabled = true;
            BtnSave.BackColor = ColorTranslator.FromHtml("#666666");
            BtnSave.ForeColor = Color.White;
        }
    }

    protected void DrpForMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DrpForMonth.SelectedItem.Text == "Please Select")
        {
            LblErr.Text = "Invalid Selection!!!";
            LblErr.Visible = true;
            BtnSave.Enabled = false;
            BtnSave.BackColor = Color.LightGray;
            BtnSave.ForeColor = Color.Red;
        }

        else
        {
            BtnSave.Enabled = true;
            BtnSave.BackColor = ColorTranslator.FromHtml("#666666");
            BtnSave.ForeColor = Color.White;
        }
    }

文本框更改事件代码:

    protected void TxtComments_TextChanged(object sender, EventArgs e)
    {
        if (TxtComments.Text == "")
        {
            LblErr.Text = "Please Enter a Comment!!!";
            LblErr.Visible = true;
            BtnSave.Enabled = false;
            BtnSave.BackColor = Color.LightGray;
            BtnSave.ForeColor = Color.Red;
        }

        else if (TxtComments.Text != "")
        {
            BtnSave.Enabled = true;
            BtnSave.BackColor = ColorTranslator.FromHtml("#666666");
            BtnSave.ForeColor = Color.White;
        }
    }   

aspx代码:

<asp:TextBox ID="TxtComments" runat="server" BorderColor="#666666" BorderWidth="1px" 
 Font-Names="Calibri" Font-Size="Small" ForeColor="#034599" Height="106px" TextMode="MultiLine" Width="617px" ontextchanged="TxtComments_TextChanged">

【问题讨论】:

  • 这对人们帮助你很有帮助......如果你是准确的并向我们展示问题的代码的确切部分......而不是粘贴你的完整代码并期望人们阅读
  • 能否请您发布设计代码(aspx 文件)?
  • @iJay 感谢您的回复。我只粘贴了那些对其他人有用的代码,以了解我想要获得什么以及如何获得。事实上,我在帖子中已经清楚地提到了这个问题。
  • @Sudhakar Tillapudi 是的,肯定会更新我的帖子

标签: c# asp.net button textbox


【解决方案1】:

1.您需要将TextBoxAutoPostBack属性设置为True

2.在比较输入 StringEmptyString 时,您需要输入 Trim 以便删除 whitespaces

您可以使用String.IsNullOrWhiteSpace() 来检查nullemptywhitespaces。 试试这个:

设计代码

<asp:TextBox ID="TxtComments" runat="server" OnTextChanged="TxtComments_TextChanged"   
AutoPostBack="True"></asp:TextBox>

代码隐藏:使用Trim()函数

   protected void TxtComments_TextChanged(object sender, EventArgs e)
    {
        if (TxtComments.Text.Trim().Equals(""))
        {
            LblErr.Text = "Please Enter a Comment!!!";
            LblErr.Visible = true;
            BtnSave.Enabled = false;
            BtnSave.BackColor = Color.LightGray;
            BtnSave.ForeColor = Color.Red;
        }    
        else
        {
            BtnSave.Enabled = true;
            BtnSave.BackColor = ColorTranslator.FromHtml("#666666");
            BtnSave.ForeColor = Color.White;
        }
    }     

使用String.IsNullOrWhiteSpace()函数

 protected void TxtComments_TextChanged(object sender, EventArgs e)
    {
        if (String.IsNullOrWhiteSpace(TxtComments.Text))
        {
            LblErr.Text = "Please Enter a Comment!!!";
            LblErr.Visible = true;
            BtnSave.Enabled = false;
            BtnSave.BackColor = Color.LightGray;
            BtnSave.ForeColor = Color.Red;
        }    
        else
        {
            BtnSave.Enabled = true;
            BtnSave.BackColor = ColorTranslator.FromHtml("#666666");
            BtnSave.ForeColor = Color.White;
        }
    }     

解决方案 2: 将 TextBox 错误消息显示为第一个错误

protected void BtnSave_Click(object sender, EventArgs e)
{
   if (TxtComments.Text.Trim().Equals(""))
    {
        LblErr.Text = "Please Enter a Comment!!!";
        LblErr.Visible = true;
        BtnSave.Enabled = false;
        BtnSave.BackColor = Color.LightGray;
        BtnSave.ForeColor = Color.Red;
    }    
   else if (DrpForYear.SelectedItem.Text == "Please Select" || DrpForMonth.SelectedItem.Text == "Please Select" || RadView.SelectedItem.Text == "")
    {
        LblErr.Text = "Invalid Selection!!!";
        LblErr.Visible = true;
        BtnSave.Enabled = false;
        BtnSave.BackColor = Color.Gray;
        BtnSave.ForeColor = Color.Red;
    }

    else
    {
          /*your code*/
    }
  }

【讨论】:

  • 谢谢伙计,我完全忘记了使用 AutoPostBack。会尝试并让你知道。并感谢您的“修剪”:)
  • 需要更多帮助。当 TextBox 为空时,我试图在按钮单击时显示错误消息。我需要消息为“请输入评论”,但现在我得到的是“无效选择”,只是因为循环放置在 BtnSave 中。那么你能告诉我如何单独显示它
  • @Arjun:您需要在 btnSave 点击事件中添加空检查作为第一个检查,请参阅我编辑的答案solution2
  • 非常感谢。我只是颠倒了您的解决方案2 我需要将无效选择作为第一个错误消息。现在一切正常。
【解决方案2】:

你需要设置

TxtComments.AutoPostBack= true

在代码后面

或者

文本框设计页面中的AutoPostBack="True"

这样

<asp:TextBox ID="TxtComments" runat="server" AutoPostBack="True"></asp:TextBox>   

【讨论】:

    【解决方案3】:

    将 Autopostback 设置为 true

    <asp:TextBox ID="TxtComments" runat="server" BorderColor="#666666" BorderWidth="1px" Font-Names="Calibri" Font-Size="Small" ForeColor="#034599" Height="106px" TextMode="MultiLine" Width="617px" ontextchanged="TxtComments_TextChanged" AutoPostBack="true">
    

    【讨论】:

      猜你喜欢
      • 2011-01-14
      • 2010-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多