我有一个文本框和标签,彼此相邻。 Label的文本值来自下拉列表。如果标签的值小于文本框值,我必须将文本框的焦点转到自身,然后它继续,直到在文本框中输入正确的值。 你能帮帮我吗?
这是我的代码: - .aspx文件: -
<div class="row mb10">
<asp:Label runat="server" ID="lbl_Adults" CssClass="col-lg-2 control-label" Text="Adults : " AssociatedControlID="txt_Adults"></asp:Label>
<div class="col-lg-6">
<div class="col-lg-4" style="margin-left:-15px;">
<asp:TextBox runat="server" ID="txt_Adults" CssClass="form-control" OnTextChanged="txt_Adults_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:RequiredFieldValidator Font-Bold="true" ForeColor="Red" runat="server" ID="req_Adults" ControlToValidate="txt_Adults" ErrorMessage="Enter No. of Adults"></asp:RequiredFieldValidator>
</div>
<div class="col-lg-8" style="font-family:'Copperplate Gothic'">
Only <asp:Label runat="server" ID="lbl_No_Of_Adults"></asp:Label> Adult(s) allowed.
</div>
</div>
</div>
这是.cs文件代码: -
protected void txt_Adults_TextChanged(object sender, EventArgs e)
{
while (true)
{
if (txt_Adults==null)
{
txt_Adults.Text = Convert.ToString("0");
txt_Adults.Focus();
//txt_Adults_TextChanged(sender, e);
}
else if (Convert.ToInt32(txt_Adults.Text) > Max_NoAdults)
{
txt_Adults.Text = Convert.ToString("0");
txt_Adults.Focus();
//txt_Adults_TextChanged(sender, e);
}
else
{
NoAdults = Convert.ToInt32(txt_Adults.Text);
//NoAdults is a static variable that collects final(right) value of txt_Adults.text;
break;
}
}
}
答案 0 :(得分:0)
只需这样做
protected void txt_Adults_TextChanged(object sender, EventArgs e)
{
//this condition will always be true unless and until both the `Text`
//string are same
if (lbl_No_Of_Adults.Text.Trim() != txt_Adults.Text.Trim())
{
txt_Adults.Focus();
}
}
否则,如果您希望仅在Label
值小于TextBox
的情况下匹配条件,请执行此操作。
protected void txt_Adults_TextChanged(object sender, EventArgs e)
{
int a = 0 , b =0;
int.TryParse(lbl_No_Of_Adults.Text.Trim(), out a);
int.TryParse(txt_Adults.Text.Trim(), out b);
if (a < b)
{
txt_Adults.Focus();
}
}