Hello,
I have a RadNumericTextBox used to accept a numeric value. Now I got a requirement that when the user input 0, the text in the text box should be changed to a string like "Disabled" instead of the numeric value 0.
Is there any solution? Thanks in advance.
Best regards
Zhang Rui
6 Answers, 1 is accepted
You can handle the valueChanged event and use the set_displayValue to change the display text, the internal value will still remain 0.
Here is sample code:
<
script
type
=
"text/javascript"
>
function valueChanged(textbox, args) {
if (args.get_newValue() == 0) {
textbox.set_displayValue("Disabled");
}
}
</
script
>
<
telerik:RadNumericTextBox
runat
=
"server"
ClientEvents-OnValueChanged
=
"valueChanged"
></
telerik:RadNumericTextBox
>
This will also handle the case if the input is empty. You can modify it further like: args.get_newValue() === "0" if needed.
Regards,
Vasil
Telerik
Hello Vasil,
Thanks for your help. It works, when the value changed to 0, "Disabled" displayed.
But if I input '0' in the text box then press Enter key, or click the "Down" spin button at this time, the display text changed back to '0' from "Disabled". It seems the reason is that there is no value-change event to change the display text. How can I solve it?
Best regards
Zhang Rui
If the enter causes PostBack either because of Default Button in the page or because of AutoPostBack=true for the NumericTextBoxControl. The display value will be formatted server side.
In this case you can handle the PreRender event of the control to set the desired text.
<
script
type
=
"text/javascript"
>
function valueChanged(textbox, args) {
if (args.get_newValue() == 0) {
textbox.set_displayValue("Disabled");
}
}
</
script
>
<
telerik:RadNumericTextBox
runat
=
"server"
OnPreRender
=
"Unnamed_PreRender"
ClientEvents-OnValueChanged
=
"valueChanged"
>
</
telerik:RadNumericTextBox
>
C#
protected
void
Unnamed_PreRender(
object
sender, EventArgs e)
{
RadNumericTextBox textbox = sender
as
RadNumericTextBox;
if
(textbox.Value == 0)
{
textbox.DisplayText =
"disabled"
;
}
}
Regards,
Vasil
Telerik
Hi Vasil,
Thanks for your fast response. I've tried your solution, it did not work.
I do not think it is a PostBack issue. Actually I've disabled auto post back. I think the point is that the internal value still remains 0 when "Disabled" displayed. When you input 0 or click the "Down" spin button, the display text changed to 0, but the internal value did not change. Hence you can not get the value changed event on client side, consquently, the script was not invoked to change the display text.
Is there any other solution? Thanks.
Best regards
Zhang Rui
Hi Vasil,
Now my problem is that when "Disabled" displayed, the value will be changed to 0 again when I click spin down. Is there any solution?
Thanks.
Best regards
Zhang Rui
This does not happen with my test page after applying both server side and client side logic.
Could you specify the version of RadControls that you are using and your browser?
Regards,
Vasil
Telerik