[英]Text Changed Event Not Firing
我有2个下拉列表,1个单选按钮和1个文本框以及一个按钮。 我试图禁用“下拉菜单”,“单选按钮”和“空文本框”时禁用按钮。 我能够禁用下拉菜单和单选按钮的按钮,并将消息显示为“无效选择”,为此,我已经在“选定索引更改事件”甚至“按钮单击事件”中编写了代码,并且工作正常。 但是当文本框为空时,我无法禁用按钮。 希望仅当我在文本框中键入某些内容并且尝试在文本框为空时单击该按钮时才启用此按钮,我需要显示一条消息,说“请输入评论”。 我也尝试了TextBox的Text Changed Event,但未触发。 并且请任何人让我知道如何使用Flags将所有这些内容放在一起。
注意:单击按钮时将显示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.您需要将TextBox
AutoPostBack
属性设置为True
。
2.在比较输入String
和EmptyString
,需要Trim
输入,以便删除whitespaces
。
要么
您可以使用String.IsNullOrWhiteSpace()
检查null
, empty
和whitespaces
。 尝试这个:
设计规范
<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*/
}
}
你需要设置
TxtComments.AutoPostBack= true
在后面的代码中
要么
TextBox设计页面中的AutoPostBack="True"
像这样
<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.