c# - 文本更改事件未触发

标签 c# asp.net button textbox

我有 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">

最佳答案

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*/
    }
  }

关于c# - 文本更改事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20779766/

相关文章:

c# - 加密 web.config 文件

javascript - 如何在 Google Maps JS API v3 中自定义 mapTypes 按钮的显示

android - 在 Eclipse 中制作所需的布局

ios - 如何使按钮触摸敏感

c# - 投票有什么问题?

c# - 如何在文本框 C# 中显示组合框选择的值?

c# - 为什么非逐字字符串不能包含换行符?

c# - asp.net mvc批处理(外部线程)挂起,也许超时?

javascript - C# javascript 通过标签和属性点击按钮

javascript - ASP.NET 服务器端 Javascript