Telerik blogs

As you already know we have introduced new rendering model for RadInput in Q3 2011. It was later set as default mode in Q1 2012.

The initial idea behind this change was to remove 2 of the input elements from the output HTML of the control and use a span that to overlay the visible one in order to show the formatted value. With this approach we really have fewer elements on the page and in the same time we could show, validate and submit different values. We also removed the table in order to provide better styling potential and boost the performance.

In the last Q we had to support both renderings to be backwards compatible with the older versions. People are still able to set EnableSingleInputRendering=”false” and to use the old rendering in their projects, to avoid rewriting the styles. However supporting both modes is causing performance penalty since they work differently. Not only that the JavaScript code is bigger, but has lots of checks in each moment what is the mode and what should happen.

I know what you will say in this moment: Why not remove the old rendering, leave only the new one and it would work even faster? That’s good idea. However the old rendering has one advantage that we don’t want to lose: it uses a table. As it could be bad in most of the cases, the table could be used when the control has label. In case when we want the label to have minimal length, and the input to get the rest of the length automatically without any calculation, we need a table.

We come up with this new approach that is combination between both modes:

  • Render one input element that to be visible all the time like in Q2 2012.
  • Do not use SPAN element like in Q3 2011.
  • Render only one input like in Q2 2012.
  • Set validators to access different value from the visible one to work with cultures properly.
  • Provide a way for showing different text in the input, without changing the validation value, in order to show empty/error messages dynamically on all Input controls.

New Validation Model

The idea is to render only the input element all the time, and remove the span element and the excessive inputs.  To achieve this we store the validation value in “expando” property and we submit real value through the client state.  Also we needed to override the ValidatorGetValue function of asp:Validators to look our expando property called “RadInputValidationValue”. 

The Role of the EnableSingleInputRendering Property since Q2 2012

It makes difference in the rendering.  By default EnableSingleInputRendering is still “true”, and when you set it to “false”, the control renders table around the label, input and button elements.

Here is an example ASP markup:

<telerik:RadDateInput ID="RadDateInput1" runat="server" Label="Date1" Width="200px" />
<br />
<telerik:RadDateInput ID="RadDateInput2" runat="server" Label="Date2" Width="200px"
EnableSingleInputRendering="false" />

The rendered HTML will be like(I’ve  stripped some styles to be more clear):
<span class="riSingle RadInput RadInput_Default" style="width: 200px;">
  <label style="width: 80px;">
    Date1</label>
  <span class="riContentWrapper" style="width: 120px;">
    <input id="RadDateInput1" name="RadDateInput1" class="riTextBox riEnabled" />
  </span></span>
<br />
<div class="RadInput RadInput_Default" style="width: 200px;">
  <table class="riTable" style="width: 200px;">
    <tr>
      <td>
        <label class="riLabel" for="RadDateInput2" id="RadDateInput2_Label">
          Date2</label>
      </td>
      <td class="riCell" style="width: 100%;">
        <input id="RadDateInput2" name="RadDateInput2" class="riTextBox riEnabled" style="width: 100%;" />
      </td>
    </tr>
  </table>
</div>

By default in non-single input rendering mode the width of the label is not set, and the length of the input is 100%, so it will be auto sized to get all possible free space in the table. When in single input rendering mode, the default length of the label is 40% and 60% for the input. This width can be changed with the LabelWidth property. And in your browsers the inputs will look like this:

 

You probably spotted that the riContentWrapper is also needless in this case, but it will stay there until Q2 2012 SP1 for backwards compatibility.

Another reason you would might want to set EnableSingleInputRendering="false" for your Telerik’s ASP.NET AJAX Input controls is to avoid re-computing of the styles dynamically on the client.

Tips and Tricks for Client and Server-Side Programming

Accessing the Client object and input element:

<telerik:RadDateInput ID="RadDateInput1" runat="server" ClientEvents-OnLoad="loadHandler" />
<script type="text/javascript">
function loadHandler(sender, args)
{
//In this event handler "sender" is actually the client side object.
 
//Accessing objects and elements:
var clientSideObject = $find("<%=RadDateInput1.ClientID %>");
var visibleInputElement = $get("<%=RadDateInput1.ClientID %>");
 
//Accessing the visible value:
var formattedDisplayValue1 = clientSideObject.get_displayValue();
var formattedDisplayValue2 = visibleInputElement.value; //if the input is blurred
 
//Accessing the validation value:
var validationValue1 = clientSideObject.get_validationText();
var validationValue2 = visibleInputElement.RadInputValidationValue;
 
//Accessing the edit value:
var editValue1 = clientSideObject.get_editValue();
var editValue2 = visibleInputElement.value; //if the input is focused
 
//Get the control's value:
var value1 = clientSideObject.get_value();
 
//Find the client object if you have its textbox element:
var object = $find(visibleInputElement.id);
 
//Get the HTML element if you have reference to the client object:
var element1 = $get(clientSideObject.get_id()); //suggested approach
var element2 = clientSideObject._textBoxElement; //hacky but will work with the old rendering as well
}
</script>

New DisplayText server-side property

The new DisplayText property allows you to set the display value from the Server to a different value the actual value.  Similar to the empty message, but shown even if the input is not empty (cool, ain’t it). This text will be cleared once the user changes the input value.

Aspx:

<telerik:RadDateInput runat="server" ID="dateInput"></telerik:RadDateInput>

C#: 

protected void Page_Load(object sender, EventArgs e)
{
    dateInput.SelectedDate = DateTime.Now;
    dateInput.EmptyMessage = "Please enter date";
    dateInput.DisplayText = "Click here to change the date";           
}

In this example, initially the users will see “Click here to change the date”. When they click in the input they will see the current date. When they change the date and get out of focus, they will see the newly entered date. But if all the text is deleted, they will see the empty message. The DisplayText property can be used if you do server-side validation and you want to tell the client that his input is not valid after the PostBack.

Showing informative messages inside the textbox

Similar to the server-side DisplayText property, there is a displayValue client-side property. You can use it when you want to show some message inside the textbox with JavaScript code. And this message will not affect the control actual value:
<script type="text/javascript">
function changedHandler(textbox, eventArgs)
{
    if (textbox.get_value() != "John")
    {
        textbox.set_displayValue("Please enter 'John' in this textbox");
    }
    //currently the value of the InputControl is what the user entered, however he/she sees message that we set.
}
</script>
<telerik:RadTextBox Width="500px" Text="John" runat="server"
    ClientEvents-OnValueChanged="changedHandler" />

Known Limitations (For Now)

In password mode the empty message is shown as bullets like in Q3 2011

Changes You Should Do After Upgrade

If you upgrade from Q1 2012:

Public client side API was no changes. So if you strictly used only public methods you don’t need to change anything, and the control will work the same way.

If you upgrade from Q3 2011 or older:

  • If you have accessed the visible input element using: 
  • $get("<%=input1.ClientID %>_text")
    Now you should use:
  • $get("<%=input1.ClientID %>")
  • If you have accessed the value of NumericTextBox using:
  • $get("<%=NumericTextBox1.ClientID %>_value").value;
    You should change your code to:
  • $find("<%=NumericTextBox1.ClientID %>").get_value();
  • If you have accessed the validation value using:
  • $get("<%=input1.ClientID %>").value;
    Now you have to use:
  • $get("<%=input1.ClientID %>").RadInputValidationValue;

Hope you enjoy the improvements we made!

 


About the Author

Iana Tsolova

is Product Manager at Telerik’s DevTools division. She joined the company back in the beginning of 2008 as a Support Officer and has since occupied various positions at Telerik, including Senior Support Officer, Team Lead at one of the ASP.NET AJAX teams and Technical Support Director. Iana’s main interests are web development, reading articles related to geography, wild nature and latest renewable energy technologies.

Related Posts

Comments

Comments are disabled in preview mode.