我正在为文件上传要求创建网页。每个页面都有一个MasterType模板。我将其包含在我的aspx文件中,如下所示:
<%@ MasterType VirtualPath="~/MasterPage/Main.master" %>
我遵循了我在网上找到的使用更新面板实现fileupload功能的内容:
<asp:Panel ID="panel_Actions" runat="server">
<asp:UpdatePanel ID="upanel_Actions" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Button ID="btn_downloadEmpTemplate" runat="server" OnClick="btn_downloadEmpTemplate_Click" Text="Download Template"/>
<asp:FileUpload runat="server" ID="fu_excelUpload" />
<asp:Button ID="btn_uploadSubmit" runat="server" OnClick="btn_UploadSubmit_Click" Text="Upload"/>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btn_uploadSubmit" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
[每当我按下提交(btn_uploadSubmit
)时,母版页模板就会完全消失。而且我只剩下页面内容。发生了什么事?
我的pageLoad
和我的onclick
用于上传提交的功能:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
System.Web.UI.ScriptManager.GetCurrent(this).RegisterPostBackControl(this.btn_downloadEmpTemplate);
}
protected void btn_UploadSubmit_Click(object sender, EventArgs e)
{
string s = this.fu_excelUpload.FileName;
}
编辑:我认为问题可能与该项目中脚本管理器的实现方式有关。它存在于主模板而不是页面中。包括一些主代码:
<head>...</head>
<body>
<form id="form" runat="server" >
<asp:ToolkitScriptManager ID="tsm" runat="server" AjaxFrameworkMode="Enabled" AsyncPostBackTimeout="1800" EnablePartialRendering="true" LoadScriptsBeforeUI="true" ScriptMode="Auto"></asp:ToolkitScriptManager>
<div>...</div>
<div>...</div>
<div>...</div>
<div>...</div> // main content content template here
<script>...</script>
</form>
</body>
</html>
警告:这是“在我的机器上工作”的文章,无论如何都不是复制和粘贴解决方案。
我假设您已经看到official documentation和FileUpload
的互操作性UpdatePanel
:
当您在UpdatePanel控件中使用FileUpload控件时,必须使用作为面板的PostBackTrigger对象的控件来上传文件。
查看您的代码,看来您已经正确理解了这一部分。在您的上下文中,MasterType
指令似乎也没有什么区别-它向页面中添加了正确键入的MasterType
属性(否则,您将获得祖先Master
并必须手动进行强制转换),您似乎并没有使用在这里。
我创建了新的WebForms项目(.net 4.7.2),并应用了一些代码:
MasterPage
<%@ Master Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<asp:ContentPlaceHolder id="HeaderContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<h1>Custom Master</h1>
<form id="form1" runat="server">
<asp:ScriptManager runat="server">
<Scripts>
<%--Framework Scripts--%>
<asp:ScriptReference Name="MsAjaxBundle" />
<asp:ScriptReference Name="jquery" />
<asp:ScriptReference Name="bootstrap" />
<asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
<asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
<asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
<asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
<asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
<asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
<asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
<asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
<asp:ScriptReference Name="WebFormsBundle" />
<%--Site Scripts--%>
</Scripts>
</asp:ScriptManager>
<asp:ContentPlaceHolder id="MainContent" runat="server"></asp:ContentPlaceHolder>
</form>
</body>
</html>
<%@MasterType VirtualPath="CustomMaster.master" %>
<%@ Page Language="C#" MasterPageFile="CustomMaster.master" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="WebApplication4.Test" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:Panel ID="panel_Actions" runat="server">
<asp:UpdatePanel ID="upanel_Actions" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Button ID="btn_downloadEmpTemplate" runat="server" OnClick="btn_downloadEmpTemplate_Click" Text="Download Template"/>
<asp:FileUpload runat="server" ID="fu_excelUpload" />
<asp:Button ID="btn_uploadSubmit" runat="server" OnClick="btn_UploadSubmit_Click" Text="Upload"/>
<asp:Label ID="lbl_filename" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btn_uploadSubmit" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
</asp:Content>
据我所知-该组合按预期工作,所以建议您仔细查看一下您的实际代码,看看是否有明显不同的地方(using System;
using System.Web.UI;
namespace WebApplication4
{
public partial class Test : Page
{
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.GetCurrent(this).RegisterPostBackControl(this.btn_uploadSubmit);
}
protected void btn_UploadSubmit_Click(object sender, EventArgs e)
{
lbl_filename.Text = this.fu_excelUpload.FileName;
}
protected void btn_downloadEmpTemplate_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
}
中的文件可能有所不同),或添加更多代码您的问题。