0
votes

I have a fileUpload control and Button inside datalist which is inside UpdatePanel and user need to upload file using fileUpload control and button.How to add postback trigger for the button so that the selected files via fileupload get uploaded to server.

         <asp:UpdatePanel runat="server" ID="UpdatePanel">
                    <ContentTemplate>

     <asp:DataList ID="dlComments" runat="server" Width="100%" 
                                      onitemcommand="dlComments_ItemCommand" DataKeyField="CommentID" 
                                      onitemdatabound="dlComments_ItemDataBound" >
                                 <ItemTemplate>
<asp:Label ID="lblSnNo" runat="server" class="label-small-grey" Text='<%#Eval("CommentNumber") %>' ></asp:Label>
<label style="font-size:12px; font-style:normal"><%#Eval("CommentText") %></label>
              <textarea runat="server" id="txtaCommentText" style="resize:none;" rows="3" cols="3" class="form-control"  placeholder="Your Comments Here"></textarea><Br>
            <asp:FileUpload ID="fileUploadReply" multiple="true" runat="server" class=""   /></p><br>
            <asp:Button ID="btnPostReplyComment" CommandName="PostReplyComment" class="btn btn-primary" runat="server" Text="Post" />

    </ItemTemplate>

                        </asp:DataList>
                   </ContentTemplate>



        <Trigger>
        </Triggers>
            </asp:UpdatePanel>

On codeBehind,

 protected void dlComments_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "PostReplyComment")
{

FileUpload fileUploadReply = (FileUpload)e.Item.FindControl("fileUploadReply");
 functionTOStoreCommetns();
     string filename = Path.GetFileName(fileUploadReply.FileName);
            fileUploadReply.SaveAs(Server.MapPath("~/") + filename);
}


}

The files get uploaded if its not within datalist by adding PostBackTrigger to UpdatePanel Please help to add post back trigger for the buttin

1

1 Answers

0
votes

In your DataList ItemDataBound event, do this:

protected void dlComments_ItemDataBound(object sender, DataListItemEventArgs e)
{
    FileUpload lFileUpload = (FileUpload)e.Item.FindControl("fileUploadReply");
    PostBackTrigger lTrigger = new PostBackTrigger();
    lTrigger.ControlID = lFileUpload.ID;
    UpdatePanel.Triggers.Add(lTrigger);
}