本文介绍了GridView中的Javascript日期验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友,
由于我是ASP .net的新手,因此在验证GridView中文本框上的日期时遇到问题.我想在数据库中更新之前验证日期.请提出任何建议.
Hi Friends,
As I am New to ASP .net , I am facing problem while validating a date on a textbox in GridView. I want to validate the date before updating in database. Please give any suggestions.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
/**--------------------------
//* Validate Date Field script- By JavaScriptKit.com
//* For this script and 100s more, visit http://www.javascriptkit.com
//* This notice must stay intact for usage
---------------------------**/
function checkdate(input){
var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
var returnval=false
if (!validformat.test(input.value))
alert("Invalid Date Format. Please correct and submit again.")
else{ //Detailed check for valid date ranges
var dayfield=input.value.split("/")[0]
var monthfield=input.value.split("/")[]
var yearfield=input.value.split("/")[2]
var dayobj = new Date(yearfield, monthfield-1, dayfield)
if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
else
returnval=true
}
if (returnval==false) input.select()
return returnval
}
</script>
</head>
<body>
<form onsubmit ="return checkdate(this.mydate) " runat="server">
<asp:GridView runat="server" ID="gd" AutoGenerateColumns="false" onrowupdating="gd_RowUpdating" onrowcommand="gd_RowCommand" onrowediting="gd_RowEditing">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ID="txtid" Text='<%#Bind("Eid")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ID="txt123" Text='<%#Bind("Date")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="btn123" OnClientClick="return checkdate(this.mydate);" Text="Click Me" CommandName="Update" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label runat="server" ID="lblMessage"></asp:Label>
</form>
</body>
</html>
代码背后的代码:
Code Behind Code:
protected void gd_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (cnn.State == ConnectionState.Closed)
cnn.Open();
GridViewRow grow = gd.Rows[e.RowIndex];
TextBox txt123 = (TextBox)grow.FindControl("txt123");
TextBox txtid = (TextBox)grow.FindControl("txtid");
SqlCommand cmd = new SqlCommand("update emp_details set date='" + txt123.Text + "' where Eid="+Convert.ToInt32(txtid.Text )+ "", cnn);
cmd.ExecuteNonQuery();
GridBind();
lblMessage.Text = "Updated";
cnn.Close();
}
推荐答案
代码背后的代码:
Code Behind Code:
protected void gd_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (cnn.State == ConnectionState.Closed)
cnn.Open();
GridViewRow grow = gd.Rows[e.RowIndex];
TextBox txt123 = (TextBox)grow.FindControl("txt123");
TextBox txtid = (TextBox)grow.FindControl("txtid");
SqlCommand cmd = new SqlCommand("update emp_details set date='" + txt123.Text + "' where Eid="+Convert.ToInt32(txtid.Text )+ "", cnn);
cmd.ExecuteNonQuery();
GridBind();
lblMessage.Text = "Updated";
cnn.Close();
}
OnClientClick="return checkdate(''<%#DataBinder.Eval(Container.DataItem,"Date")%>'');"
这篇关于GridView中的Javascript日期验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!