提交后BeginFormSitefinity表单消失 [英] BeginFormSitefinity form disappears after submitting

查看:73
本文介绍了提交后BeginFormSitefinity表单消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用BeginFormSitefinity助手时,表单在提交后消失,然后回发完成.

When I use the BeginFormSitefinity helper the form disappears after submitting, and then the postback is done.

预期的行为只是要做的回发.我去浏览器进行调查,发现当您使用BeginFormSitefinity时,会在HTML中添加一个额外的脚本.

The expected behavior would be only the postback to be done. I went to the browser and investigated and I found out that when you use BeginFormSitefinity an extra script is added to your html.

该脚本基本上创建了一个新的带有display = none的表单.并将您的表单附加到不可见"表单上,因此您的表单在提交后就会消失.

That script basically creates a new form with display = none. And appends your form to the "invisible" form, and thus your form disappears after submitting.

我正在使用Sitefinity 8.1,我想知道是否有任何方法可以避免这种情况?

I'm using Sitefinity 8.1, and I would like to know if is there any way to avoid this?

这是添加的脚本(我无法控制):

This is the script added (I have no control over it):

<script type="text/javascript">
            (function () {
                var container = document.getElementById("myForm2");
                if (container === null)
                    return;

                var inputs = container.querySelectorAll("input");
                var allInputs = document.forms["aspnetForm"].querySelectorAll('input');
                for (var i = 0; i < allInputs.length; i++) {
                    allInputs[i].addEventListener("invalid", function(event) {
                        if (Array.indexOf(inputs, document.activeElement) >= 0 && Array.indexOf(inputs, event.target) < 0)
                            event.preventDefault();
                    }, true);
                }

                var submitClick = function () {
                    var isValid = true;
                    for (var i = 0; i < inputs.length; i++) {
                        if (typeof inputs[i].willValidate !== "undefined" && inputs[i].willValidate)
                            isValid = inputs[i].validity.valid && isValid;

                        if (typeof jQuery !== "undefined" && typeof jQuery.validator !== "undefined")
                            isValid = jQuery(inputs[i]).valid() && isValid;
                    }

                    if (isValid) {
                        var form = document.createElement("form");

                        form.style.display = "none";
                        form.setAttribute("action", "/order-calendar/Search");
                        form.setAttribute("method", "POST");
                        form.setAttribute("enctype", document.forms["aspnetForm"].getAttribute("enctype"));
                        form.setAttribute("encoding", document.forms["aspnetForm"].getAttribute("encoding"));                            

                        form.appendChild(container);

                        document.body.appendChild(form);

                        // We prevent kendo upload widget from submitting empty inputs.
                        var kInputs = container.querySelectorAll(".k-upload input[type='file']");
                        for(var i = 0; i < kInputs.length; i++) {
                            var kInput = kInputs[i];
                            if (!kInput.value) {
                                // Prevent submitting an empty input
                                kInput.setAttribute("disabled", "disabled");

                                window.setTimeout(function() {
                                    kInput.removeAttribute("disabled");
                                }, 0);
                            }
                        }

                        form.submit();

                        return false;
                    }
                };

                var handleFormSubmitElements = function (elementName) {
                    var allSubmitElements = container.getElementsByTagName(elementName);
                    var elementCount = allSubmitElements.length;
                    while(elementCount) {
                        typeAttr = allSubmitElements[elementCount - 1].getAttribute("type");
                        if(typeAttr == "submit") {
                            allSubmitElements[elementCount - 1].onclick = submitClick;
                        }
                        elementCount--;
                    }
                };

                handleFormSubmitElements("input");
                handleFormSubmitElements("button");
            })();
        </script>

这是我的表格(它的值是自动读取的,这就是onkeydown返回false的原因):

This is my form (its values are read automatically, that's why onkeydown returns false):

@using (Html.BeginFormSitefinity("Search", "myForm2"))
    {
     <div id="main_content" style="max-width: 600px; max-height:700px;float:left;overflow: hidden;">

     <table>
                <tr><td colspan="2" style="text-align:center;">@Html.Label("City")</td></tr>

                <tr>
                    <td>@Html.Label("Code") </td>
                    <td>

                        @Html.Kendo().TextBoxFor(x => x.Code).HtmlAttributes(new { onkeydown = "return false", style = "color: green; width:100%;", id = "Code" })
                    </td>
                </tr>
                <tr>
                    <td>@Html.Label("City Code") </td>
                    <td>

                        @Html.Kendo().TextBoxFor(x => x.CityCode).HtmlAttributes(new { onkeydown = "return false", style = "color: green; width:100%;", id = "CityCode" })
                    </td>
                </tr>

      </table>

    <input type="submit" class="btn btn-success" value="submit">
}

这是视觉上发生的事情:

This is what happens visually:

推荐答案

我们的团队也遇到了同样的问题.已创建到Telerik的支持票.有一个答案:

Our team also had the same issue. Was created support ticket to Telerik. And there is an answer:

本质上,这是使用BeginFormSitefinity时的预期行为.发生这种情况的原因是,在ASP.NET Web表单中,页面上仅允许使用一种带有runat ="sever"的表单.这就是为什么BeginFormSitefinity在默认情况下呈现为div并单击提交按钮时动态创建并提交表单的原因.为了克服此行为,您将需要创建一个自定义帮助程序和HybridForm.您可以查看以下 StackOverflow答案,以获取有关如何创建自定义HybridForm的更多信息.

Essentially, this is expected behavior when using the BeginFormSitefinity. The reasons this happens is because in ASP.NET web forms only one form with runat="sever" is allowed on a page. This is why the BeginFormSitefinity renders as a div by default and a form is dynamically created and submitted when the submit button is clicked. In order to overcome this behavior you will need to create a custom helper and HybridForm. You can take a look at the following StackOverflow answer for more information on how to create a custom HybridForm.

这篇关于提交后BeginFormSitefinity表单消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆