0

我有一个文件上传控件的问题,我有一个手风琴,在 ContentTemplate 里面我有一个文件上传控件,当提交发送按钮时,我试图找到该控件,但总是让它为空。

<asp:Accordion ID="Accordion1" runat="server" AutoSize="None" FramesPerSecond="40"
                SelectedIndex="-1" TransitionDuration="100" FadeTransitions="true" RequireOpenedPane="false"
                SuppressHeaderPostbacks="true" ContentCssClass="accordionContent" HeaderCssClass="accordionHeader"
                HeaderSelectedCssClass="accordionHeaderSelected" 
                OnItemCommand="Accordion1_ItemCommand" 
                onitemdatabound="Accordion1_ItemDataBound">
                <HeaderTemplate>
                    <%#DataBinder.Eval(Container.DataItem, "order_id")%>
                    <asp:Label ID="lblOrderID" runat="server" Text='<%#Eval("description")%>'></asp:Label>
                </HeaderTemplate>
                <ContentTemplate>
                    <asp:Label ID="lblNotes" runat="server" Text='<%#Eval("notes")%>'></asp:Label>
                    <asp:Label ID="lbltext" runat="server" Text='להגשת מועמדות למשרה זו צרף קו"ח'></asp:Label>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                    <asp:Button ID="btnSend" runat="server" Text="שלח" CommandName="send" />
                </ContentTemplate>
            </asp:Accordion>

 protected void Accordion1_ItemCommand(object sender, CommandEventArgs e)

    {
        if (e.CommandName == "send")
        {
            FileUpload fu = new FileUpload();
            fu = (FileUpload) FindControl("FileUpload1");
            if (fu.HasFile)
            {
                SmtpClient client = new SmtpClient();
                MailAddress from = new MailAddress("", "יש לך הודעה חדשה", System.Text.Encoding.UTF8);
                MailAddress to = new MailAddress("");
                MailMessage message = new MailMessage(from, to);
                message.Body = "";
                //Attachment data = new Attachment(FileUpload.
                //message.Attachments.Add(FindControl("fu"));
            }
        }
    }
4

1 回答 1

0

通过执行 FindControl,您将在顶级容器中进行搜索,并且 FindControl 不是递归的。如果您想找到上传控件,您需要从直接父级搜索,这似乎是 Accordion1。

FileUpload fu = Accordion1.FindControl("FileUpload1") as FileUpload;
于 2012-04-22T13:46:40.723 回答