1 Replies - 8803 Views - Last Post: 10 May 2012 - 04:36 PM

#1 kathyExalon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-April 12

HTML Anchor Tag Dynamically Populated, get Object Ref Error?

Posted 26 April 2012 - 01:56 AM

I am STRUGGLING with this, hope someone can shed some light? Don't know what I'm missing here?
Trying to populate HTML anchor tag href dynamically, so in my aspx page, I have the following:

<a id="cbphone" runat="server" class="cb-phone" href="#" onclick="clickPhone(409)" > 
            <asp:ImageButton ID="ImageButton1" runat="server" CssClass="contactButton" 
                ImageUrl="~/images/phone.png" onclick="ImageButton1_Click" /></a>



and in my cs page I have this:

HtmlAnchor phone = (HtmlAnchor)this.FindControl("cbphone");
      phone.HRef = "tel:" + cL.GetBusinessTel(customerid);
      Page.Controls.Add(phone);


and I get "Object reference not set to an instance of an object." error on this line, after looking for the html anchor tag:

phone.HRef = "tel:" + cL.GetBusinessTel(customerid);

I have debugged, it does get the tel no.

am I missing something obvious?

Is This A Good Question/Topic? 0
  • +

Replies To: HTML Anchor Tag Dynamically Populated, get Object Ref Error?

#2 pradeep1210  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 56
  • Joined: 08-May 12

Re: HTML Anchor Tag Dynamically Populated, get Object Ref Error?

Posted 10 May 2012 - 04:36 PM

Two Things:

1. You don't need the FindControl method, since you have the runat=Server tag specified. You can directly use the control name.

2. Remove this line and it should start working properly.
Page.Controls.Add(phone);
This is because you already have the control added to your form in the aspx file.

This leaves you with just one line code instead of what you have at present and it should work:
this.cbphone.HRef = "tel:" + cL.GetBusinessTel(customerid);


On a side note, I'm just curious why you use the <a> tags, and not the <asp:Hyperlink> tag, which is eventually rendered as <a> in the browser. The proper asp.net way should have been like this, though what you are using would also work:

In aspx page:
<asp:HyperLink id="cbphone" runat="server" CssClass="cb-phone" NavigateUrl="#" onclick="clickPhone(409)" > 
    <asp:ImageButton ID="ImageButton1" runat="server" CssClass="contactButton" 
        ImageUrl="~/images/phone.png" onclick="ImageButton1_Click" />
</asp:HyperLink>



Code-behind:
this.cbphone.NavigateUrl = "tel:" + cL.GetBusinessTel(customerid);

This post has been edited by pradeep1210: 10 May 2012 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1