当我点击此列名称" DisplayName"我将帮助从GridHyperLinkColumn打开存储在数据库中的pdf文件。我希望它使用target = _new在浏览器上打开我的pdf文件。现在我能够使用verbinary(MAX)将文件存储在我的MSSQL表中,并在新浏览器中打开该链接但不访问pdf文件。我的asp.net表单源文件如下所示:
<ClientSettings AllowDragToGroup="True">
<Selecting AllowRowSelect="True" />
</ClientSettings>
<MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSourceGridArchive">
<Columns>
<telerik:GridHyperLinkColumn DataTextField="DisplayName" FilterControlAltText="Filter DisplayName column" HeaderText="DisplayName" DataNavigateUrlFormatString="DisplayName" DataNavigateUrlFields="DisplayName" SortExpression="DisplayName" Target="_new" UniqueName="DisplayName">
</telerik:GridHyperLinkColumn>
<telerik:GridBoundColumn DataField="doc_number" FilterControlAltText="Filter doc_number column" HeaderText="doc_number" SortExpression="doc_number" UniqueName="doc_number">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="doc_classification" FilterControlAltText="Filter doc_classification column" HeaderText="doc_classification" SortExpression="doc_classification" UniqueName="doc_classification">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="doc_date" DataType="System.DateTime" FilterControlAltText="Filter doc_date column" HeaderText="doc_date" SortExpression="doc_date" UniqueName="doc_date">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSourceGridArchive" runat="server" ConnectionString="<%$ ConnectionStrings:pro_dbConnectionString %>" SelectCommand="SELECT [DisplayName], [doc_number], [doc_classification], [doc_date] FROM [Document]"></asp:SqlDataSource>
.cs源码也如下所示: public partial class Archives:Page { protected void Page_Load(object sender,EventArgs e) { RadGrid1.ItemCommand + = new GridCommandEventHandler(RadGrid1_ItemCommand);
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
//your code
string sConn = System.Configuration.ConfigurationManager.ConnectionStrings["pro_dbConnectionString"].ToString();
SqlConnection objConn = new SqlConnection(sConn);
objConn.Open();
string sTSQL = "select * from Document";
SqlCommand objCmd = new SqlCommand(sTSQL, objConn);
objCmd.CommandType = CommandType.Text;
SqlDataAdapter ada = new SqlDataAdapter(objCmd);
DataTable dt = new DataTable();
ada.Fill(dt);
objConn.Close();
objCmd.Dispose();
//Bind the Data into the html anchor tag which will call the handler with ID
if (dt.Rows.Count > 0)
{
string tbl = "";
for (int i = 0; i < dt.Rows.Count; i++)
{
tbl += @"<li>
<a target='_blank' href='DocHandler.ashx?ID=" + dt.Rows[i]["SNo"].ToString();
tbl += @"' title='";
tbl += @"' >" + dt.Rows[i]["DisplayName"].ToString();
tbl += @"</a>
</li>";
}
Response.Write(tbl);
}
}
}
然后我创建了一个扩展名为.ashx.cs的句柄来处理我的文件流,如下所示:
公共类DocHandler:IHttpHandler {
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string id = context.Request.QueryString["ID"].ToString();
string sConn = System.Configuration.ConfigurationManager.ConnectionStrings["pro_dbConnectionString"].ToString();
SqlConnection objConn = new SqlConnection(sConn);
objConn.Open();
string sTSQL = "select Name_File,Extension,ContentType,FileData,FileSize from Documente where SNo=@ID";
SqlCommand objCmd = new SqlCommand(sTSQL, objConn);
objCmd.CommandType = CommandType.Text;
objCmd.Parameters.AddWithValue("@ID", id);
SqlDataAdapter ada = new SqlDataAdapter(objCmd);
DataTable file = new DataTable();
ada.Fill(file);
objConn.Close();
objCmd.Dispose();
if (file.Rows.Count > 0)
{
DataRow row = file.Rows[0];
string name = (string)row["Name_File"];
string contentType = (string)row["ContentType"];
Byte[] data = (Byte[])row["FileData"];
int FileSize = Convert.ToInt32(row["FileSize"].ToString());
// Send the file to the browser
context.Response.AddHeader("Content-type", contentType);
context.Response.AddHeader("Content- Disposition", "attachment; filename=" + name);
context.Response.OutputStream.Write(data, 0, FileSize);
context.Response.Flush();
context.Response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
但是我无法在我的radgrid表上访问它,但是我可以在一个简单的webform上填充它,请在这方面需要帮助,这样当我点击超链接时它会加载我的pdf文件
由于
答案 0 :(得分:0)
如果要修改网格列中的控件,请使用ItemDataBound事件。另外,请参阅此处以获取有关如何访问具体单元格/列的示例:http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows - 您将看到需要更改控件属性而不使用Response.Write()。
然后,使用应该使用的格式字符串,请参阅此处http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/columns/column-types#gridhyperlinkcolumn
如果每个网格行需要多个链接,则应使用模板列并以与当前方法类似的方式创建列表,但ItemCommand中的代码应位于ITemplate类(InstantiateIn方法)中,你应该再次在容器中创建控件/设置HTML字符串,而不是用户Response.Write。