文件上传后,UpdatePanel中的ASP.NET控件不起作用

时间:2018-07-25 18:48:35

标签: asp.net file-upload asp.net-ajax updatepanel

我是使用AJAX UpdatePanel的新手,我试图寻找该问题的答案,但尚未找到解决方案。

由于要隐藏和显示基于不同按钮单击的面板,因此我在UpdatePanel中具有多个控件。同样在UpdatePanel内部的是FileUpload控件。在我上传文件之前,一切正常。之后,所有控件(如按钮或单选按钮列表)或JQuery脚本(如日期选择器)均不会在上传后在UpdatePanel中触发。

这是aspx代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Admin.aspx.cs" Inherits="Site.Admin" %>
<%@ MasterType  virtualPath="~/Site1.master"%>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>

        <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
        <script src="//code.jquery.com/jquery-1.12.4.js"></script>
        <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
        <script type="text/javascript" src="scripts/jquery-ui-timepicker-addon.js"></script>
        <link rel="stylesheet" href="Content/jquery-ui-timepicker-addon.css">

    <script type="text/javascript"> 
        function pageLoad() { 
            $(function () { 
                Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

                function EndRequestHandler(sender, args) {
                    $('.picker').datetimepicker();
                }
            }); 
        } 
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True"></asp:ScriptManager>
            <asp:UpdatePanel ID="adminUpdatePanel" UpdateMode="Conditional" ChildrenAsTriggers="True" runat="server">
                    <ContentTemplate>

                <div class="form-group">
                                    <div class='input-group date' id='datetimepicker3'>
                                        <asp:TextBox ID="tb_EndDate" class="form-control picker" runat="server"></asp:TextBox>
                                        <span class="input-group-addon">
                                                <span class="glyphicon glyphicon-calendar"></span>
                                        </span>
                                    </div>
                            </div>

                            <div class="form-group">
                                    <asp:RadioButtonList ID="rbl_Active" runat="server" OnSelectedIndexChanged="rbl_Active_SelectedIndexChanged" RepeatDirection="Horizontal" CellPadding="4" CellSpacing="4" AutoPostBack="True">
                                        <asp:ListItem>Yes</asp:ListItem>
                                        <asp:ListItem>No</asp:ListItem>
                                    </asp:RadioButtonList>
                            </div>

                <div class="form-group">
                                    <label>Upload Photo</label>
                                        <asp:FileUpload ID="imageFileUpload" runat="server" />                                    
                                        <asp:Button class="btn btn-primary" ID="btn_UploadImg" runat="server" Text="Upload Image" OnClick="btn_UploadImg_Click" CausesValidation="False" />
                                        <asp:Button ID="btn_CancelImg" OnClick="btn_CancelImg_Click" class="btn btn-primary" runat="server" Text="Change/Remove Image" CausesValidation="False" />
                            </div>
            </ContentTemplate>
            </asp:UpdatePanel>
</asp:Content>

以及与文件上传有关的代码:

protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.GetCurrent(this).RegisterPostBackControl(this.btn_UploadImg);
        }

protected void btn_UploadImg_Click(object sender, EventArgs e)
        {
            bool isUploaded = uploadImage(imageFileUpload);
            if (isUploaded)
            {                
                btn_CancelImg.Visible = true;

            }
        }

我不确定自己在做什么错。从Visual Studio运行时,它工作正常,但是当推送到服务器时,一切都会失败

1 个答案:

答案 0 :(得分:0)

除了将 Visible 设置为true / false之外,您还可以尝试:

 <asp:Panel ID="pnlToHide" runat="server" style="display:none">

必须显示时,可以使用以下内容:

protected void btnShow_Click(object sender, EventArgs e)
{
    pnlToHide.Attributes["style"] = "display: '';";
}

此外,您还可以尝试使用 完整回发/呈现

<Triggers>
    <asp:PostBackTrigger ControlID="btnFileUpload"/>
</Triggers>