2

I am new to programming. I have created a GridView and used SqlDataSource to bind. Grid presents several variables from SQL Server database, including hypertext links.

For a particular field I need to: - evaluate a database field "Journal_title" - insert that into a TemplateField.NavigateUrl as part of a longer string - hide the link if another field ("Indexed_NIH") is NULL

The syntax for the string is correct, and works if I insert a single title, but I need to read all titles from the database and insert them into the URL.

My current code successfully displays the link text in appropriate records (i.e. when "Indexed_NIH != NULL), but the NavigateUrl is not displaying correctly.

Any suggestions welcome - please remember that I am new to this!

<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>

Nothing in codebehind.

Tim Bacon
  • 41
  • 2
  • 4

3 Answers3

1

Good way to do is make use of RowDataBound event of grid control and assign the link to hyperlink button

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
        }
    }
}
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • PLEASE bear with me - I am new to programming. I tried this approach, renaming gv, but VS2010 is not allowing me to add correct control name (lnkPubMed). Is there any simpler way? I am targeting only one template in a gv of 8 total values - all of which are working perfectly. Even the lnkPubMed is displaying correctly, but just will not let me insert variable for Journal_title. – Tim Bacon Jul 12 '12 at 16:53
1

Thanks so much to you both for your help.

In the end I was able to keep my current code, and after much trial and error (!!!) I found that this gives me what I was looking for:

<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>

Seems among my problems were: escaping quotes; correctly adding strings into the URL; and of course inexperience!

Again my sincere thanks!

Tim Bacon
  • 41
  • 2
  • 4
0

You can read everyitem of your GridView in the row databound event. Add a row databound event to your grid view.

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 

 }

}
Waqar Janjua
  • 6,113
  • 2
  • 26
  • 36