我有一个fileUpload控件和位于UpdatePanel内的datalist中的Button,用户需要使用fileUpload控件和按钮上传文件。如何为按钮添加回发触发器,以便通过fileupload将所选文件上传到服务器。
<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>
在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);
}
}
如果文件不在datalist中,则通过将PostBackTrigger添加到UpdatePanel来上传文件 请帮助为buttin添加回发触发器
答案 0 :(得分:0)
在您的DataList ItemDataBound
事件中,执行以下操作:
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);
}