This is a migrated thread and some comments may be shown as answers.

How to make DateTimePickerFor input field readonly without javascript?

7 Answers 202 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Oleg
Top achievements
Rank 1
Oleg asked on 06 Oct 2011, 07:34 PM
Hi,

How to make the input field of the DateTimePicker readonly?

Adding readonly to html attributes throws an exception

<%= Html.Telerik().DateTimePickerFor(x => x.StartDate)
        .InputHtmlAttributes(new { tabindex = 4, style = "width:100%", readonly="readonly"})%>

The exception is:
error CS1525: Invalid expression term 'readonly'

7 Answers, 1 is accepted

Sort by
0
madmike210
Top achievements
Rank 1
answered on 07 Oct 2011, 03:13 PM
If it is a readonly field, why not just use a textbox with readonly set?

You can also Disable the datetimepicker which has the same effect as a readonly:

<%= Html.Telerik().DateTimePickerFor(x => x.StartDate).Enable(false) %>
0
Oleg
Top achievements
Rank 1
answered on 07 Oct 2011, 04:07 PM
What I am trying to reach is: disable user input into the textbox, but allow clicking calendar button to select a date.

Setting .Enable(false) disables input box and calendar functionality. That's not what I am trying to reach.

Any other suggestions?
0
madmike210
Top achievements
Rank 1
answered on 07 Oct 2011, 04:32 PM

Oh, that makes sense, and it is a clever idea.  The only way I see this happening is by using javascript:

<%= Html.Telerik().DateTimePickerFor(x => x.StartDate)
                  .ClientEvents(events=>events.OnLoad("OnStartDateLoad"))%>
 

<script type="text/javascript">
function OnStartDateLoad()
{
     $("#StartDate").attr("readonly", "readonly");
}
</script>

0
Oleg
Top achievements
Rank 1
answered on 07 Oct 2011, 04:46 PM
Thanks, madmike210.

Yeah, the javascript approach works, but I wanted to avoid it, if possible.
0
Accepted
madmike210
Top achievements
Rank 1
answered on 07 Oct 2011, 05:08 PM
The problem with your intial approach is that 'readonly' is a reserved word in C#.  That is why you are getting the error.  I suppose you can capitalize 'readonly' so that C# doesn't think its a reserved word, but then the attribute 'Readonly' isn't xhtml compliant, and some browsers might not handle it properly.  But I seriously doubt that it will cause a problem in most borwsers.  Try this:

<%= Html.Telerik().DateTimePickerFor(x => x.StartDate)
 
        .InputHtmlAttributes(new { tabindex = 4, style = "width:100%", Readonly="readonly"})%>
0
Oleg
Top achievements
Rank 1
answered on 07 Oct 2011, 05:14 PM
awesome!
capitalizing did the trick.
Thanks!
0
Richard Wilde
Top achievements
Rank 1
answered on 18 Sep 2012, 08:14 AM
or use @ to escape the attribute readonly

e.g.

.InputHtmlAttributes(new { @readonly="readonly"})
Tags
Date/Time Pickers
Asked by
Oleg
Top achievements
Rank 1
Answers by
madmike210
Top achievements
Rank 1
Oleg
Top achievements
Rank 1
Richard Wilde
Top achievements
Rank 1
Share this question
or