vb.net 文本框应该只接受日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22153602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Textbox should only accept date
提问by user3373775
I have a web form which several datefields in it, I am using Ajaxcalender for the user to enter the date, I want to restrict the user from typing in extra info other than the date or even not being able to type anything in the textbox apart the value from the calender extender. I am using ASP.NET, VB.NET and SQL Server 2008 database
我有一个 Web 表单,其中包含多个date字段,我正在使用Ajax日历供用户输入日期,我想限制用户输入日期以外的其他信息,甚至无法在文本框中输入任何内容来自压延机扩展器的值。我正在使用 ASP.NET、VB.NET 和 SQL Server 2008 数据库
My date format is dd-MMM-yy
我的日期格式是 dd-MMM-yy
I tried:
我试过:
Enable = false
but it is not saving the date to database
但它没有将日期保存到数据库
Below is a sample of my code:
下面是我的代码示例:
<asp:TextBox ID="TextBox5" runat="server" Width="150px" style="margin-top: 0px"
ReadOnly="False" CausesValidation="True"></asp:TextBox>
<asp:CalendarExtender ID="TextBox5_CalendarExtender" runat="server" Format="dd-MMM-yy" PopupButtonID="ImageButton1" TargetControlID="TextBox5">
</asp:CalendarExtender>
Appreciate your help
感谢你的帮助
Thank you
谢谢
采纳答案by kal kokah
First you'll need to set the textbox as 'read only' in code behind, usually in Page_Load event like this:
首先,您需要在后面的代码中将文本框设置为“只读”,通常是在 Page_Load 事件中,如下所示:
TxtDate.Attributes.Add("readonly","readonly");
Next, modify the design of the textbox to be like this in markup:
接下来,在标记中修改文本框的设计如下:
<asp:TextBox ID="TxtDate" runat="server" Width="150px"></asp:TextBox>
<a href="JavaScript:clearText('<%=TxtDate.ClientID %>')">
<img alt="" src="../clear.png"></img></a>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" Enabled="True"
Format="dd-MMM-yy" PopupPosition="Right" TargetControlID="TxtDate">
</asp:CalendarExtender>
And finally add this small script to clear the date:
最后添加这个小脚本来清除日期:
<script type="text/javascript">
function clearText(ctrl) {
document.getElementById(ctrl).value = "";
};
By doing this, you will have a textbox when clicking on it it will pop up the calendar, it will not allow users to type in, and when the user click on the "clear" image associated with the textbox, it will clear the text.
通过这样做,您将拥有一个文本框,单击它会弹出日历,不允许用户输入,当用户单击与文本框关联的“清除”图像时,它将清除文本.
回答by Yuriy Galanter
Don't set ReadOnly = Trueor Enabled = Falsethat will prevent the value from being posted back. You need to only prevent user interaction while allowing value to be updateable otherwise. One way is to prevent field from obtaining focus, try adding this attribute:
不要设置,ReadOnly = True否则Enabled = False会阻止该值被回传。您只需要阻止用户交互,同时允许值以其他方式更新。一种方法是防止字段获得焦点,尝试添加此属性:
onfocus = "this.blur();"
For more take a look at this post.
更多信息请看这篇文章。
回答by Priyanka Bhootra
Setting Readonly attribute will not cause server side process . we can use contentEditable="false"it will be available on server side
设置只读属性不会导致服务器端进程。我们可以使用contentEditable="false"它将在服务器端可用

