Update Panel refresh on code behind function call in C# - Ajax

Student picture Student · Nov 5, 2011 · Viewed 7.8k times · Source

I am using Uploadify v2.1.4 to upload images using ASP.Net C# FM 4.0.

In this page i have other controls also, but i want a functionality in such a way that when i upload images it should automatically refresh the UpdatePanel1 to show image uploaded

Default.aspx FILE

<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
   <ContentTemplate>                                 
        <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatDirection="Horizontal" >
            <ItemTemplate>
                <br /><img src='http://test.kashmirsouq.com/ImageUploads/<%# Eval("ImageID") %>' width="100px" height="100px"   vspace="2" hspace="2" border="1" />
                <br /><asp:LinkButton ID="lnkBtnDeleteImage" CommandArgument='<%# Eval("sno") %>' CommandName="Delete" runat="server">
         Delete</asp:LinkButton>
        <br />
            </ItemTemplate>
        </asp:DataList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:SQLConnectionString %>" 
            SelectCommand="SELECT [sno], [ImageID] FROM [User_Images]">
        </asp:SqlDataSource>
 </ContentTemplate>
</asp:UpdatePanel>

Page example is here test.kashmirSouq.com

I am calling FileUplaad.aspx file to upload image using jQuery

<script type="text/javascript">
        $(document).ready(function () {
            $('#fuFiles').uploadify({
                'uploader': 'Scripts/uploadify.swf',
                'script': 'FileUploads.aspx',
                'cancelImg': 'Scripts/cancel.png',
                'auto': 'true',
                'multi': 'true',
                'fileExt': '*.jpg;*.gif;*.png',
                'buttonText': 'Browse...',
                'queueSizeLimit': 5,
                'simUploadLimit': 2
            });
        });

</script>

and in FileUpload.aspx.cs file i save the file on the server and database, I need a way so that i can refresh the updatepanel1 from function saveData() which is in FileUpload.aspx.cs

protected int saveData()
{
            String strSql = "INSERT INTO HMS_User_Images(ImageID,UserID,ImageCreationDate) ";
            strSql += " VALUES ('" + filename + "','123456789', '" + DateTime.Now + "')";
            int result = DataProvider.intConnect_Select(strSql);
}

So when i upload images it should refresh do partial page update of the grid. Please give me a example how i can do it using C#

Please advice how i can do this code sample would be highly appreciated.

Regards

Answer

Mayur Borad picture Mayur Borad · Nov 5, 2011

If You want to refresh the Update panel , Try This...

UpdatePanel1.Update();

If the page is enabled for partial-page rendering, when you invoke the Update method, the UpdatePanel control's content is updated in the browser. Call the Update method if you have server code that must execute to determine whether an UpdatePanel control should be updated. If you plan to use the Update method, set the UpdateMode property to Conditional. If you want the decision to update the panel to be determined in server logic, make sure that the ChildrenAsTriggers property is false and that no explicit triggers are defined for the panel.

In a typical page development scenario, if you define triggers or if the ChildrenAsTriggers property is true for the UpdatePanel control, the Update method is automatically called during the page life cycle.

If the ContentTemplate property is not defined for the UpdatePanel control, no updates of the panel will occur.