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

DropDownList set value with javascript

3 Answers 194 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Louis
Top achievements
Rank 1
Iron
Iron
Iron
Louis asked on 12 Jan 2012, 02:41 AM
Hello,

When I add a new row in my grid, I need to change the item of my dropDownlist (in editorTemplate) (actually the first item) for the second one or third one.

In the edit.cshtml page, this is the field in the Grid :

...
columns.ForeignKey(c => c.NoTypeEtape, (System.Collections.IEnumerable)ViewBag.DropDownListTypesEtape, "NoTypeEtape", "DescriptionTypeEtape_fr").Width(100);
....

I try since few hours and I cannot find how to change the value of my dropdownlist (and the text associate with) :
var lst = $('#NoTypeEtape').data('tDropDownList');


I try with previousValue and so on... Any suggestion?

3 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 12 Jan 2012, 11:40 AM
Hello Louis,

You can modify the GridForeignKey editor template (which is used to render the ForeignKey column) so if its used for the property which index you want to change, set the selected index to be the desired one.
e.g.
@(Html.Telerik().DropDownList()
    .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(""))
    .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
        .SelectedIndex(ViewData.TemplateInfo.GetFullHtmlFieldName("") == "NoTypeEtape" ? 1 : 0)
)

This way the initial selected index will be set to 1 when the property name is NoTypeEtape.
I hope this helps.

Regards,
Petur Subev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Louis
Top achievements
Rank 1
Iron
Iron
Iron
answered on 12 Jan 2012, 04:30 PM
A big thank you Petur for your precious help.

But I need to change it regarding the last row of my Grid (the dropdownlist in my new now edit row must be the same value that the same column of the last row).  So, I think I have to manipulate it in JavaScript.  I'm right?
0
Louis
Top achievements
Rank 1
Iron
Iron
Iron
answered on 12 Jan 2012, 05:38 PM
Hey Petur,

Thank you for all, I found it in your demo at :
http://demos.telerik.com/aspnet-mvc/razor/combobox/clientsideapi

        function selectItemInDropDownList()
{     
var dropDownList = $("#DropDownList").data("tDropDownList");           
var index = $("#dropDownListItemIndex").data("tTextBox").value();           
dropDownList.select(index);       
}

Tags
Grid
Asked by
Louis
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Petur Subev
Telerik team
Louis
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or