简体   繁体   English

文本更改事件未触发

[英]Text Changed Event Not Firing

I got 2 Dropdown list, 1 Radio button and 1 Textbox along with a Button. 我有2个下拉列表,1个单选按钮和1个文本框以及一个按钮。 I am trying to Disable Button when Dropdowns,Radio Buttons are not selected alon with Empty Textbox. 我试图禁用“下拉菜单”,“单选按钮”和“空文本框”时禁用按钮。 I am able to Disable the button for Dropdown and Radio Button and displaying the message as "Invalid Selection" and for this I have written code on Selected Index Change Event and even in Button Click Event and its working fine. 我能够禁用下拉菜单和单选按钮的按钮,并将消息显示为“无效选择”,为此,我已经在“选定索引更改事件”甚至“按钮单击事件”中编写了代码,并且工作正常。 But I am not able to Disable the Button when the Textbox is Empty. 但是当文本框为空时,我无法禁用按钮。 Want this Button to be Enabled only when I type something in Textbox and when i try to Click the Button When Textbox is Empty i need a message to be displayed saying "Please Enter a Comment". 希望仅当我在文本框中键入某些内容并且尝试在文本框为空时单击该按钮时才启用此按钮,我需要显示一条消息,说“请输入评论”。 I have tried Text Changed Event for TextBox too but it is not firing. 我也尝试了TextBox的Text Changed Event,但未触发。 And Please anybody let me know how to put all these together in Button Click event using Flags. 并且请任何人让我知道如何使用Flags将所有这些内容放在一起。

Note: There are 2 Error messages to be displayed upon Button Click. 注意:单击按钮时将显示2条错误消息。 This should come in loop with Flags being assigned. 这应该与分配的标志一起循环。

So far I have tried this, 到目前为止,我已经尝试过了

Button Click Code: 按钮点击代码:

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 Selected Index Codes: 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 Changed Event Code: 文本框已更改事件代码:

    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 code: 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. You need to set AutoPostBack property of the TextBox to True . 1.您需要将TextBox AutoPostBack属性设置为True

2. while comparing the input String with EmptyString , you need to Trim the input so that whitespaces would be removed. 2.在比较输入StringEmptyString ,需要Trim输入,以便删除whitespaces

or 要么

you can use String.IsNullOrWhiteSpace() to check for null , empty and whitespaces . 您可以使用String.IsNullOrWhiteSpace()检查nullemptywhitespaces Try This: 尝试这个:

Design Code 设计规范

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

Code Behind: using Trim() function 背后的代码:使用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;
        }
    }     

or using String.IsNullOrWhiteSpace() function 使用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;
        }
    }     

Solution 2: display TextBox error message as first error 解决方案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*/
    }
  }

You need to set 你需要设置

TxtComments.AutoPostBack= true

in Code behind 在后面的代码中

Or 要么

AutoPostBack="True" in TextBox Design Page TextBox设计页面中的AutoPostBack="True"

Like this 像这样

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

将自动回传设置为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">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM