问题描述
我有两个文本框,我插入日期值和一个文本框的名称。当我点击搜索按钮,记录这两个日期显示之间。它正在工作。但是当我没有在日期文本框中插入值,只插入命名文本框并单击搜索按钮,然后它给我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;
}
推荐答案
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.
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
....
这篇关于日期值的空文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!