Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp.net C# hyperlink column 1

Status
Not open for further replies.

Kurt111780

Technical User
Nov 20, 2003
235
GB
Hello,

I'm using a datagrid with a hyperlink column.

Code:
<asp:HyperLinkColumn DataTextField="txtName" DataNavigateUrlField="showID" DataNavigateUrlFormatString="exboDetail.aspx?id={0}" HeaderText="Exhibition" />

It works great but I only want the link to show up if a field in the database is not blank. How can I do this without the ID field?

Thanks,
Kurt
 
If you have a look at the ItemCreated and ItemDataBound methods of the DataGrid, you will be able to enable/disable hyperlink controls based on whatever parameters you want.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I am not sure but...

I would look into the ItemDataBound event and check the value at that time - make it not visible if the data doesn't exist... The example that I am including is just changing the color based on the value but should give you an idea.

Code:
    Private Sub dg_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgInOrOut.ItemDataBound
        If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
            Dim strIn As String = e.Item.Cells(5).Text
            If strIn = "Out" Then
                e.Item.BackColor = Color.Red
            Else
                e.Item.BackColor = Color.Gold
            End If
        End If
    End Sub

Hope everyone is having a great day!

Thanks - Jennifer
 
I'm new to asp.net and C# so that vb.net looks really confusing.

I tried searching on ItemDataBound but could not find a good example.

I'm not sure what this is doing: e.Item.ItemType

Any other ideas?

Thanks,
Kurt
 
The e.Item.ItemType is just checking the type of item that is created in the DatGrid (i.e. a header row, a normal or alternating row or a footer row).

If you type ItemDataBound into google the first result you get back is an MSDN article with both C# and VB.NET sample illustrating it's uses so information on this subject isn't very hard to find:


----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Ok so far this is what I have but it does not seem to do anything.

Code:
void Item_Bound(Object sender, DataGridItemEventArgs e)
      {

      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
         {
		 
		 		if (description == "")
				{	
					e.Item.FindControl("lblLink").Visible = false;
				}
				else
				{
					e.Item.FindControl("lblLink").Visible = false;

				}
				
		}
}

Even if I take everything out but this
Code:
e.Item.FindControl("lblLink").Visible = false;
It still does nothing and the link is still there.

Really what I need is something like this
Code:
e.Item.FindControl("lblLink").NavigateUrl = someUrl;
If I do that I get an error.
CS0117: 'System.Web.UI.Control' does not contain a definition for 'NavigateUrl'


Any more ideas? seems like im getting closer.

Thanks,
Kurt
 
You need to get a reference to the link before accessing it.

FindControl() returns an object of type Control, not HyperLink.

So:

HyperLink lnk = e.Item.FindControl("lblLink") as HyperLink;
if(lnk != null)
{
lnk.NavigateUrl = "yourpage.aspx";
}//if

But, if you want to do this type of thing, I recommend that you junk the HyperLinkColumn in exchange for a TemplateColumn. It will give you much more granular control over your layout. For example:

<asp:TemplateColumn HeaderText="Exhibition">
<ItemTemplate>
<asp:HyperLink id=lnkLink runat=Server />
</ItemTemplate>
</asp:TemplateColumn>

Then, in your OnItemDataBound event, grab the link w/ the following code:

HyperLink lnkLink = e.Item.FindControl("lnkLink") as HyperLink;
if(lnkLink != null)
{
//set the properties of the link here, including visiblity
}//if

Hope that helps.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Great post link9

Hope everyone is having a great day!

Thanks - Jennifer
 
Thanks, that helps a lot. However it still does not work. I got rid of all the if statements for now.

Code:
void Item_Bound(Object sender, DataGridItemEventArgs e)
      {

	HyperLink lnkLink = e.Item.FindControl("lnkLink") as HyperLink;

lnkLink.NavigateUrl = "yourpage.aspx";

}

Do I have to call Item_Bound somewhere?

Thanks,
Kurt

Code:
	      		<ItemTemplate>
   					<asp:HyperLink ID="lnkLink" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txtName")%>'  />
	      		</ItemTemplate>
 
Do I have to call Item_Bound somewhere?
Yes - if you don't tell it to call it how would it know?

If you look at the MSDN article they call it with this line:
Code:
OnItemDataBound="Item_Bound"


----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Or you can wire it up in the Init event of your page:

myDataGrid.ItemDataBound += new DataGridItemEventHandler(Item_Bound)

So the += operator there is adding a subscription to the ItemDataBound event of the datagrid... the subscriber is your event handler.

-p

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Thanks for your help.

It sorta works. Here is what I have:

Code:
void Item_Bound(Object sender, DataGridItemEventArgs e)
{
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
         {
		 
			HyperLink lnkLink = e.Item.FindControl("lnkLink") as HyperLink;
			Label lblURL = e.Item.FindControl("lblURL") as Label;

			if(lblURL != null)
			{
 				lnkLink.Enabled = true;
			}
			else
			{
				lnkLink.Enabled = false;
			}//end if		
			 
		}//end if
}

Code:
<asp:TemplateColumn HeaderText="Exhibition">
	<ItemTemplate>
   		<asp:Label Visible="false" ID="lblURL" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "txtLink")%>'/>
   		<asp:HyperLink ID="lnkLink" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "txtName")%>' NavigateUrl = '<%#"exhibitDetail.aspx?ID=" + (DataBinder.Eval(Container.DataItem, "showID" ))%>'  />
	 </ItemTemplate>
</asp:TemplateColumn>

It doesn't seem to matter if what is in the datbase field txtLink. All the links still show up.

Am I doing something wrong?

Thanks,
Kurt
 
A couple of things:

(1) What you need to do is to check the .Text property of the label once you get a reference to it. The label, in the case, will never be null. It's the .Text property that may be == string.Empty, so that's what you need to check.

Instead of:
if(lblURL != null)

try:
if(lblURL != null && lblURL.Text != string.Empty)

Note that I'm still testing to make sure that my reference is not null. That's good programming practice, and you should always check to make sure the target of an 'as' statement is not null.

(2) Your databinding statements there, while they work, use a technique called late binding, and it is slower to bind that way. Opt instead for early binding. You want to downcast the Item to its proper type instead of letting the framework figure it out at runtime. For example, if your datasource is a datatable, your statement would look like this:

<asp:Label Visible="false" ID="lblURL" runat="server" Text='<%#((DataRowView)(Container.DataItem)["txtLink"]%>' />

You can modify to suit the type of your datasource.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Thanks, It works now.

There is a problem. In firefox the ones with out a link show up fine.

In internet explorer the ones with out links look faded.

The lnkLink.Enabled = false; seems to be doing this but I need that to disable the link.

Thanks,
Kurt
 
Probably just set .Visible = true/false instead of enabled.

Enabled is not a cross browser property.

-p

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Thanks link9,

It works great now!

Code:
void Item_Bound(Object sender, DataGridItemEventArgs e)
{
      if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
         {
		 
			HyperLink lnkLink = e.Item.FindControl("lnkLink") as HyperLink;
			HyperLink noLink = e.Item.FindControl("noLink") as HyperLink;
			Label lblURL = e.Item.FindControl("lblURL") as Label;

			if(lblURL != null && lblURL.Text != string.Empty)			{
 				lnkLink.Visible = true;
				noLink.Visible = false;
			}
			else
			{
				lnkLink.Visible = false;
				noLink.Visible = true;
				
			}//end if		
			 
		}//end if
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top