【发布时间】:2013-12-26 05:48:24
【问题描述】:
我有 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">
【问题讨论】:
-
这对人们帮助你很有帮助......如果你是准确的并向我们展示问题的代码的确切部分......而不是粘贴你的完整代码并期望人们阅读
-
能否请您发布设计代码(aspx 文件)?
-
@iJay 感谢您的回复。我只粘贴了那些对其他人有用的代码,以了解我想要获得什么以及如何获得。事实上,我在帖子中已经清楚地提到了这个问题。
-
@Sudhakar Tillapudi 是的,肯定会更新我的帖子