Hyperlink column in a GridView

G

Guest

Hi

I have a GridView that is displaying master records. Some of these records
have child records.

I would like to a column to my master GridView such that for each master
record that has detail records, it displays a hyperlink that then takes the
user to the details page. If a master record doesn't have any children, I
just want it to display its ID with no hyperlink.

In a nutshell: the cell must only be clickable if there are detail records
associated with that row.

What is the best way of doing this please?
 
G

Guest

Hi,

One approach is create a column for hyperlink and in HyperLink attribute of
Enable call the function which will return true or false based on if detail
records are there are not.

It will be something like:

<asp:GridView>
..
..
..
<columns>
<asp:HyperLink runat="server" Enabled='<% IsDetailsRecord %>'
</asp:GridView>

Code Behind Page

protected bool IsDetailsRecord ()
{

//your logic
return;
}
 
G

Guest

Hi Raj

Thanks for your reply. I am afraid I need to leave now so I haven't been
able to try your suggestion.

Would your way still show the ID underlined? The requirement is that the ID
should only be underlined if the record has child data.
 
G

Guest

Hi

This is how I fixed the problem:

In the aspx, I added an ItemTemplate column that calls my code behind
passing in important property values of the underlyinf record that is being
bound to (such as the primary key which is called ID):

<asp:TemplateField HeaderText="Transaction" SortExpression="FunctionCategory">
<ItemStyle Width="20%" />
<ItemTemplate>
<%# this.GetTransactionColumnHTML( (int)Eval("ID"),
(string)Eval("FunctionCategory"), (int)Eval("DetailsCount") )%>
</ItemTemplate>
</asp:TemplateField>

The code behind then does:

/// <summary>
/// Returns an HTML fragment to be used for the Transaction column
in the grid.
/// If the master record has details records an anchor with a link
to the details page is returned;
/// otherwise, a label with the transaction name is returned.
/// </summary>
/// <param name="pk">Primary key of of the master audit record being
rendered</param>
/// <param name="functionCategory">The transaction type e.g.
Login</param>
/// <param name="detailsCount">The number of details records
associated witht the master audit record</param>
/// <returns>HTML fragement</returns>
public string GetTransactionColumnHTML(int pk, string
functionCategory, int detailsCount)
{
string s = string.Empty;

if (detailsCount > 0)
{
// Master record has details records, so return a hyperlink
to the AuditDetails.aspx
s = "<a href='AuditDetails.aspx?MasterID=" +
Convert.ToString(pk) + "'>" + functionCategory + "</a>";
}
else
{
// Master record doesn't have any children so just return a
label.
s = "<label>" + functionCategory + "</label>";
}

return s;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top