C# 文本更改事件未触发

C# 文本更改事件未触发,c#,asp.net,button,textbox,C#,Asp.net,Button,Textbox,我有两个下拉列表,一个单选按钮和一个文本框以及一个按钮。我试图禁用按钮时,下拉列表,单选按钮没有选择一个空的文本框。我能够禁用下拉按钮和单选按钮,并将消息显示为无效选择,为此,我编写了关于所选索引更改事件的代码,甚至在按钮单击事件及其工作正常的代码。但当文本框为空时,我无法禁用该按钮。希望此按钮仅在我在Textbox中键入内容时启用,并且当我尝试在Textbox为空时单击按钮时,需要显示一条消息,说明“请输入注释”。我也尝试过文本框的文本更改事件,但它没有启动。请任何人让我知道如何把所有这些一起

我有两个下拉列表,一个单选按钮和一个文本框以及一个按钮。我试图禁用按钮时,下拉列表,单选按钮没有选择一个空的文本框。我能够禁用下拉按钮和单选按钮,并将消息显示为无效选择,为此,我编写了关于所选索引更改事件的代码,甚至在按钮单击事件及其工作正常的代码。但当文本框为空时,我无法禁用该按钮。希望此按钮仅在我在Textbox中键入内容时启用,并且当我尝试在Textbox为空时单击按钮时,需要显示一条消息,说明“请输入注释”。我也尝试过文本框的文本更改事件,但它没有启动。请任何人让我知道如何把所有这些一起在按钮点击事件使用标志

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

到目前为止我已经试过了

按钮点击代码:

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;
        }
    }
Textbox已更改事件代码:

    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">
一,。您需要将TextBox的AutoPostBack属性设置为True

二,。在将输入字符串与EmptyString进行比较时,需要修剪输入,以便删除空白

您可以使用String.IsNullOrWhiteSpace检查null、empty和whitespaces。 试试这个:

设计规范

或者使用String.IsNullOrWhiteSpace函数

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

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*/
    }
  }
一,。您需要将TextBox的AutoPostBack属性设置为True

二,。在将输入字符串与EmptyString进行比较时,需要修剪输入,以便删除空白

您可以使用String.IsNullOrWhiteSpace检查null、empty和whitespaces。 试试这个:

设计规范

或者使用String.IsNullOrWhiteSpace函数

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

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*/
    }
  }
你需要设置

TxtComments.AutoPostBack=true

暗藏

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

像这样

<asp:TextBox ID="TxtComments" runat="server" AutoPostBack="True"></asp:TextBox>   
你需要设置

TxtComments.AutoPostBack=true

暗藏

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

像这样

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

将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">

将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">

如果你能准确地告诉我们问题所在代码的确切部分,这将对人们帮助你很有帮助。你可以粘贴完整的代码并希望人们阅读吗?请发布设计代码ASPX文件?@iJay Thnks以供回复。我只粘贴了那些对其他人有用的代码,以了解我试图获取的内容和方式。事实上,我已经在我的帖子中清楚地提到了这个问题。@Sudhakar Tillapudi yeah肯定会更新我的帖子。这对帮助你的人很有帮助……如果你说得准确,并向我们展示问题代码的确切部分……而不是粘贴你的完整代码,希望人们阅读。你能发布设计代码ASPX文件吗?@iJay谢谢你的回复。我只粘贴了那些对其他人有用的代码,以了解我试图获取的内容和方式。事实上,我已经在我的帖子中清楚地提到了这个问题。@Sudhakar Tillapudi yeah肯定会更新我的帖子谢谢我的朋友,我完全忘记了使用AutoPostBack。我会试试,让你知道。谢谢你的修剪:还需要一点帮助。当文本框为空时,我试图在Buton click上显示错误消息。我需要的消息,请输入一个评论,但现在我作为无效的选择只是因为循环放置在BtnSave与所有一起。那么你能告诉我如何分开展示吗that@Arjun:您需要在BTN中添加空的签入保存单击事件作为第一次检查,请参阅我编辑的答案和解决方案2 Anks a ton。我刚刚颠倒了你的解决方案2,我需要无效的选择作为第一条错误消息。现在一切都很好。谢谢,伙计,我完全忘了使用自动回邮。我会试试,让你知道。谢谢你的修剪:还需要一点帮助。当文本框为空时,我试图在Buton click上显示错误消息。我需要的消息,请输入一个评论,但现在我作为无效的选择只是因为循环放置在BtnSave与所有一起。那么你能告诉我如何分开展示吗that@Arjun:您需要在BTN中添加空的签入保存单击事件作为第一次检查,请参阅我编辑的答案和解决方案2 Anks a ton。我刚刚颠倒了你的解决方案2,我需要无效的选择作为第一条错误消息。现在一切都很好。