不要在UpdatePanel中使用FileUpload

时间:2014-02-22 16:39:24

标签: c# asp.net ajax asp.net-ajax

<asp:ScriptManager runat="server" ID="S"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="U" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Panel runat="server" ID="U1">
            <asp:Button runat="server" ID="Btnnew" Text="new" OnClick="Btnnew_Click"/>
        </asp:Panel>
        <asp:Panel runat="server" ID="U2">
            <asp:FileUpload runat="server" ID="FU" />
            <asp:Button  runat="server" ID="Btnok" OnClick="Btnok_Click"  Text="ok"/>
        </asp:Panel>
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="btnok" />
    </Triggers>
</asp:UpdatePanel>

和服务器代码是

protected void Page_Load(object sender, EventArgs e)
{
    U1.Visible = true;
    U2.Visible = false;
    U.Update();
}
protected void Btnnew_Click(object sender, EventArgs e)
{
    U1.Visible = false;
    U2.Visible = true;
    U.Update();
}
protected void Btnok_Click(object sender, EventArgs e)
{
    U.Update();
    FU.PostedFile.SaveAs("");
}

但文件上传为空。 没有人可以帮助我!!!

1 个答案:

答案 0 :(得分:0)

很遗憾,您无法使用UpdatePanel内的其他容器来保留FileUpload控件。但你可以这样做:

在标记中:

<asp:ScriptManager runat="server" ID="S"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="U" UpdateMode="Conditional">
    <ContentTemplate>
            <asp:Button runat="server" ID="Btnnew" Text="new" OnClick="Btnnew_Click" />
            <asp:FileUpload runat="server" ID="FU" />
            <asp:Button runat="server" ID="Btnok" OnClick="Btnok_Click" Text="ok" />
    </ContentTemplate>
    <Triggers>
        <asp:PostBackTrigger ControlID="Btnok" />
    </Triggers>
</asp:UpdatePanel>

更改代码以隐藏/取消隐藏控制,并保存文件:

protected void Page_Load(object sender, EventArgs e)
{
    Btnnew.Visible = true;
    FU.Visible = false;
    Btnok.Visible = false;
    U.Update();
}
protected void Btnnew_Click(object sender, EventArgs e)
{
    Btnnew.Visible = false;
    FU.Visible = true;
    Btnok.Visible = true;
    U.Update();
}
protected void Btnok_Click(object sender, EventArgs e)
{
    if (FU.HasFile)
    {
        string fileName = FU.FileName;
        FU.SaveAs(Server.MapPath("~/Images/") + fileName);// Assuming you have Images folder in the root
    }
    U.Update();
}

您可以使用<div><table>,如果您愿意,可以使用css使它们看起来像个小组。