Each control returns a pair of values, the singular selected value and a second long dictionary type string with more information on the control and its selected value
.
For example a Rad Input Text box will return two fields:
key = "ctl00$MainContentMembers$tsupcontrol_0"
value = "Mick"
and
value = "{\"enabled\":true,\"emptyMessage\":\"Display Name\",\"validationText\":\"Mick\",\"valueAsString\":\"Mick\",\"lastSetTextBoxValue\":\"Mick\"}"
Isn't it nice how the number zero can fall be take off the end of $tsupcontrol_ to get the control number.
A DropDownList only returns the second dictionary item and no single value with the selection.
key = "ctl00_MainContentMembers_tsupcontrol_3_ClientState"
value = "{\"enabled\":true,\"logEntries\":[],\"selectedIndex\":3,\"selectedText\":\"Four%20is%20square\",\"selectedValue\":\"Four\"}"My post parsing looks for $tsupcontrol_ to find the values and gets to ignore the more complicated compound values. Now I must complicate my code having to look for non $tsupcontrol_ values because DropDownList does not output a single return value which can be easily seen as "selectedValue" in the complicated string. I have a Asp radio button list that gives the selected value just fine.
Is this a bug I must work around or is there a way to make the control return its selected value properly like the other well behaved controls? Since the control is constructed in server code I cannot use Javascript to reference it or reference it as a runat control on the server.
George
16 Answers, 1 is accepted
The work around this bug to get the actual value from the returned JSON string is...
if (fieldType == FieldTypes.Dropdown) {
if (Helper.IsEmpty(value)) {
return;
}
JToken token = JObject.Parse(value);
value = (string)token.SelectToken("selectedValue");
}
The empty value check is needed because of a second bug. If the form has a validation error on any control then when the validation is fixed the dropdown returns an empty string for its JSON value. It does return the ClientState but empty. I cannot think of a work around for this one, it is just a straight up bug my users will complain about until Telerik does something about it. Nice getting to play the blame game at least.
It feels like the posting aspect of the drop down was simply not tested.
I would suggest using the RadDropDownList client-side or server-side API In order to retrieve the RadDropDownList selected value instead of parsing the client state.
For example on the server you can access it as shown:
//code behind
RadDropDownList1.SelectedValue
On the client-side:
//JavaScript
$find(
"ctl00_ContentPlaceholder1_RadDropDownProducts"
).get_selectedItem().get_value()
Regards,
Boyan Dimitrov
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
Your work around will not work for me. I am using an arbitrarily defined set of definitions to create a form in code, the dropdown is created in code resulting in no server-side ASP API being available (i.e. there is no runat the dropdown which is not registered with the form). Using a client side api would be more complicated than it is worth since the java script could not know the control name in advance or how many dropdowns there might be.
Using posted data is used many applications and is required for my application, the Rad dropdown does not behave properly in posted data. Pease admit is a bug and say it will be fixed in a future drop. If you cannot do this, then the solution would be to use a different control than Telerik's Rad dropdown.
George
I am afraid that there is no generic way of generating the control's client state in order to be same for all controls. In general the client-state is intended to be used internally in order to recreate the control, but not for retrieving the selected value or etc. For such purposes we suggest using the control's API (server-side or client-side).
As a possible workaround you can create a JavaScript serializer object and deserialize the RadDopDownList client-state.
//code
RadDropDownListClientState clientState =
null
;
var serializer =
new
JavaScriptSerializer();
try
{
clientState = serializer.Deserialize<RadDropDownListClientState>(clientStateValue);
clientState.SelectedText = DecodeText(clientState.SelectedText);
clientState.SelectedValue = DecodeText(clientState.SelectedValue);
}
In this case you can get the selected way much easier.
Regards,
Boyan Dimitrov
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
If the RadDropDownList cannot reliably return a posted value I guess it is not a control I should be using. I had thought reliable post data would be a priority for html controls.
In order to avoid any further confusions please note that the provided code in my last response should be executed on the server.
//code
protected
void
Page_Load(
object
sender, EventArgs e)
{
string
cs = Request.Form[
"RadDropDownList1_ClientState"
];
var serializer =
new
JavaScriptSerializer();
RadDropDownListClientState clientState;
try
{
clientState = serializer.Deserialize<RadDropDownListClientState>(cs);
string
SelectedText = clientState.SelectedText;
string
SelectedValue = clientState.SelectedValue;
}
catch
(Exception)
{
throw
;
}
}
Regards,
Boyan Dimitrov
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
The solution to my problem is to instead use the asp dropdown which simply posts its selected value on every post. And then worry about the style implications of Telerik styles verses Bootstrap.
George
I just came across this same issue while integrating RadDropDownList and RadComboBox from the latest ASP.NET release.
Boyan - what you say is correct IF and ONLY IF the postback is caused by the control, i.e. the RadDropDownList or RadComboBox.
However, George is correct in saying that WHEN a postback is caused by a different control, such as a button, then the values for the RadDropDownList or RadComboBox cannot be found.
This is important because if I am building a form which used RadDropDownList or RadComboBox with autopostback = false, then the correct values will not be available to the form.
Unfortuately, the solution is to use vanilla ASP.NET DropDownList.
Does your scenario involve dynamically adding the RadDropDownList to the page or it is declared in the markup?
Regards,
Ivan Danchev
Telerik by Progress
I was specifically referring to the DropDownList being created on the server or in the markup. In case it is the former then on every postback the DropDownList is created anew, thus no information about the selected item is kept. Whereas if the control is declared in the markup information about the selected item can be accessed on the server when the postback is initiated by another control (a Button for example). If the item selected has both Text and Value set:
<
telerik:DropDownListItem
Text
=
"ListItem2"
Value
=
"2"
></
telerik:DropDownListItem
>
Regards,
Ivan Danchev
Telerik by Progress
The DropDownList is created in code behind.
I do all of my coding in code behind. With much more complicated Telerik controls, they behave as expected on postback - i.e. providing the updated value.
I am surprised that relatively simpler controls like DropDownList and RadComboBox do not provide the updated values when a form is posted to the server.
As I noted, since the DropDownList does not provide updated values upon postback of a form, I use the ASP.NET DropDownList.
I tested the behavior of a dynamically added RadDropDownList and asp:DropDownList with version 2016.3.1027 of the controls and the RadDropDownList's SelectedValue property contained the expected value after a postback. Could you give the attached runnable sample page a try and elaborate more on the different behavior of both controls you have noticed. It would be helpful if you could modify the sample recreating the specific scenario the difference can be observed in.
Regards,
Ivan Danchev
Telerik by Progress
Thank you for the ButtonPostbackTest.zip file. It does work. When converted to a Control in a page, it does work for this simple coding example.
However, the FormValue for both above tests - in this case RadDDList1_ClientState is empty.
I am expecting that the new selected value is present in the Context.Request.Form posted back values.
It does not matter whether the selected value is '2' or the JSON for the RadDropDownList. It just needs to be present.
After hitting the post this reply, I realized that I do not need to get the value for the RadDropDownList from the form because it is already updated to the new value. Perfect!
There is definitely a note required in the docs.telerik.com for this control to say that it works this way.
Now I am thinking - what a great ASP.NET world it would be if every control worked that way!
Thank you.
Thank you for getting back to us. Both the RadComboBox and RadDropDownList behave identically in this scenario and the SelectedValue property holds the new value after postback.
Regards,
Ivan Danchev
Telerik by Progress