我正在使用c#ASP.NET 我有一个类似于下面的GridView
ID | Name | Date | DocID
-----------------------------
1 | John | 27/02/2019 | 1
2 | Mary | 20/01/2019 | 2
3 | Mark | 01/01/2019 | 3
3 | Mark | 01/01/2019 | 4
3 | Mark | 01/01/2019 | 5
DocID是指向上载到数据库的文档的链接。
我已经能够将重复的行合并为1,因此我的表当前如下所示
ID | Name | Date | DocID
-----------------------------
1 | John | 27/02/2019 | 1
2 | Mary | 20/01/2019 | 2
3 | Mark | 01/01/2019 | 3 4 5
除此之外,我现在对所有3个文档都有1个链接,而不是每个文档都有一个链接。
有没有一种方法可以使每个文档都有自己的链接,但仅使用一个LinkButton?或者我缺少某些东西,而链接按钮不是这样做的方法吗?
预先感谢
编辑:这是我针对上下文的一些代码
创建GridView
using (SqlCommand cmd = new SqlCommand("SELECT tblQuote.qID, tblQuote.qInsurer, tblQuote.qDate, tblQuote.qQuote, tblQuote.qTerms, tblQuote.qUser, tblQMethod.qmDesc, " +
"STUFF((SELECT ' ' + CAST(dID AS VARCHAR) FROM tblDocuments WHERE tblDocuments.qID = tblQuote.qID FOR XML PATH('')),1, 0, '') [dID], " +
"STUFF((SELECT ' ' + dName FROM tblDocuments WHERE tblDocuments.qID = tblQuote.qID FOR XML PATH('')),1, 0, '') [dName] " +
"FROM tblQuote INNER JOIN tblQMethod ON tblQuote.qQMethod = tblQMethod.qmID " +
"WHERE tblQuote.rID = " + Session["riskID"], tcf))
{
DataTable dt = new DataTable();
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
gvQuote.DataSource = dt;
gvQuote.DataBind();
}
}
文档链接
protected void lnkDocument_Click(object sender, EventArgs e)
{
int dID = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName, contentType;
using (SqlCommand cmd = new SqlCommand("SELECT * FROM tblDocuments WHERE dID = @dID", tcf))
{
cmd.Parameters.AddWithValue("@dID", dID);
tcf.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["dDocument"];
contentType = sdr["dType"].ToString();
fileName = sdr["dName"].ToString();
}
tcf.Close();
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
GridView TemplateField
<asp:TemplateField HeaderText="Documents Link">
<ItemTemplate>
<asp:LinkButton ID="lnkDocument" runat="server" Text='<%# Bind("dID") %>' ToolTip='<%# Bind("dName") %>' CommandArgument='<%# Eval("dID") %>' OnClick="lnkDocument_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
中继器代码 通过在RowDataBound下添加以下代码,我已经能够使链接单独显示,但是现在所有文档都显示在DocID中
if (e.Row.RowType == DataControlRowType.DataRow)
{
Repeater rp = (Repeater)e.Row.FindControl("rpDocument");
using (SqlCommand cmd = new SqlCommand("SELECT dID, dName FROM tblDocuments", tcf))
{
DataTable dt = new DataTable();
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
rp.DataSource = dt;
rp.DataBind();
}
}
}
答案 0 :(得分:0)
您可以修改在gridview列模板中生成的标记。
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="ltMarkup" runat="server" Text='<%# Eval("DocID") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
然后管理在gridview的rowdatabound事件中生成的链接:
protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
string[] docs = e.Row.Cells[3].Text.Split(' ');
var lt = (Literal)e.Row.Cells[3].FindControl("ltMarkup");
foreach(var d in docs)
lt.Text += String.Format("<a href='{0}'>Link {0}</a><br/>", d);
}
}
HTH