UpdatePanel中的Repeater中的AsyncFileUpload

时间:2017-12-05 09:22:55

标签: c# asp.net updatepanel repeater asyncfileupload

AsyncFileUpload有效。唯一的问题是当按下重复AsyncFileUpload控件的LinkBut​​ton时文件名消失。有没有办法获取和存储文件名? FileName不起作用。不是真的热衷于共享代码隐藏,但如果有必要解决这个问题,可能会这样做。

<asp:UpdatePanel ID="LibraryResourceUpdatePanel" runat="server">
        <ContentTemplate>
            <div class="field-group list-of-resource">
                <asp:Repeater ID="RptRequest" runat="server" OnItemDataBound="RptRequest_ItemDataBound">
                    <ItemTemplate>
                        <div class="resource">
                            <div class="remove-input">
                                <asp:LinkButton ID="LbRemoveRequest" CssClass="ic fa fa-minus-circle" runat="server" OnClick="LbRemoveRequest_Click" CausesValidation="false"></asp:LinkButton>
                                <span>Remove</span>
                            </div>
                            <h2>Details of Resources 
                        <span class="counter">
                            <asp:Literal ID="LitCount" runat="server"></asp:Literal>
                        </span>
                            </h2>
                            <ul>
                                <li>
                                    <fieldset class="form-group">
                                        <legend>Accession No.</legend>
                                        <asp:TextBox ID="TxbAccessionNumber" CssClass="form-control" runat="server" />
                                        <asp:RequiredFieldValidator runat="server" ControlToValidate="TxbAccessionNumber" ErrorMessage="Email is required" ForeColor="Red" Display="Dynamic" />
                                    </fieldset>
                                </li>
                                <li>
                                    <fieldset class="form-group">
                                        <legend>Details</legend>
                                        <asp:TextBox ID="TxbDetails" runat="server" Rows="4" TextMode="MultiLine" />
                                        <asp:RequiredFieldValidator runat="server" ControlToValidate="TxbDetails" ErrorMessage="Details are required" ForeColor="Red" Display="Dynamic" />
                                    </fieldset>
                                </li>
                                <li>
                                    <fieldset class="form-group">
                                        <legend>Image</legend>
                                        <ajaxToolkit:AsyncFileUpload runat="server"
                                            ID="FileUpload" OnUploadedComplete="FileUpload_UploadedComplete" ClientIDMode="AutoID" PersistFile="true"/>
                                        <asp:RequiredFieldValidator runat="server" ControlToValidate="FileUpload" ErrorMessage="File Upload required" ForeColor="Red" Display="Dynamic" />
                                    </fieldset>
                                </li>
                            </ul>
                        </div>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
            <div class="add-input">
                <asp:LinkButton ID="LbAddRequest" CssClass="ic fa fa-plus-circle" runat="server" OnClick="LbAddRequest_Click" CausesValidation="false" ></asp:LinkButton>
                <span>Add another request</span>
            </div>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="LbAddRequest" EventName="click" />
        </Triggers>
    </asp:UpdatePanel>

1 个答案:

答案 0 :(得分:0)

首先将命令名添加到linkbutton并删除click事件:

<asp:LinkButton ID="LbAddRequest" runat="server" 
    CommandName="AddRequest"></asp:LinkButton>
<span>Add another request</span>

在代码隐藏中尝试此ItemCommand事件以开始工作:

protected void RptRequest_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "AddRequest")
    {
        FileUpload myFileUpload = (FileUpload)e.Item.FindControl("FileUpload");
        if (myFileUpload.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(myFileUpload.FileName);
                myFileUpload.SaveAs(Server.MapPath("~/") + filename);
                myStatusLabel.Text = "Upload Success";
            }
            catch (Exception ex)
            {

                myStatusLabel.Text = "Upload Fail" + ex.Message;
            }
        }
        else
        {

            myStatusLabel.Text = "myFileUpload Has No File";
        }
    }      
}

在此处阅读详细信息:https://forums.asp.net/t/1904302.aspx?FileUpload+Inside+a+Repeater+Can+t+Find+File