Tim Bacon
Tim Bacon

Reputation: 41

Reading variable into NavigateUrl in TemplateField in GridView asp.net 4 c#

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.

Upvotes: 2

Views: 4295

Answers (3)

Tim Bacon
Tim Bacon

Reputation: 41

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!

Upvotes: 1

Waqar Janjua
Waqar Janjua

Reputation: 6123

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 

 }

}

Upvotes: 0

Pranay Rana
Pranay Rana

Reputation: 176906

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

Upvotes: 1

Related Questions