开发者

how to change the label text if date displayed in textbox2 is greater than textbox1?

I have textbox in my asp.net 3.5 VB.net webform

in my textbox1 the text is : 30-Dec-2010, 06:00:00 PM

i want when the date in textbox is greater tha开发者_Python百科n textbox1 then the Label text would be "No REfund ! Sorry"

How to do this


You should use ASP.Net CompareValidator for this purpose. You can check both on client- and serverside. Besides i would recommend not to have Date AND Time in one Textbox. That makes it more difficult to validate and it's not standard, so it might be confusing and error-phrone for users.

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBox1" ControlToValidate="TextBox2" Type="Date" Operator="GreaterThan" runat="server" ErrorMessage="No REfund ! Sorry" EnableClientScript="true" ></asp:CompareValidator>
<asp:Button ID="BtnPostback" runat="server" Text="postback" />

On the serverside you should also trigger the validation(f.e. if javascript is disabled):

Private Sub BtnPostback_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnPostback.Click
     Page.Validate()
     If Me.IsValid Then
         'Do something f.e. save'
     End If
End Sub


CompareValidator Class


It would be better if you do it using javascript because this functionality does not require postback. A similar question has been posted on stackoverflow which compares two dates using javascript. Check it out here. You just need to extend it to incorporate assigning text to the label.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜