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

[Solved] Set default values on new record

3 Answers 171 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.
JULIA
Top achievements
Rank 1
JULIA asked on 27 Jul 2010, 11:58 AM
Hello,

if i add a new line in my grid, the dropdownlist will always have the value from the first line. how can i set this value manually?
I have the following EditorTemplate

<%= Html.DropDownList( null, new SelectList( ( IEnumerable )ViewData["karnofskyEcogTypes"],
                                    "KarnofskyEcogTypeId", "LongDescription" ), new { style = "width: 90%;" } )%>
  
<script type="text/javascript">
    var $dropdown = $('#KarnofskyEcogType');
    $('<option/>', { text: '...', value: "" }).prependTo($dropdown);
    var $tr = $dropdown.closest('tr:has(form)');
    var grid = $tr.closest('.t-grid').data('tGrid');
    var dataItem = grid.dataItem($tr);
    $dropdown.val(dataItem.KarnofskyEcogType ? dataItem.KarnofskyEcogType.KarnofskyEcogTypeId : "");
</script>

The only way for validation ( with the RequiredAttrbiute ) is to let the value an empty string. is this the best way?

Regards,
Timo

3 Answers, 1 is accepted

Sort by
0
Peter
Top achievements
Rank 1
answered on 28 Jul 2010, 09:56 AM
Hi,

I believe that you need to use this SelectList constructor in order to select initial value.

Regards,
Peter
0
JULIA
Top achievements
Rank 1
answered on 29 Jul 2010, 01:27 PM
Hi,

it doesn't solve the problem.
The following function is running after the click on "Add New".
$(function () {
        $(".t-grid-add").click(function () {
            alert($('.t-grid-new-row').find("#KarnofskyEcogType").length);
        });
    });

But the alert shows always 0. How can i find this DropDownList. The second click on add new (without cancel the new row) shows 1.
Is the DropDownList not inside the DOM when the click occurs?

Regards,
Timo
0
JULIA
Top achievements
Rank 1
answered on 16 Aug 2010, 02:04 PM
Hello,

the problem is the code inside the EditorTemplate

<%= Html.DropDownList( null, new SelectList( ( IEnumerable )ViewData["karnofskyEcogTypes"],
                                    "KarnofskyEcogTypeId", "LongDescription" ), new { style = "width: 90%;" } )%>
  
<script type="text/javascript">
    var $dropdown = $('#KarnofskyEcogType');
    $('<option/>', { text: '...', value: "" }).prependTo($dropdown);
    var $tr = $dropdown.closest('tr:has(form)');
    var grid = $tr.closest('.t-grid').data('tGrid');
    var dataItem = grid.dataItem($tr);
    $dropdown.val(dataItem.KarnofskyEcogType ? dataItem.KarnofskyEcogType.KarnofskyEcogTypeId : "");
</script>

The property "dataItem.KarnofskyEcogType.KarnofskyEcogTypeId" is always set to the value from the first row. Is there a workaround or way to find out if this dataItm is inserted?
Tags
Grid
Asked by
JULIA
Top achievements
Rank 1
Answers by
Peter
Top achievements
Rank 1
JULIA
Top achievements
Rank 1
Share this question
or