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

DropDownList posted value

16 Answers 459 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
George
Top achievements
Rank 2
George asked on 07 Mar 2015, 03:44 AM
I have built a custom from on the server using Rad controls that include a DropDownList.  All the controls return a select value except for DropDownList.

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
key = "ctl00_MainContentMembers_tsupcontrol_0_ClientState"
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

Sort by
0
George
Top achievements
Rank 2
answered on 09 Mar 2015, 01:35 AM
The dropdown is only value that uses ClientState so I had to was required to write special code to look after this bug just to get the string.

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.

0
Boyan Dimitrov
Telerik team
answered on 11 Mar 2015, 03:48 PM
Hello George,

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.

 
0
George
Top achievements
Rank 2
answered on 11 Mar 2015, 04:34 PM
I take it that you do not consider it a bug for the dropdown list to not return data reliably and in a manner consistent with the other controls. Personally I think this is completely unacceptable for software that costs as much as it does.

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
0
Accepted
Boyan Dimitrov
Telerik team
answered on 16 Mar 2015, 03:45 PM
Hello 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.

 
0
George
Top achievements
Rank 2
answered on 16 Mar 2015, 04:47 PM
My form could have some number of dropdowns, doing any work on the client is not very realistic.

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. 
0
Accepted
Boyan Dimitrov
Telerik team
answered on 19 Mar 2015, 04:02 PM
Hello,

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.

 
0
George
Top achievements
Rank 2
answered on 26 Mar 2015, 04:28 PM
The final answer from a ticket is that the RadDropDownList does not post data because of its implementation as bunch of divs.  That reminds me  of Kendo.  ClientState is an Telerik internal data item posted for the Telerik control.  ClientState cannot be relied upon because it only posts when the control is changed which messes up on validations.

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
0
Charles Wiebe
Top achievements
Rank 1
answered on 21 Nov 2016, 07:40 PM

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.

0
Ivan Danchev
Telerik team
answered on 24 Nov 2016, 11:09 AM
Hello Charles,

Does your scenario involve dynamically adding the RadDropDownList to the page or it is declared in the markup?

Regards,
Ivan Danchev
Telerik by Progress
Telerik UI for ASP.NET AJAX is ready for Visual Studio 2017 RC! Learn more.
0
Charles Wiebe
Top achievements
Rank 1
answered on 24 Nov 2016, 01:03 PM
The code is added dynamically in code behind.
0
Ivan Danchev
Telerik team
answered on 25 Nov 2016, 12:21 PM
Hello Charles,

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>
the DropDownList's SelectedText and SelectedValue properties will return the corresponding values ("ListItem2" and "2").

Regards,
Ivan Danchev
Telerik by Progress
Telerik UI for ASP.NET AJAX is ready for Visual Studio 2017 RC! Learn more.
0
Charles Wiebe
Top achievements
Rank 1
answered on 25 Nov 2016, 02:06 PM

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.

0
Ivan Danchev
Telerik team
answered on 25 Nov 2016, 02:35 PM
Hello Charles,

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
Telerik UI for ASP.NET AJAX is ready for Visual Studio 2017 RC! Learn more.
0
Charles Wiebe
Top achievements
Rank 1
answered on 25 Nov 2016, 04:21 PM

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.

0
Charles Wiebe
Top achievements
Rank 1
answered on 25 Nov 2016, 04:29 PM

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.

0
Ivan Danchev
Telerik team
answered on 28 Nov 2016, 03:59 PM
Hello Charles,

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
Telerik UI for ASP.NET AJAX is ready for Visual Studio 2017 RC! Learn more.
Tags
DropDownList
Asked by
George
Top achievements
Rank 2
Answers by
George
Top achievements
Rank 2
Boyan Dimitrov
Telerik team
Charles Wiebe
Top achievements
Rank 1
Ivan Danchev
Telerik team
Share this question
or