Kendo DropDownList Shows DataValueField Prior to DataTextField when Loading

青春壹個敷衍的年華 提交于 2019-12-04 18:22:56

The problem is probably caused by the .Deferred() statement, which delays the widget initialization until the deferred scripts are rendered via

@Html.Kendo().DeferredScripts()

I assume there is something time consuming taking place between the rendering of the DropDownList textbox and the widget initialization. So you are seeing the numeric value inside the plain non-initialized textbox. I have two suggestions:

  • move the DeferredScripts() call closer to the DropDownList declaration, if possible.
  • if the above is not possible or does not yield the desired result, then hide the DropDownList temporarily until it is initialized.

For example:

DropDownList and JavaScript

@(Html.Kendo().DropDownList()
    .Name("products")
    .DataTextField("ProductName")
    .DataValueField("ProductID")
    .Filter("contains")
    .DataSource(source =>
    {
        source.Read(read =>
        {
            read.Action("GetProducts", "Home");
        })
        .ServerFiltering(true);
    })
    .Value("3")
    .Deferred()
    .HtmlAttributes(new { @class = "temporary-hidden" })
)

// ...Something else here...

@Html.Kendo().DeferredScripts()

<script>

    // place this script immediately after the DeferredScripts() call
    // and in a document.ready handler to ensure it is executed at the right time

    $(function () {
        $(".temporary-hidden").removeClass("temporary-hidden");
    })

</script>

CSS

.temporary-hidden
{
    visibility: hidden;
}

Contrary to display:none, the visibility:hidden style will make the DropDownList textbox occupy space on the screen even while hidden, so you will avoid flickering and layout readjustments.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!