Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using AjaxToolKit for uploading files. The uploaded files are saved in a folder on server and also included in an Sql server database. I have a gridview to show the files uploaded. My code in .aspx page is:
<div>
        <asp:ToolkitScriptManager ID="ScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
       
        <asp:AjaxFileUpload ID="AjaxFileUpload1" runat="server" OnUploadComplete="AjaxFileUpload1_UploadComplete"  AllowedFileTypes="jpg,bmp,png,docx,txt" MaximumNumberOfFiles="10" ThrobberID="Loader" />
        <asp:Image ID="Loader" runat="server" ImageUrl="~/Loading.gif" CssClass="nodisplay" />
    </div>
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Connectionstring %>" SelectCommand="SELECT * FROM [Images]"></asp:SqlDataSource>
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
                    <asp:TemplateField HeaderText="imagename" SortExpression="imagename">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("imagename") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <img src='<%# "/Album/"+Eval("imagename") %>'/ >
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        
           </div>


and in codebehind i have
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {
        string filename = e.FileName.ToString();
        string filepath = "/Album/" + filename;
        string fullpath = Server.MapPath("~" + filepath);
        AjaxFileUpload1.SaveAs(fullpath);

        string cstr = ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;
        using(SqlConnection con = new SqlConnection(cstr))
        {
            con.Open();
            SqlCommand com = new SqlCommand("INSERT INTO Images (imagename) VALUES (@imagename)", con);
            com.Parameters.AddWithValue("@imagename", filename);
            com.ExecuteNonQuery();
            con.Close();
            GridView1.DataBind();
                    }
      //Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
 
    }


The program works fine except that databiding for gridview does not work. After every upload user has to refresh the page to see the gridview latest position.

I have tried using refreshing the page from code behind but it does no appear to work. (Please see comment-out portion in code behind above)

Is there any way i can databind the gridview or show updated gridview after each upload as we can do in fileupload in ASP.NET without using AjaxControlToolKit.

Thanks for help.
Posted

1 solution

I'm afraid you can not refresh gridview in that manner. You have to use javascript function to bind gridview again after uploading in ajax file upload.
You can do one of the following:
1) simply use OnClientUploadComplete="ShowUpdate()" in ajax file upload control and use
XML
<script type="text/javascript">
        function ShowUpdate() {
            __doPostBack();
        }
</script>

as its javascript function.
2) you can put the gridview inside update panel and then postback the update panel using javascript function OnClientUploadComplete of ajax file upload

You may refer below links to get your job done:
AjaxFileUpload+to+refresh+GridView+after+upload+complete[^]
reload-gridview-or-repeater-on-ajaxfileupload-uploadcomplete-event-in-asp-net[^]

Hope it Helps.. Happy Coding! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900