5

I am sorry for posting this question as it may be find silly to all, but I am not getting the exact solution.

The question is: I have a Date time picker in my project, it comes after the 3 textboxes in the form, if there is no text is entered in the textbox and enter on submit, it gives a message(validation) that data to be entered. In the same way, if the date is not selected, it should proceed further.

What is the code to do that, the code which worked for other textboxes and not working for datetimepicker control is:

       if (dateInsert.Value.ToString() = string.Empty)
        {
            MessageBox.Show("Please select date!");
            dateInsert.Focus();
            return;
        }
4

5 回答 5

5

请更正代码,看看它是否有效

               if (dateInsert.Value.ToString() == "")
              {
                MessageBox.Show("Please select date!");
                dateInsert.Focus();
                return;
               }
于 2013-09-11T11:08:46.260 回答
2

empty 没有直接的解决方案DateTimePicker。清空 DateTimePicker 的唯一方法是设置CustomFormat然后设置empty space为值。

dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = " ";

即使您这样做,控件中的值也会被清除,但如果您在代码中访问控件的 value 属性,它将返回当前日期时间。所以你的条件永远是假的。

//This is always false
dateInsert.Value.ToString() = string.Empty

解决方案

而不是在条件中使用Value使用Text

if(dateInsert.Text = " ")
于 2013-09-11T10:41:58.123 回答
0

如果您使用的是 Visual Studio .....使用此代码验证空文本框

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="None"
        ErrorMessage="Select Date" ControlToValidate="dateInsert" ValidationGroup="validation"> </asp:RequiredFieldValidator>
于 2013-09-11T10:33:45.573 回答
0
if (string.IsNullOrEmpty(dateInsert.Text)
          {
            MessageBox.Show("Please select date!");
            dateInsert.Focus();
            return;
           }

希望这可以帮助某人

于 2016-06-15T01:40:16.160 回答
-1
if(datepicker.Text == " ")
messagebox.show("Please Select Date");

// this works 100 %
于 2017-11-03T16:05:10.203 回答