views:

58

answers:

5

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)

A: 

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.

Kyra
+1  A: 

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.

Justin Morgan
A: 

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

<asp:TextBox onfocus="blur();" ... />
dcp
But what about copy and paste?
w69rdy
@w69rdy -See my latest solution.
dcp
+1  A: 

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.

Rafael Belliard
A: 

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

Jeroen
I disagree. My regular users would get lost with labels, whereas with textboxes, at least they know that if by hovering them the cursor changes, it's a safe bet to click it (which would trigger the calendar popup).
Rafael Belliard
I use a calendar icon for that and leave the textbox editable.
Jeroen