2 Replies - 4826 Views - Last Post: 20 February 2013 - 07:37 AM Rate Topic: -----

#1 Mideoan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 31-May 11

ItemDataBound event when using HyperLinkColumn

Posted 19 February 2013 - 03:18 PM

I have a Datagrid that contains a HyperLinkColumn.

            <asp:HyperLinkColumn  
                HeaderStyle-CssClass="colHeader"
                ItemStyle-CssClass="colGeneral"
                HeaderText="Tracking #"
                DataNavigateUrlField="TrackingNumber"
                DataNavigateUrlFormatString="http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums={0}&IsOrderHistory=true"
                DataTextField="TrackingNumber"
                ItemStyle-Width="140px"
                Target="_blank">
            </asp:HyperLinkColumn>



I need to dynamically change the DataNavigateUrlFormatString in the ItemDataBound event. Sounds easy enough, but I don't know how to cast my Item to a HyperLinkColumn to set it.

Here is basically what I have that builds but obviously doesn't work.

        protected void grdOrders_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {

                HyperLinkColumn hlc = (HyperLinkColumn)e.Item.DataItem;

                if (hlc.DataNavigateUrlField.Substring(0, 2) == "1Z")
                {
                    hlc.DataNavigateUrlField = "Hi";
                }
                else
                {
                    hlc.DataNavigateUrlField = "Bye";
                }

                
            }


Google has failed me. Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: ItemDataBound event when using HyperLinkColumn

#2 Mideoan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 77
  • Joined: 31-May 11

Re: ItemDataBound event when using HyperLinkColumn

Posted 20 February 2013 - 07:32 AM

Found a solution. If anyone wants to reference this in the future, this was what I did.

        protected void grdOrders_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {

                HyperLink hl = e.Item.Cells[5].Controls[0] as HyperLink;

                if (hl.Text.Contains("1Z"))
                {
                    hl.NavigateUrl = "http://wwwapps.ups.com/WebTracking/track?track=yes&trackNums=" + hl.Text.ToString();
                    
                }
                else
                {
                    hl.NavigateUrl = "https://tools.usps.com/go/TrackConfirmAction_input?qtc_tLabels1=" + hl.Text.ToString();
                }

                
            }


        }


Detects if it is UPS or USPS and links website passing query string of tracking number.
Was This Post Helpful? 1
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: ItemDataBound event when using HyperLinkColumn

Posted 20 February 2013 - 07:37 AM

Edit: Looks like you got it figured out while I was writing this, nice. Anyway I'll leave this here for anyone else that might find it useful.



You're not going to convert the DataItem to a HyperLinkColumn. The HyperLinkColumn represents a single property/field of your data, the DataItem represents the entire row.

I think you'd have an easier time converting your HyperLinkColumn into a TemplateColumn and using a HyperLink control. Then you can use FindControl in your ItemDataBound event to locate that HyperLink control and update its value.

HyperLink hl = e.Item.Cells[hyperLinkColumnIndex].FindControl("HyperLinkID") as HyperLink;
// update HyperLink



Another suggestion I'd make is to start using the GridView and ListView controls unless you are stuck with pre .Net 2.0 for some reason.

MSDN Website said:

If you are familiar with the DataGrid control from the .NET Framework version 1.0, the GridView control is the successor to the DataGrid control.

This post has been edited by Nakor: 20 February 2013 - 07:44 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1