【发布时间】:2018-01-18 06:09:54
【问题描述】:
我正在制作我的 SQL Server 表记录的 Excel 报告。在 Asp 页面网格视图中,我正在获取所需的数据,然后将这些数据下载到 excel 中。一列有超链接值,我需要这个超链接应该只在 Asp 页面网格视图中工作,但下载后,它应该重定向到一个新页面,其中将显示未经授权的访问错误。我没有得到如何在 Excel 文件超链接单击中显示未经授权的错误链接。
这是我的代码
protected void LnkBtnViewImage_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
LinkButton lnkbtimage = sender as LinkButton;
GridViewRow gvrowreport = lnkbtimage.NamingContainer as GridViewRow;
//int Exhid = Convert.ToInt32(gvrowreport.Cells[1].Text);
string Exhid = ((HiddenField)gvrowreport.Cells[0].FindControl("HiddenField1")).Value;
SqlCommand cmd = new SqlCommand("select ImageName,ImageData from CompanyImage where Edition_Id='" + Session["Edition_ID"].ToString() + "' and Exhibitor_ID=@Exhibitor_ID ", con);
cmd.Parameters.AddWithValue("@Exhibitor_ID", Exhid);
//Select Statement con
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlDataReader dr = cmd.ExecuteReader();
if (dr!=null)
{
dr.Read();
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
//string filePath = GridViewLogo.DataKeys[gvrow.RowIndex].Value.ToString();
//if (!Convert.IsDBNull(dr["ImageData"]))
//{
Response.ContentType = "application/vnd.ms-jpg";
//to open file prompt Box open or Save file
Response.AddHeader("content-disposition", "attachment;filename=" + dr["ImageName"].ToString());
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite((byte[])dr["ImageData"]);
Response.End();
//}
//else
//{
// //lblhid.Text = "Image is not uploaded here !!";
// //lblhid.ForeColor = Color.Green;
// //lblhid.Visible = true;
// //// lblhexcelerror.Visible = false;
// //gvrow.Visible = false;
//}
}
else
{
//LinkButton lnkbtn = sender as LinkButton;
//GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
//gvrow.Visible = false;
}
con.Close();
【问题讨论】: