在updatepanel asp.net c#中的AsyncFileUpload中上传文件后显示gridview
作者:互联网
我想在updatepanel中使用AsyncFileUpload
上传文件后,我想在gridview中显示上传的文件.
我使用这种方法,但上传后我看不到gridview.请帮我看看我的gridview.
我的脚本是:
<script type="text/javascript" language="javascript">
var ifIgnoreError = false;
function uploadError(sender, args) {
if (ifIgnoreError) {
$get("<%=lblStatus.ClientID%>").style.color = "red";
$get("<%=lblStatus.ClientID%>").innerHTML = " File not permitted ";
alert("Allowed file is .jpg ,.jpeg,.png,.gif ");
}
else {
alert(args.get_message());
}
}
function StartUpload(sender, args) {
var fileName = args.get_fileName();
var fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1);
if (fileExtension == 'jpg' || fileExtension == 'jpeg' || fileExtension == 'png' || fileExtension == 'gif') {
$get("<%=lblStatus.ClientID%>").style.color = "green";
$get("<%=lblStatus.ClientID%>").innerHTML = "Uploading...";
}
else {
ifIgnoreError = true;
sender._stopLoad();
}
}
function UploadComplete(sender, args) {
$get("<%=lblStatus.ClientID%>").style.color = "#33CCFF";
$get("<%=lblStatus.ClientID%>").innerHTML = "The file was uploaded successfully";
//If this code is uncomment, I can't see gridview
var btnClick = document.getElementById("btnClick");
btnClick.click();
//If this code uncomment, file uploaded twice
// __doPostBack('UpdatePanel1', '');
}
</script>
和HTML是:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnClick" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" ClientIDMode="AutoID"
OnClientUploadComplete="UploadComplete" OnClientUploadError="uploadError"
OnClientUploadStarted="StartUpload" ErrorBackColor="#66CCFF" onuploadedcomplete="AsyncFileUpload1_UploadedComplete" ThrobberID="Throbber"
UploaderStyle="Traditional" UploadingBackColor="#66CCFF" Width="400px" />
<asp:Button runat="server" ID="btnClick" Text="Update grid" style="display:none"/>
<asp:Label ID="Throbber" runat="server">
<img src="../../images/Pages/loading19.gif" align="absmiddle" alt="loading" />
</asp:Label>
<asp:Label ID="lblStatus" runat="server" Style="font-family: tahoma; font-size: 11px;"></asp:Label>
<asp:GridView ID="GrdMadarek" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="ImgMadarek" runat="server" Width="50px" Height="40px" ImageUrl='<%#Eval("MadrakFileName","~/files/{0}")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkBtnDelMadrek" runat="server">delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
和aspx.cs中的代码是:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params.Get("__EVENTTARGET") == "UploadPostback")
{
}
else
{
if (AsyncFileUpload1.HasFile)
{
string FinalFileName = Path.GetExtension(AsyncFileUpload1.FileName);
string strPath = MapPath("~/files/") + FinalFileName;
AsyncFileUpload1.SaveAs(strPath);
if (Session["MadarekList"] != null)
{
var MadarekList = (List<MadrakDetails>)Session["MadarekList"];
MadrakDetails Madrak = new MadrakDetails();
Madrak.MadrakFileName = FinalFileName;
MadarekList.Add(Madrak);
Session["MadarekList"] = MadarekList;
GrdMadarek.DataSource = MadarekList;
GrdMadarek.DataBind();
}
else
{
var MadarekList = new List<MadrakDetails>();
MadrakDetails Madrak = new MadrakDetails();
Madrak.MadrakFileName = FinalFileName;
MadarekList.Add(Madrak);
Session["MadarekList"] = MadarekList;
GrdMadarek.DataSource = MadarekList;
GrdMadarek.DataBind();
}
}
}
}
public class MadrakDetails
{
string madrakFileName = string.Empty;
public string MadrakFileName
{
get { return madrakFileName; }
set { madrakFileName = value; }
}
}
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
// no code here
}
解决方法:
在绑定GridView的每一行之后添加以下行[GrdMadarek.DataBind()]
UpdatePanel1.Update();
另外,尝试使用AsyncFileUpload控件的onuploadedcomplete事件来保存文件.
protected void AsyncFileUpload1_UploadedComplete
(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
if (AsyncFileUpload1.HasFile)
{
string strPath = MapPath("~/UploaddedFiles/") + Path.GetFileName(e.filename);
AsyncFileUpload1.SaveAs(strPath);
...
...
GrdMadarek.DataBind();
UpdatePanel1.Update();
}
}
标签:c,asp-net,asyncfileupload 来源: https://codeday.me/bug/20190709/1408246.html