display gridview after upload file in AsyncFileUpload in updatepanel asp.net c#

有些话、适合烂在心里 提交于 2019-12-10 16:48:46

问题


I want use AsyncFileUpload in updatepanel

after uploaded file, I want display uploaded file in gridview.

I use this method, but after upload I can't see gridview. Please help me to see my gridview.

my scripts is:

<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>

and html is :

<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>

and code in aspx.cs is :

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
    }

回答1:


Add the below line AFTER every line where you bind your GridView [GrdMadarek.DataBind()]

UpdatePanel1.Update();

Also, try using the event onuploadedcomplete of AsyncFileUpload control to save your files.

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();

  }
}

Refer the article here.



来源:https://stackoverflow.com/questions/18682909/display-gridview-after-upload-file-in-asyncfileupload-in-updatepanel-asp-net-c-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!