Hyperlink template column and change link text based on Condition

P

Paul D. Fox

How do I create a Hyperlink column in a Datagrid which launches a pop-up window, and have the text in the hyperlink change based on a condition? I'm trying to do something like the following:

<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<a href="Javascript://" onClick="_openApproval(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);" >
<asp:HyperLink ID="lnkApproval"></asp:HyperLink>
</a>
</ItemTemplate>
</asp:TemplateColumn>

And I suppose I code something in the ItemDataBound to change the Hyperlink's Text? But How?

Paul
 
K

Kim Quigley

Paul,

There are a number of ways you can accomplish what you are asking, but the best solution depends on what part of the link you're trying to change and what the conditions are. If your onClick code isn't going to change and you only want to modify the link text, then this is what I recommend putting in the ItemDataBound method:

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
If something then
CType(e.Item.Cells(YourColumnID).FindControl("LinkLabel"), Label).Text = 'something
End If
End If

Your column would need to look like this

<asp:TemplateColumn>
<ItemTemplate>
<a href="Javascript://" onClick='alert(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);'><asp:Label id=LinkLabel runat="server" text="Unchanged Text"></asp:Label></a>
</ItemTemplate>
</asp:TemplateColumn>

How do I create a Hyperlink column in a Datagrid which launches a pop-up window, and have the text in the hyperlink change based on a condition? I'm trying to do something like the following:

<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<a href="Javascript://" onClick="_openApproval(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);" >
<asp:HyperLink ID="lnkApproval"></asp:HyperLink>
</a>
</ItemTemplate>
</asp:TemplateColumn>

And I suppose I code something in the ItemDataBound to change the Hyperlink's Text? But How?

Paul
 
P

Paul D. Fox

Thanks Kim, that was a big help however, I had to resort to:
"If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then"

In order for it to properly execute. I don't understand why your IF statement failed.

Paul
Paul,

There are a number of ways you can accomplish what you are asking, but the best solution depends on what part of the link you're trying to change and what the conditions are. If your onClick code isn't going to change and you only want to modify the link text, then this is what I recommend putting in the ItemDataBound method:

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
If something then
CType(e.Item.Cells(YourColumnID).FindControl("LinkLabel"), Label).Text = 'something
End If
End If

Your column would need to look like this

<asp:TemplateColumn>
<ItemTemplate>
<a href="Javascript://" onClick='alert(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);'><asp:Label id=LinkLabel runat="server" text="Unchanged Text"></asp:Label></a>
</ItemTemplate>
</asp:TemplateColumn>

How do I create a Hyperlink column in a Datagrid which launches a pop-up window, and have the text in the hyperlink change based on a condition? I'm trying to do something like the following:

<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<a href="Javascript://" onClick="_openApproval(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);" >
<asp:HyperLink ID="lnkApproval"></asp:HyperLink>
</a>
</ItemTemplate>
</asp:TemplateColumn>

And I suppose I code something in the ItemDataBound to change the Hyperlink's Text? But How?

Paul
 
K

Kim Quigley

I'm glad you were able to figure it out :)
Thanks Kim, that was a big help however, I had to resort to:
"If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then"

In order for it to properly execute. I don't understand why your IF statement failed.

Paul
Paul,

There are a number of ways you can accomplish what you are asking, but the best solution depends on what part of the link you're trying to change and what the conditions are. If your onClick code isn't going to change and you only want to modify the link text, then this is what I recommend putting in the ItemDataBound method:

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
If something then
CType(e.Item.Cells(YourColumnID).FindControl("LinkLabel"), Label).Text = 'something
End If
End If

Your column would need to look like this

<asp:TemplateColumn>
<ItemTemplate>
<a href="Javascript://" onClick='alert(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);'><asp:Label id=LinkLabel runat="server" text="Unchanged Text"></asp:Label></a>
</ItemTemplate>
</asp:TemplateColumn>

How do I create a Hyperlink column in a Datagrid which launches a pop-up window, and have the text in the hyperlink change based on a condition? I'm trying to do something like the following:

<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<a href="Javascript://" onClick="_openApproval(<%# DataBinder.Eval(Container, "DataItem.Training_Schedule_ID") %>);" >
<asp:HyperLink ID="lnkApproval"></asp:HyperLink>
</a>
</ItemTemplate>
</asp:TemplateColumn>

And I suppose I code something in the ItemDataBound to change the Hyperlink's Text? But How?

Paul
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top