日期值的空文本框 [英] empty textbox for date value

查看:84
本文介绍了日期值的空文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框,我插入日期值和一个文本框的名称。当我点击搜索按钮,记录这两个日期显示之间。它正在工作。但是当我没有在日期文本框中插入值,只插入命名文本框并单击搜索按钮,然后它给我error.Error是:字符串未被识别为有效的DateTime。

我必须做什么才能将日期的文本框清空。

这里是我的代码...

i have two textbox in wich i insert date value and one textbox for name.when i click on search button, record bettween this two date display.it is working.But when i not insert value in date textbox and insert only in name textbox and click on search button,then it gives me error.Error is:String was not recognized as a valid DateTime.
what i have to do to take textbox of date empty.
here is my code...

protected void btnSearch_Click(object sender, EventArgs e)
{
    objdal2.Name = txt_Searchname.Text;
    objdal2.DateFrom =Convert.ToDateTime(txt_SearchDateFrom.Text);
    objdal2.DateTo =Convert.ToDateTime(txt_SearchDateTo.Text);
    objdal2.LeaveType = ddlLeaveType.SelectedItem.Text;
    objdal2.Approve =Convert.ToBoolean(ddlApprove.SelectedItem.Text);

   dv= objdal2.Search_Data();
   GridView1.DataSource = dv;
   GridView1.DataBind();
   lblmsg2.Text = "Total Records: " + count;
}

推荐答案

简单的方法是检查方法的顶部:

The simple way is to check at the top of your method:
if (string.IsNullOrWhitespace(txt_SearchDateFrom.Text) || string.IsNullOrWhitespace(txt_SearchDateTo.Text))
   {
   MessageBox.Show("Please enter a form and a to date");
   return;
   }

但这并不能阻止他们进入垃圾场。我会使用它,加上我会使用TryParse:

But that doesn''t stop them entering rubbish. I would use that, plus I would use TryParse:

if (DateTime.TryParse(txt_SearchDateFrom.Text, out objdal2.DateFrom) &&
    DateTime.TryParse(txt_SearchDateTo.Text, out objdal2.DateTo))
   {
   ...
   }

但我的首选是完全不使用TextBoxes来防止这两个问题!您是否考虑过DateTimePicker?这样,用户就无法输入无效日期,您也无需进行任何转换。

But my preference would be to prevent both problems altogether by not using TextBoxes at all! Have you considered a DateTimePicker instead? That way, the user can''t enter an invalid date, and you don''t have to do any conversion.


您好,

请验证日期文本框不应为空(javscript)。

或如果您传递空文本框值,则只需将当前日期作为该文本框的默认日期进行编码,只需单击搜索按钮

Hi,
Please Validate the Date Textbox should not be empty in (javscript).
or If your passing Empty text box value then just pass the Current date as default date for that textbox in coding wise just check on clicking on search button
if(textbox.text=="") 
{

textbox.text=Todaysdate;
}
else
{
 textbox.text="what ever your passing through text box value only"
}



它只会工作试着让我知道

....


It will work just try and let me know
....


如果你正在使用存储过程然后将日期值传递为null .....

否则使用c#...................................
从前端管理它


更多clarificatins发送您的搜索数据代码
if you are using stored procedure then pass date value as null.....
else manage it from front end using c#...................................

for more clarificatins send your searchdata code


这篇关于日期值的空文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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