簡體   English   中英

Flieupload在ASCX頁面上不起作用

[英]Flieupload is not working on ascx page

我面臨一個問題。 我在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>

試試這個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與Master頁面一起使用,則

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM