Refresh Page when Download Button is clicked in ASP.Net

Last Reply on Sep 28, 2015 10:14 AM By Shashikant

Posted on Sep 24, 2015 03:17 AM
I have gv as follows:
code page aspx:
 
 <asp:GridView ID="grvDanhSach" AutoGenerateColumns="False" SkinID="SkinDefault" OnRowDataBound="grvDanhSach_RowDataBound"
                                BorderStyle="None" BorderWidth="1px" Width="100%" runat="server">
                                <Columns>
                                    <asp:TemplateField HeaderText="idCV" SortExpression="idCV" Visible="False">
                                        <ItemTemplate>
                                            <asp:Label ID="lblidCV" runat="server" Text='<%# Bind("idCV") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Số công văn" SortExpression="SoCV">
                                        <ItemTemplate>
                                            <asp:LinkButton ID="lbtSoCV" Text='<%# Bind("SoCV") %>' 
                                                CommandArgument='<%#Eval("FileToanVan")%>' OnClick="OpenPDF" runat="server" />
                                        </ItemTemplate>
                                        <HeaderStyle Width="150px" />
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="RowNum" HeaderText="RowNum" SortExpression="RowNum" Visible="false" />
                                    <asp:BoundField DataField="FileToanVan" HeaderText="FileToanVan" SortExpression="FileToanVan"
                                        Visible="false" />
                                    <asp:BoundField DataField="TrichYeu" HeaderText="Trích yếu" SortExpression="TrichYeu" />
                                    <asp:BoundField DataField="DonViThucHien" HeaderStyle-Width="200px" HeaderText="Đơn vị thực hiện"
                                        SortExpression="DonViThucHien" />
                                    <asp:BoundField DataField="NgayNhan" HeaderText="Ngày nhận" SortExpression="NgayNhan"
                                        DataFormatString="{0:dd/MM/yyyy}">
                                        <HeaderStyle Width="80px" />
                                    </asp:BoundField>
                                    <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Đánh dấu">
                                        <ItemTemplate>
                                            <asp:LinkButton ID="lbtMark" runat="server"><img src="../images/mark-task.png" alt="" style="border:0;" /></asp:LinkButton>
                                        </ItemTemplate>
                                        <HeaderStyle Width="70px" />
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:TemplateField>
                                    <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Giao việc">
                                        <ItemTemplate>
                                            <asp:LinkButton ID="lbtGiaoViec" runat="server" OnClick="lbtGiaoViec_Click"><img src="../images/change_process.png" alt="" style="border:0;" /></asp:LinkButton>
                                        </ItemTemplate>
                                        <HeaderStyle Width="60px" />
                                        <ItemStyle HorizontalAlign="Center" />
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="TrangThai" SortExpression="TrangThai" Visible="False">
                                        <ItemTemplate>
                                            <asp:Label ID="lblTrangThai" runat="server" Text='<%# Bind("TrangThai") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>

code c#:

protected void grvDanhSach_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Services.cTB_CongVan mCongVan = new Services.cTB_CongVan();
                Services.cTB_User mUser = new Services.cTB_User();
                string strSoCV = "", strKhoangTrang = "";
                strKhoangTrang = "&nbsp;&nbsp;";
                DataSet ds = mCongVan.CongVan_SelectbyUserName(hidUserID.Value, "", "", cboLoaiCV.SelectedValue, "", "", currPage, 10, 5);
                DataTable dtb = ds.Tables[0];
                Label lblTrangThai = (Label)e.Row.FindControl("lblTrangThai");
                LinkButton lbtSoCV = (LinkButton)e.Row.FindControl("lbtSoCV");
                Label lblidCV=(Label)e.Row.FindControl("lblidCV");
                if (mCongVan.CongVan_DocHayChua(hidUserID.Value, int.Parse("0" + lblidCV.Text)) == 1)
                {
                    strSoCV = lbtSoCV.Text + strKhoangTrang;
                    lbtSoCV.Text = strSoCV;
                }
                else
                {
                    strSoCV = lbtSoCV.Text + strKhoangTrang + "<img src=\"../images/new.gif\" border=\"0\" /> ";
                    lbtSoCV.Text = strSoCV;
                }
                dtb.Dispose();
                mUser = null;
                mCongVan = null;
            }
        }
        catch
        { }
    }

 

protected void OpenPDF(object sender, EventArgs e)
    {
        Services.cTB_CongVan mCongVan = new Services.cTB_CongVan();
        GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
        Label lblidCV = (Label)clickedRow.FindControl("lblidCV");
        LinkButton lnkView = sender as LinkButton;
        string urlName = Request.Url.AbsoluteUri;
        // Removing the Page Name
        urlName = urlName.Remove(urlName.LastIndexOf("/")-5);
        //Adding FolderName and FileName in the URL
        string url = string.Format("{0}/Store/{1}", urlName, lnkView.CommandArgument.ToString());
        string script = "<script type='text/javascript'>window.open('" + url + "')</script>";
        this.ClientScript.RegisterStartupScript(this.GetType(), "script", script);
      
        mCongVan.UserDocCV_Edit(1, hidUserID.Value, int.Parse("0" + lblidCV.Text), 1);
        /*
        Services.cTB_CongVan mCongVan = new Services.cTB_CongVan();
        GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
        Label lblidCV = (Label)clickedRow.FindControl("lblidCV");
        mCongVan.UserDocCV_Edit(1, hidUserID.Value, int.Parse("0" + lblidCV.Text), 1);
        
        LinkButton lnk = (LinkButton)sender;
        if (lnk != null)
        {
            Response.AddHeader("content-disposition", "attachment; filename=" + lnk.CommandArgument);
            Response.WriteFile(Server.MapPath("~/Store/" + lnk.CommandArgument.ToString()));
            Response.End();
        }
        */
        
        mCongVan = null;
        
    }

I use grvDanhSach_RowDataBound() to load data and insert image "New" for data row that have not been read.

When click the data row, i use OpenPDF() to open pdf content and remove the image "New", but the image doesn't remove until I reload page.

I want a solution that remove image "New" when i click the data using funtion OpenPDF().

Thanks.

Posted on Sep 24, 2015 07:21 AM

Hi haocaocu,

Please add the below line of code at the end of OpenPDF method so that it automatically redirect you.

Code

Response.Redirect(Request.Url.AbsoluteUri);

 


Posted on Sep 24, 2015 06:52 PM

Hi dharmendr,

I've inserted into the line of code that it worked, but not open the pdf file. You help me with. Thank you


Posted on Sep 27, 2015 10:28 PM

Hi dharmendr

I insert this code into methods OpenPDF end, it worked ok (new image disappears) but does not open the pdf file. You have no other way to help me with. Thank you!


Posted on Sep 28, 2015 02:07 AM

Hi haocaocu,

I will get back to you.


Posted on Sep 28, 2015 10:14 AM Modified on on Sep 29, 2015 04:39 AM

Here I have created sample that will help you out.

HTML

<div>
    <script type="text/javascript">
        function Redirect() {
            setTimeout(function () {
                window.location.reload(1);
            }, 5000);
        }
    </script>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EmptyDataText="No files uploaded"
        OnRowDataBound="RowDataBound">
        <Columns>
            <asp:TemplateField HeaderText="File">
                <ItemTemplate>
                    <asp:Label ID="lblFileName" Text='<%#Eval("FileName") %>' runat="server" />
                    <asp:HiddenField ID="hfViewed" Value='<%#Eval("View") %>' runat="server" />
                    <asp:HiddenField ID="hfId" Value='<%#Eval("Id") %>' runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Download">
                <ItemTemplate>
                    <asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("FilePath") %>'
                        runat="server" OnClick="DownloadFile" OnClientClick="Redirect()"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>

Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGird();
    }
}

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ((Label)e.Row.FindControl("lblFileName")).Text += Convert.ToBoolean(((HiddenField)e.Row.FindControl("hfViewed")).Value) ? "**" : "";
    }
}

protected void DownloadFile(object sender, EventArgs e)
{
    string filePath = (sender as LinkButton).CommandArgument;
    GridViewRow row = (GridViewRow)(sender as LinkButton).NamingContainer;
    int fileId = Convert.ToInt32(((HiddenField)row.FindControl("hfId")).Value);
    UpdateFileView(fileId, true);
    Response.ContentType = ContentType;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
    Response.WriteFile(filePath);
    Response.End();
}

private void BindGird()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM  [FileViewTable]"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}

private void UpdateFileView(int fileId, bool IsViewed)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("UPDATE FileViewTable SET [View] = @View WHERE [Id] = @Id"))
        {
            cmd.Parameters.AddWithValue("@Id", fileId);
            cmd.Parameters.AddWithValue("@View", IsViewed);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}

Screenshot