Showing posts with label Response. Show all posts
Showing posts with label Response. Show all posts

Tuesday, October 9, 2012

How to refresh page automatically? - 4 different ways


Some of the sites those work on the basis of updated data like - Cricket score updates, Share market data, For those kind of sites it is required that to update or refresh the page automatically. In this article, I am  explaining - How to refresh page automatically?

Here I am showing 4 different ways to update or refresh the page automatically. This can be done using following ways-

1. Client side code
2. Server side code

Client side code-

In client side we have following options for this-

A. Using meta tag-

By setting the attributes value in meta tag you can auto refresh the page.

Like this-

<head runat="server">
 <meta  http-equiv="REFRESH" content="1">
</head>
 

B. Using reload() -

reload() function refresh the page explicitly, So use this function inside  setTimeout() function and give interval for setTimeout().

Like this-

<script type="text/javascript">
        function RefreshPage(time) {
            setTimeout("location.reload(true);", time);
        }
</script>


Now call this function on body load() event.

<body onload="RefreshPage(1000);">


Here 1000 is time to refresh the page after particular interval, you can change this time.

Server side code-

In server side we have following options for this-

A. Using Response.AddHeader()

you can set value for refreshing the page in Page_Load() event.

Like this-

protected void Page_Load(object sender, EventArgs e)
{
   Response.AddHeader("Refresh", "1000");
}


B. Using Timer control.

By setting the Interval and Enabled property you can refresh the page.

Like this-

first set Enabled property of Timer control= true
now set Interval property of Timer control= 1000 (your desired value)

last  you need to implement Tick() event for Timer Control-

protected void TimerControl1_Tick(object sender, EventArgs e)
{
    //Your code
}



Now done, these are some possible ways for auto refreshing the page; you can use any of these.



Thanks


Monday, June 11, 2012

Downloading file using GridView in Asp.Net.



In some of the cases we want to give file listing with download option. Here I am explaining, How to download file using GridView in Asp.Net?

For this task, I have created my Database table like this-
FileDetails(FileId,FileName,FilePath)


Here-

FileId -  Auto generated filed, to maintain primary key for file
FileName - Name of Uploaded file
FilePath - Complete file path for uploaded file
 

In this example, I am designing GridView with three columns, Here FileId and FilePath will be invisible mode, FileName will be displayed to the user. based on user action FileId and FilePath will be fetched from GridView and these values will be used for further processing.  
 

.aspx code-

 <asp:GridView ID="grdFileDetails" runat="server" AutoGenerateColumns="false" OnRowCommand="grdFileDetails_RowCommand">
        <EmptyDataTemplate>
            <div style="color: Red; text-align: center; width: 700px">
                No Data Found.
            </div>
        </EmptyDataTemplate>
        <Columns>
            <asp:TemplateField HeaderText="File ID" ItemStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <asp:Label ID="lblFileId" runat="server" Text='<%# Eval("FileId") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="File Name" ItemStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <asp:Label ID="lblFileName" runat="server" Text='<%# Eval("FileName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="File Path" Visible="false">
                <ItemTemplate>
                    <asp:Label ID="lblFilePath" runat="server" Text='<%# Eval("FilePath") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Download">
                <ItemTemplate>
                    <asp:Button ID="btnDownLoad" runat="server" Text="Download" CommandName="download"  CommandArgument='<%# Eval("FilePath") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

 
In above code, I have taken 4 columns in GridView, but here File ID and File Path is hidden, I am not showing File Path and File ID. I am passing FilePath value using CommandArgument property, this value will be used in server side code to download file. For button I have set CommandName= "download" so that button click can be handled in RowCommand() event of GridView.

        <ItemTemplate>
            <asp:Button ID="btnDownLoad" runat="server" Text="Download" CommandName="download"  CommandArgument='<%# Eval("FilePath") %>' />
        </ItemTemplate>

 

The output of this .aspx will be -

Downloading file using GridView.

Now .cs code-

 protected void grdFileDetails_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                //Code to download file
                if (e.CommandName == "download")
                {
                    string filepath = Convert.ToString(e.CommandArgument);

                    byte[] data = System.IO.File.ReadAllBytes(filepath);
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.AddHeader("Content-Type", "Application/octet-stream");
                    Response.AddHeader("Content-Length", data.Length.ToString());
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + filepath);
                    Response.BinaryWrite(data);
                    Response.End();
                }

            }

            catch (Exception)
            {
                 throw;
            }
        }


In above code, I have implemented GridVIew_RowCommand() event , here first I am checking commandName value using e.CommandName.

To get particular file path here I am using e.CommandArgument like this-

   string filepath = Convert.ToString(e.CommandArgument);

 After getting File path I have used Response object to download file like this-
 
   Response.BinaryWrite(data);
 
Here data will contain binary data that will be downloaded as a file. Before download the file it is must that we have to set some settings using Response.AddHeader() method. Here we have to set Content-Type and  Content-Length for downloading object.  Now if you will click on download button then related file will be downloaded.

Thanks