简体   繁体   中英

Literal control not updating

I am using a plugin called uploadify to show file upload progress to users. The uploadify script calls default.aspx (asynchronously). In the Page_Load method of the default.aspx I run validation checks on the other form data that was passed through it.

If a validation check fails I like to display an error message using a literal control and then exit. The problem is the literal control is not being updated with the validation error messages.

Updated

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;


        if (context.Request.Files["Filedata"] != null)
        {
            if (context.Request["Name"] == null)
            {
                litValidationErrors.InnerHtml = "Please enter a name";

                return;
            }
        }
    }
}


 <script type="text/javascript">
    $(document).ready(function ()
    {
        $('#file_upload').uploadify({
            'uploader': '/Plugins/Uploadify/uploadify.swf',
            'script': '/default.aspx',
            'cancelImg': '/Plugins/Uploadify/images/cancel.png',
            'folder': '/FileUploads',
            'auto': false,

            'onComplete': function (event, ID, fileObj, response, data)
            {
                var uploadifyResponse = $("#<%= litValidationErrors.ClientID %>", $(response));

                if (uploadifyResponse.length > 0)
                {
                    $("#<%= litValidationErrors.ClientID %>").css("display", "inline").text(uploadifyResponse.text());
                }
            }

        });


        $('#MainContent_superSubmit').click(function ()
        {
            var jsonFormData = {
                'Name': $('#MainContent_txtName').val(),
                'Password': $('#MainContent_txtPassword').val()
            };

            $('#file_upload').uploadifySettings('scriptData', jsonFormData);
            $('#file_upload').uploadifyUpload();
        });
    });
</script>


    <html>
    .....
    <asp:Button ID="superSubmit" runat="server" Text="Button"  />
 <span id="litValidationErrors" runat="server" style="display: none; color: #ff0000;"></span>
    </html>

Change the litValidationErrors control from Literal to the span with runat="server", remove Visible="false" and hide it by setting style="display: none;". Also, add the onComplete event handler to the uploadify:

$(function () {
        $('#file_upload').uploadify({
            'uploader': '/Plugins/Uploadify/uploadify.swf',
            'script': '/WebForm1.aspx',
            'expressInstall': '/Plugins/UploadifyexpressInstall.swf',
            'cancelImg': '/Plugins/Uploadify/images/cancel.png',
            'folder': '/App_Data/FileUploads',
            'auto': false,

            'onComplete': function (event, ID, fileObj, response, data) {
                var uploadifyResponse = $("#<%= litValidationErrors.ClientID %>", $(response));

                if (uploadifyResponse.length > 0) {
                    $("#<%= litValidationErrors.ClientID %>").css("display", "inline").text(uploadifyResponse.text());
                }
            }
        });
    });

Add callback function to your script that will update the text of literal control

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM