【问题标题】:Flieupload is not working on ascx pageFlieupload 在 ascx 页面上不起作用
【发布时间】:2014-11-11 10:05:27
【问题描述】:

我遇到了一个问题。我在aspx 页面上放置了一个控件ascx。在 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>

【问题讨论】:

  • 上传文件的按钮在哪里
  • @Ganesh_Devlekar 在 ascx 第一部分中用于文件上传的图像按钮
  • 我发布了一个尝试 100% 工作的答案
  • @Ganesh_Devlekar 答案在哪里?我看不见

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


【解决方案1】:

试试这个 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);

【讨论】:

  • 我在我的机器上测试了它不工作的地方。有什么错误吗??
  • AjaxControlToolkit.ToolkitScriptManager.RegisterPostBackControl(ImageButton1);
  • 我添加了这个,错误是需要对象引用
  • 我的页面结构是这样的 Master Page > aspx page > ascx page here is file uploader
  • 错误。在您的用户控件中仅使用 ImageButton1_OnClick 代码,并将其余代码放在您在 .aspx 页面中使用用户控件()的位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-09
相关资源
最近更新 更多