UpdatePanel中的FormView中的FileUpload

时间:2010-08-24 11:26:55

标签: asp.net file-upload updatepanel postback formview

情景:
我有一个ASP.Net网页,我打算用它来让用户(不是真正的用户,但基本上是内容管理员)使用FormView在表格中插入和编辑记录。这个FormView位于UpdatePanel中,因为我还使用级联下拉列表让用户选择一些值。

现在,此FormView还包含4个FileUpload控件,您可能知道这些文件上载控件需要完整的回发,因为大多数浏览器都不允许Javascript访问磁盘。所以,这个问题可以通过以下方式解决:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <Triggers>
           <asp:PostBackTrigger ControlID="InsertButton" />
           <asp:PostBackTrigger ControlID="UpdateButton" />
           </Triggers>
                    <ContentTemplate>....</ContentTemplate>
</asp:UpdatePanel>

编辑:忘记添加文件上传发生在SqlDataSource的OnUpdatingOnInserting事件中。

问题:
由于InsertButtonUpdateButton位于Formview中,因此我无法通过标记直接访问其ID。和MSDN says

  

以编程方式添加   PostBackTrigger控件不是   支撑。

请建议一些解决方案来完成这项工作。任何有关此事的见解都受到高度赞赏。感谢。

P.S.-对我来说可行的解决方案是将UpdatePanel的PostBackTrigger设置为整个FormView本身:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <Triggers>
           <asp:PostBackTrigger ControlID="FormView1" />
           </Triggers>
                    <ContentTemplate>....</ContentTemplate>
</asp:UpdatePanel>

但是现在由于需求的一些变化,这个解决方案(如果你称之为解决方案)是不可接受的。

3 个答案:

答案 0 :(得分:3)

你有没有给过iframe用于回发?类似的东西:

<iframe name="uploader" id=uploader 
          src="uploaderSender.aspx?AllowedExtension=<%= AllowedExtension %>&StoringPath=<%= StoringPath %>&StoringFileName=<%= StoringFileName %>&OldFileName=<%= OldFileName %>&MaximumSize=<%= MaximumSize %>"
         width="450" height="50" frameborder=0  scrolling=no >
        </iframe>

与uploaderSender.aspx一样:

<form action="UploaderReceiver.aspx" method="post"  enctype="multipart/form-data">
 <input type="file" name="file" id="file"  onchange="document.getElementById('IsFileUploading').style.visibility = 'visible'; document.forms[0].submit()"/>

    <span id="IsFileUploading" style="visibility: hidden">
        <asp:Image ID="Image1" runat="server" ImageUrl="~/immagini/Ajax-loader.gif" />
    </span>
</form>

和UploaderReceiver.aspx之类:

protected void Page_Load(object sender, EventArgs e)
        {

            //if there is one file to process
            if (Request.Files.Count > 0)
                //create the folder if it does'nt exists and returns the local path to get it
                string StoringPathToBeSaved = StoringPath.GetFolderPath();

                // append the name of the file to upload to the path.
                            StoringPathToBeSaved = StoringPathToBeSaved + StoringFileName + Extension;

                            Request.Files[0].SaveAs(StoringPathToBeSaved);

        }

这只是一些代码,只是为了让你弄清楚你是否对这种处理上传的方式感兴趣,如果你想要,我可以给你更多。

见到你,祝你的代码好运,

答案 1 :(得分:2)

耶!!终于搞定了!

以下是如何:

与MSDN所说的相反,我们实际上可以通过编程方式添加PostBack触发器。不一定是UpdatePanel,而是ScriptManager

经过几个小时的游戏,这是有效的:

我们无法访问FormView中的控件,直到模板已呈现,因此我们只能在formview的OnDataBound事件后添加回发触发器。

protected void FormView1_DataBound(object sender, EventArgs e)
    {
        if (FormView1.CurrentMode == FormViewMode.Edit)
        {
            LinkButton lb = (LinkButton)FormView1.FindControl("UpdateButton");
            ScriptManager.GetCurrent(Page).RegisterPostBackControl(lb);
        }

        //Similarily you can put register the Insert LinkButton as well.
    }

现在,如果您的UpdatePanel导致ConditionalUpdate,您可以执行以下操作以使其正常工作:

标记:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <ContentTemplate>..
            <EditItemTemplate>
              ...
            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" OnClick="Cause_PostBack"CommandName="Update">Update</asp:LinkButton>
             ...
            </EditItemTemplate>
           ..</ContentTemplate>
</asp:UpdatePanel>

代码隐藏:

//call this function as the OnClick Event Handler for the Controls you want to register as
//triggers.
protected void Cause_PostBack()
    {
        UpdatePanel1.Update();
    }

否则,如果您的情况允许(就像我的情况一样),只需设置UpdatePanel的UpdateMode="Always"

答案 2 :(得分:0)

这是旧的,但试图解决另一个问题,并遇到了这个问题。这不是我的问题,但对于遇到这种情况的新人来说,这是另一种选择。

你说你的问题是:

  

由于InsertButton和UpdateButton驻留在Formview中,我无法通过标记直接访问其ID

您实际上可以通过标记访问其ID,以用作PostBackTrigger中的ControlID。您只需使用页面html标记中创建的按钮的 名称 作为ControlID。当您在浏览器中查看页面时,可以通过查看页面源找到创建的名称。它通常是按钮的FormView + $ +名称。

例如,假设您有一个名为“FormView1”的FormView,其中包含一个Insert按钮,您在设计过程中将ID设为“btnInsert”。如果您在浏览器中打开页面以实时查看它,然后查看页面的来源,您会注意到该按钮的html标记实际上将被赋予名称“FormView1 $ btnInsert”。

在PostBackTrigger中使用该名称作为ControlID,您的更新面板将起作用。

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
       <Triggers>
             <asp:PostBackTrigger ControlID="FormView1$btnInsert" />
       </Triggers>
       <ContentTemplate>....</ContentTemplate>
</asp:UpdatePanel>
相关问题