开发者

Prevent users from modifying date in textbox

I have a textbox with a calendar icon next to it. When the icon is clicked a popup form displays the calendar control. I have it set up where they can only select the week ending date(saturday) and that date is displayed in the textbox.

I want to prevent users from editing the textbox. I've tried using the readonly and enabled properties but that doesn't work.

How can I keep users from modifying the date in the textbox?

(I'm using the .Net time tracker starter kit template for the 开发者_运维问答site)


I do this by setting the textbox as read-only property from server side.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        txtStartDate.Attributes.Add("readonly", "readonly");
        txtEndDate.Attributes.Add("readonly", "readonly");
    }
}

It works, and it's non-JavaScript dependant.


Try using a Label instead, styled to look like a TextBox. You can also set the TextBox's Enabled property to false, but that might not work for you depending on what you're doing. The alternative is probably using some JS to prevent changes to the value, but that could get pretty complicated.


You can call javascript blur() in the onfocus event:

<asp:TextBox onfocus="blur();" ... />


I know it is a hack but you could always save the selected date as a variable. Then use an actionlistener that is called when the value is changed. If the date is invalid (ie. Not a week ending date) then replace the textbox with the saved variable. Otherwise if the date is value the replace the variable for if the user changes it.


If the user isn't supposed to edit the text in the textbox you shouldn't be using a textbox. Use a label.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜