Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

asp.net - i have 3 dropdowns ( Hours // Minutes // Am/Pm ) traying to validate From not Greater than To

I have 3 dropdowns, dd#1 have Hours, dd#2 have minutes and dd#3 have am/pm, im trying to validate to dropdowns FROM aren't greater than TO, i manage to validate the from hours and minutes but with AM and PM i cant its not working for me

Im using Visual Basic as main language i tried with a customvalidator but idk how they work that much BTW Im a rookie and still learning .-.

<div class="row form-group">
                        <div class="col-xs-12">
                            <asp:Localize ID="LblAlmDesdeTextAdmSch" runat="server"
                                meta:resourceKey="LblAlmDesdeTextAdmSchTextResource1" Text="From:"></asp:Localize>
                        </div>
                        <div class="col-xs-12 col-sm-5 col-md-10">
                            <asp:DropDownList ID="cboLunchFromHours" CssClass="dropdown form-control drop-width col-xs-1 " runat="server">
                                <asp:ListItem>1</asp:ListItem>
                                <asp:ListItem>2</asp:ListItem>
                                <asp:ListItem>3</asp:ListItem>
                                <asp:ListItem>4</asp:ListItem>
                                <asp:ListItem>5</asp:ListItem>
                                <asp:ListItem>6</asp:ListItem>
                                <asp:ListItem>7</asp:ListItem>
                                <asp:ListItem>8</asp:ListItem>
                                <asp:ListItem>9</asp:ListItem>
                                <asp:ListItem>10</asp:ListItem>
                                <asp:ListItem>11</asp:ListItem>
                                <asp:ListItem Selected="True">12</asp:ListItem>
                            </asp:DropDownList>

<asp:DropDownList ID="cboLunchFromMinutes" CssClass="dropdown form-control drop-width col-xs-1 " runat="server">
                                <asp:ListItem Selected="True">00</asp:ListItem>
                                <asp:ListItem>15</asp:ListItem>
                                <asp:ListItem>30</asp:ListItem>
                                <asp:ListItem>45</asp:ListItem>
                            </asp:DropDownList>

<asp:DropDownList ID="cboLunchFromAP" CssClass="dropdown form-control drop-width col-xs-1 " runat="server">
                                <asp:ListItem>AM</asp:ListItem>
                                <asp:ListItem Selected="True">PM</asp:ListItem>
                            </asp:DropDownList>
                            <asp:Label ID="lbllunchMsg" runat="server"></asp:Label>
                        </div>
                    </div>

<div class="row form-group">
                        <div class="col-xs-12">
                            <asp:Localize ID="LblToAlmuText" runat="server"
                                meta:resourceKey="LblToAlmuTextResource1" Text="To:"></asp:Localize>
                        </div>
                        <div class="col-xs-12 col-sm-5 col-md-10 ">
                            <asp:DropDownList ID="cboLunchToHours" CssClass="dropdown form-control drop-width col-xs-1 " runat="server">
                                <asp:ListItem Selected="True">1</asp:ListItem>
                                <asp:ListItem>2</asp:ListItem>
                                <asp:ListItem>3</asp:ListItem>
                                <asp:ListItem>4</asp:ListItem>
                                <asp:ListItem>5</asp:ListItem>
                                <asp:ListItem>6</asp:ListItem>
                                <asp:ListItem>7</asp:ListItem>
                                <asp:ListItem>8</asp:ListItem>
                                <asp:ListItem>9</asp:ListItem>
                                <asp:ListItem>10</asp:ListItem>
                                <asp:ListItem>11</asp:ListItem>
                                <asp:ListItem>12</asp:ListItem>
                            </asp:DropDownList>

<asp:DropDownList ID="cboLunchToMinutes" CssClass="dropdown form-control drop-width col-xs-1" runat="server">
                                <asp:ListItem Selected="True">00</asp:ListItem>
                                <asp:ListItem>15</asp:ListItem>
                                <asp:ListItem>30</asp:ListItem>
                                <asp:ListItem>45</asp:ListItem>
                            </asp:DropDownList>

<asp:DropDownList ID="cboLunchToAP" CssClass="dropdown form-control drop-width col-xs-1" runat="server">
                                <asp:ListItem>AM</asp:ListItem>
                                <asp:ListItem Selected="True">PM</asp:ListItem>
                            </asp:DropDownList>
                        </div>
                    </div>
question from:https://stackoverflow.com/questions/65942895/i-have-3-dropdowns-hours-minutes-am-pm-traying-to-validate-from-not-gr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First up:

If you drag + drop a text box onto the web form, then you can in the property sheet set this:

enter image description here

So, display the property sheet for the text box, and select DateTimeLocal. You get a REALLY nice date + time picker - all with nice spinners etc. and you get this:

enter image description here

so the above will save you BOAT-LOADS of time and effort.

I suppose you could combine + cobbile + code your way though your custom dropdowns, but you REALLLY are far better off to use the built in date, or in this case date + time picker. The reason is simple:

If you drop two controls on a form, then you can have a start + end time, and in code because the date is included, then you can span across mid-night (or say take out a book or some equipment for several days). And then in code, you can test/check/see if the start time (and date) is less then the end time with this simple code:

Set the auto-post back of the text box = true, and then in code you can do this:

enter image description here

When you run the above, you get this: enter image description here

So, you can "click" on the calendar. And TRY the mouse wheel to select time.

click on calendar again to confirm the results. You can ALSO type via keyboard, and it will prevent funny values etc.

So, our full markup for the above is this:

<body>
    <form id="form1" runat="server">
        <div style="float:left">
            <asp:Label ID="Label1" runat="server" Text="Enter Start Time" Font-Size="Large"></asp:Label>
            <br />
            <asp:TextBox ID="txtStartTime" runat="server" TextMode="DateTimeLocal" Width="196px" Height="16px" AutoPostBack="True"></asp:TextBox>
        </div>
        <div style="float:left;margin-left:20px">
            <asp:Label ID="Label2" runat="server" Text="Enter End Time" Font-Size="Large"></asp:Label>
            <br />
            <asp:TextBox ID="txtEndTime" runat="server" TextMode="DateTimeLocal" Width="196px" Height="16px" AutoPostBack="True"></asp:TextBox>
        </div>
        <div style="clear:both">
            <h2 id="myMsg" runat="server"></h2>
        </div>
    </form>
</body>

And our full code behind is this:

Protected Sub txtStartTime_TextChanged(sender As Object, e As EventArgs) Handles txtStartTime.TextChanged

    MyTimeCheck

End Sub

Protected Sub txtEndTime_TextChanged(sender As Object, e As EventArgs) Handles txtEndTime.TextChanged

    MyTimeCheck()

End Sub

Sub MyTimeCheck()

    If txtStartTime.Text <> "" And txtEndTime.Text <> "" Then
        If txtStartTime.Text > txtEndTime.Text Then
            Me.myMsg.InnerHtml = "Start time must be less then End Time"
        Else
            myMsg.InnerHtml = ""
        End If
    End If

End Sub

So I used mostly drag + drop. And then I used the property sheet (events), and clicked on the event stub to launch the code behind editor.

enter image description here

So if you follow above: We did mostly drag + drop. We wrote very little code - kind of like desktop developer (Access or VB6).

The MOUSE wheel works for the time picker - try it!!!!

The data is VERIFED to be date + time for you - no need for messy code to test/check the input format if you FORCE the text box to a date (or date + time in this case).

Now, I could perhaps follow up on your existing code, but I don't think it all that of a good idea when a nice date + time picker is built in for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...