Need example of code that handles change event for dropdown

1 Answer 4435 Views
General Discussions
Bob
Top achievements
Rank 2
Bob asked on 20 Jan 2013, 03:53 PM
I'm starting to worry a little that I paid a lot of money for something that may not work. The comments on this forum about MVC are scaring me.
What I want to do is to post to my controller when someone changes the selected item on a kendo dropdown list. All of the examples show this as how to process the change event:

function change(e) {
    // handle change event
};


I've got this in my view:
@(Html.Kendo().DropDownList()
    .Name("StatesDDL")
    .DataTextField("StateName")
    .DataValueField("StateID")
    .DataSource(s =>
    {
        s.Read(r =>
            {
                r.Action("GetStates", "Donation");
            });
    })
    .SelectedIndex(selectedState)
    .Height(300)
    //.HtmlAttributes(new { id = "StateDDL" })
     
    .Events(e =>
        {
              
            e.Change("change")/*.Select("select")*/;
        })
)


The control gets populated with all the items and when I click on an item the value in the control changes. The event fires - I can hit the breakpoint that I set in my event handler but I've tried everything I can think of to find out what the new selected value is, but everything shows up as undefined.

Here are just some of the things I've tried:

function change(e) {
 
    var x = $('StatesDDL option:selected').text();
    var y = $('StateDDL option:selected').val();
    var z = $("StatesDDL").val("DataTextField").val("StateName");
    var z1 = $("StatesDDL").data("DataTextField", "StateName").val();
    var z2 = $("StatesDDL").data("DataValueField", "StateID").val();
    var z3 = $('StatesDDL').data();
    var z4 = $(this).text();
 
    alert("Changed");
 
    // how to get value that was selected?
    // how to get the changed value?
    // I want to then use ajax to sent that up to my controller
};



Nothing works. I just don't seem to be able to get the selected value and I can't find any example of how to "handle" the event. I cannot believe that it isn't something really stupid that I'm doing.


1 Answer, 1 is accepted

Sort by
0
Accepted
Dimiter Madjarov
Telerik team
answered on 21 Jan 2013, 03:02 PM
Hello Bob,

This is a quote from my answer in the support thread, that you opened:

Thanks for contacting Telerik support. You are on the right way with this. You can get the selected item in the Select Event of the DropDownList. To get information about this item, you could use the dataItem() method. Here you can find a detailed demo on this topic:

Drop Down List Events Demo

Please let me know if this information helps you. Have a nice day.

 

If you need additional assistance, please continue only one of those threads (forum or support). 

Kind regards,
Dimiter Madjarov
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Thomas
Top achievements
Rank 1
commented on 22 Apr 2014, 07:05 PM

I'm trying to do the same thing as Bob, I need to get the displayed value when a user tab out of the box. if a user Tab out then shift-Tab to go back into the box and use the up or down arrow to select another value, the select event is not called again if they tab out the second time.
Thomas
Top achievements
Rank 1
commented on 22 Apr 2014, 07:31 PM

[quote]Thomas said:I'm trying to do the same thing as Bob, I need to get the displayed value when a user tab out of the box. if a user Tab out then shift-Tab to go back into the box and use the up or down arrow to select another value, the select event is not called again if they tab out the second time.[/quote]

Figured it out myself. Use the same script as the select but instead of e.item.index() you want to use this.selectedIndex
~Robert John
Hilton
Top achievements
Rank 1
commented on 01 Feb 2019, 04:09 PM

I will have to say, this is the worst documentation Telerik has ever put out. Using these controls is supposed to make things easier on the developer not make them spend hours googling for answers because the documentation is spotty at best. :-(
Ivan Danchev
Telerik team
commented on 05 Feb 2019, 12:34 PM

Hello Hilton,

Could you please elaborate more on whether your refer to the Telerik UI for ASP.NET MVC documentation as a whole or to a specific article(s). We would be grateful if you could share your thoughts on what you consider that needs to be improved in it, in order to make the documentation more helpful? In your specific case what information was missing from the documentation that, if present, could have saved you time in searching for an answer?

Regards,
Ivan Danchev
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
General Discussions
Asked by
Bob
Top achievements
Rank 2
Answers by
Dimiter Madjarov
Telerik team
Share this question
or