Flieupload无法在ascx页面上运行

时间:2014-09-17 11:30:59

标签: c# asp.net file-upload updatepanel

我遇到了问题。我在ascx页面放置了一个控件aspx。在aspx页面我正在使用UpdatePanel。在ascx页面,我使用Formview。在Formview <InsertItemTemplate>我使用控件asp:FileUpload进行文件上传。选择文件后,当我检查FileUpload.HasFile时,它总是给我假。我尝试解雇<Triggers>但不是 成功,因为文件上传位于我的ascx页面上。在下面的例子中,我正在展示我的问题部分。

code 
        FileUpload _fileUpload = FormView1.FindControl("FileUpload1") as FileUpload;
    if (_fileUpload != null && _fileUpload.HasFile)
        {
        /// some code i write here 
        }
  

ASPX

<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Always">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="GridView1" />
                <asp:PostBackTrigger ControlID="imagAddNew" />
                <asp:AsyncPostBackTrigger ControlID="EditProduct1" />
                 <asp:PostBackTrigger ControlID="ImageButton1" />
            </Triggers>
  <ContentTemplate>
  <asp:Panel ID="pnl_grid" Style="width: 100%; overflow: auto;" runat="server">
      <uc1:EditProduct ID="EditProduct1" runat="server" />
   </asp:Panel>
  

ASCX

    <asp:FormView ID="FormView1" runat="server"  Width="100%" ondatabinding="FormView1_DataBinding">
  <InsertItemTemplate>
<table>
<tr>
        <td class="label-col">
                   Image
                 </td>
                 <td class="data-col">
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                 </td>
<td>
  <asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="True" CommandName="Insert"
                                ImageUrl="~/images/save.gif" ValidationGroup="Inser" />
</td>
</tr>
</table>
  </InsertItemTemplate>
</asp:FromView>

1 个答案:

答案 0 :(得分:0)

尝试100%正常工作

此处FileUpload控件嵌套在FormView内,因此我找到了控件ImageButton,然后将PostBackTrigger添加到该控件中。所以你可以将相同的逻辑应用于GridView,Repeater,DataList等,你可以使用ToolScriptManagerScriptManager注册PostBackControl

像这样使用图像按钮

         <asp:ImageButton ID="ImageButton1" runat="server"
 OnClick="ImageButton1_OnClick" CausesValidation="True" CommandName="Insert"
    ImageUrl="~/images/save.gif" ValidationGroup="Inser" />

在您的.aspx页面中,您在页面加载时导入用户控件使用此

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            FormView ProductsFormView = (FormView)EditProduct1.FindControl("ProductsFormView");

            FindAllTextBoxes(ProductsFormView);


        }
    }



private void FindAllTextBoxes(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            if (c.GetType().ToString() == "System.Web.UI.WebControls.ImageButton")
            {
                ImageButton ImageButton1 = (ImageButton)c.FindControl("ImageButton1");
                if (ImageButton1 != null)
                {

                    ToolScriptManager1.RegisterPostBackControl(ImageButton1);
//or ScriptManager.RegisterPostBackControl(ImageButton1);
                }
            }
            if (c.Controls.Count > 0)
            {
                FindAllTextBoxes(c);
            }
        }
    }





protected void ImageButton1_OnClick(object sender, EventArgs e)
    {

        ImageButton ImageButton1 = (ImageButton)sender;
        FormViewRow row = (FormViewRow)ImageButton1.Parent.Parent;

        FileUpload FileUpload1 = (FileUpload)row.FindControl("FileUpload1");

        if (FileUpload1.HasFile)
        {

        }



    }

如果您正在使用带有母版页的ContentPlace Holder

ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
                    ToolkitScriptManager ToolScriptManager1 = (ToolkitScriptManager)ContentPlaceHolder1.FindControl("ToolScriptManager1");
                    ToolScriptManager1.RegisterPostBackControl(ImageButton1);