我有2个下拉列表,1个单选按钮和1个文本框以及一个按钮。我试图在下拉列表,单选按钮未选中空文本框时禁用按钮。我可以禁用Dropdown和单选按钮的按钮,并将消息显示为“无效选择”,为此我已经编写了有关选定索引更改事件的代码,甚至是按钮单击事件及其工作正常。但是当文本框为空时,我无法禁用按钮。只有当我在文本框中键入内容时以及当我尝试单击按钮时文本框为空时才要启用此按钮我需要显示一条消息,说“请输入注释”。我也尝试过TextBox的Text Changed Event,但它没有触发。并且请任何人让我知道如何使用Flags将所有这些放在Button Click事件中。
注意:按钮单击时会显示2条错误消息。这应该在循环中分配Flags。
到目前为止,我已尝试过这个,
按钮单击代码:
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">
答案 0 :(得分:4)
1。您需要将AutoPostBack
的{{1}}属性设置为TextBox
。
2。在将输入True
与String
进行比较时,您需要EmptyString
输入,以便删除Trim
。< / p>
或强>
您可以使用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()
解决方案2:将TextBox错误消息显示为第一个错误
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;
}
}
答案 1 :(得分:2)
您需要设置
TxtComments.AutoPostBack= true
或
TextBox设计页面中的 AutoPostBack="True"
喜欢这个
<asp:TextBox ID="TxtComments" runat="server" AutoPostBack="True"></asp:TextBox>
答案 2 :(得分:0)
将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">