文件上传在FormView控件的UpdatePanel内 [英] FileUpload in FormView inside an UpdatePanel

查看:185
本文介绍了文件上传在FormView控件的UpdatePanel内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的情景:结果
我有一个ASP.Net网页,我打算用于让用户(不是真正的用户,但内容基本上经理)插入和使用一个FormView在表格编辑记录。这FormView控件是一个UpdatePanel内,因为我还使用级联dropdownlists让用户选择一些值。

The Scenario:
I have an ASP.Net webpage which I intend to use for letting the user(not the real users, but content manager basically) insert and edit the records in a table using a FormView. This FormView is inside an UpdatePanel, as I'm also using cascading dropdownlists to let the user select some values.

现在,这个FormView控件还包含4的FileUpload控件,并且你可能知道,这些文件上传控件需要一个完整的回发,因为大多数浏览器,不要让JavaScript中访问磁盘。所以,这个问题会一直做类似解决:

Now, this FormView also contains 4 FileUpload controls, and as you might know that these fileupload controls require a full postback since most browsers do not let Javascript access the disk. So, this problem would have been solved by doing something like:

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

编辑:忘了补充一点,fileuploading发生在代替 OnUpdating OnInserting 在SqlDataSource事件

Forgot to add that the fileuploading takes place in the OnUpdating and OnInserting events of the SqlDataSource.

问题:结果
因为 InsertButton UpdateButton 位于FormView控件里面,我不能直接通过标记访问他们的ID。和MSDN 说:

The Problem:
Since the InsertButton and the UpdateButton reside inside the Formview, I cannot directly access their ID's through markup. And MSDN says that:

编程添加
  PostBackTrigger控制不
  支持。

Programmatically adding PostBackTrigger controls is not supported.

请提出了一些解决方案,以使这项工作。对此事的任何见解是非常AP preciated。谢谢你。

Please suggest some solution to make this work. Any insight on the matter is highly appreciated. Thanks.

P.S.-我一个可行的解决方案是建立在UpdatePanel的PostBackTrigger作为整个FormView控件本身:

P.S.- A workable solution for me was to set the UpdatePanel's PostBackTrigger as the whole FormView itself:

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

但现在由于位需求的变化,此解决方案(如果你把它叫做一个解决方案)是不能接受的。

But now due to a bit of change in requirements, this solution(if you call it a solution) is not acceptable.

推荐答案

耶!终于得到它的工作!

Yay!! Finally got it to work!

下面是如何:

好了相反的是MSDN说,我们其实可以添加回发触发器编程。不一定是的UpdatePanel ,但对的ScriptManager

Well contrary to what MSDN says, we can in fact add PostBack Triggers Programmatically. Not necessarily to the UpdatePanel, but to the ScriptManager.

后的玩耍时间,这里是什么工作:

After hours of playing around, here's what worked:

我们不能够访问一个FormView内控制,直到该模板已经呈现,所以我们只能添加回发触发后FormView的 OnDataBound 事件。

We are not able to access controls inside a FormView, untill the template has been rendered, so we can only add postback triggers after the formview's OnDataBound Event.

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 ,你可以做这样的事情,使其工作:

And now, if your UpdatePanel causes ConditionalUpdate, you can do something like this to make it work:

标记:

<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>

codeBehind:

CodeBehind:

//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 =始终

这篇关于文件上传在FormView控件的UpdatePanel内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆