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.

1
  • dont forget to mark it as accepted if you got the info you want... Commented Jul 12, 2012 at 15:55

3 Answers 3

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
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

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 

 }

}

1 Comment

Thanks so much to you both for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.