我是编程新手。我创建了一个GridView并使用SqlDataSource进行绑定。 Grid从SQL Server数据库中提供了几个变量,包括超文本链接。
对于特定领域,我需要: - 评估数据库字段" Journal_title" - 将其作为较长字符串的一部分插入到TemplateField.NavigateUrl中 - 如果另一个字段(" Indexed_NIH")为NULL
,则隐藏链接字符串的语法是正确的,如果我插入单个标题,则有效,但我需要从数据库中读取所有标题并将其插入到URL中。
我当前的代码成功地在适当的记录中显示链接文本(即" Indexed_NIH!= NULL),但NavigateUrl未正确显示。
欢迎任何建议 - 请记住我是新手!
<asp:TemplateField HeaderText="PubMed">
<ItemTemplate>
<asp:HyperLink ID="lnkPubMed" runat="server" Text="S" Target="_blank" NavigateUrl='http://www.ncbi.nlm.nih.gov/pubmed?term="<%# Eval("Journal_title") %>"[Journal]) AND ("last 3 years" [PDat])"' Visible='<%# Convert.ToString(Eval("Indexed_NIH")) == "True" ? true : false %>' >
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
代码隐藏中没有任何内容。
答案 0 :(得分:1)
好的方法是使用RowDataBound
网格控件事件并将链接分配给超链接按钮
protected void Gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
object[] dataitems = ((DataRowView)e.Row.DataItem).Row.ItemArray;
HyperLink hl = (HyperLink)e.Row.FindControl(ControlName);
if(hl!=null)
{
//write code to assing value to link
}
}
}
答案 1 :(得分:1)
非常感谢你们的帮助。
最后我能够保留当前的代码,经过多次试验和错误(!!!)我发现这给了我想要的东西:
<asp:TemplateField HeaderText="PubMed">
<ItemTemplate>
<asp:HyperLink ID="lnkPubMed" runat="server" Text="S" Target="_blank" NavigateUrl='<%# "http://www.ncbi.nlm.nih.gov/pubmed?term=" + (Eval("Journal_title")) + "[Journal] (\"Last 3 years\"[PDat])" %>' Visible='<%# Convert.ToString(Eval("Indexed_NIH")) == "True" ? true : false %>' >
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
我的问题似乎是:逃避报价;正确地将字符串添加到URL中;当然缺乏经验!
再次衷心的感谢!
答案 2 :(得分:0)
您可以在行数据绑定事件中阅读GridView的每个项目。 将行数据绑定事件添加到网格视图。
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//Here in cell specify the index of link button
string text = e.Row.Cells[0].Text
}
}