如何在文本更改时比较和检查带有标签的文本框? [英] How to compare and check text box with label in event of text changed?

查看:67
本文介绍了如何在文本更改时比较和检查带有标签的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框和标签,彼此相邻。 Label的文本值来自下拉列表。如果标签的值小于文本框值,我必须将文本框的焦点转到自身,然后继续,直到选择正确的值。

你能帮我吗?



我的尝试:



这是我的代码: -

.aspx文件: -

I have a text box and label sitting next to each others. Label's text value comes from a drop down list. I have to turn the focus of text box to itself if, label's value is smaller than text box value and it goes on until, right value is selected.
Can You help me?

What I have tried:

This is my code:-
.aspx file:-

<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文件代码: -



Here's .cs file code:-

protected void txt_Adults_TextChanged(object sender, EventArgs e)
         {
  
                 if (txt_Adults==null)
                 {
                     txt_Adults.Text = "";
                     txt_Adults.Focus();
                 }
                 else if (Convert.ToInt32(txt_Adults.Text) > Max_NoAdults)
                 {
                     txt_Adults.Text = "";
                     txt_Adults.Focus();
                 }
                 else if(Convert.ToInt32(txt_Adults.Text)<=Max_NoAdults)
                 {
                     NoAdults = Convert.ToInt32(txt_Adults.Text);
                 }
             
         }

推荐答案

如果使用text_changed事件,文本框将不为null。 />
The textbox will not null if using text_changed event.
int32? tempValue; //nullable int variable


这篇关于如何在文本更改时比较和检查带有标签的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆