Re: Hyperlink copy question

Discussion in 'ASP .Net' started by Brian Williams, Jan 17, 2007.

  1. When you do me.myotherLink = klink they both point to the same object in
    memory essentially they are the same object.
    What I would do is create a clone method adding all the properties and
    attributes.

    I don't know VB.Net so my example will be in C#.

    HyperLink hlnk = this.gvSSShot.SelectedRow.FindControl("hlnk_BPChart");
    HyperLink hl = Clone(hlnk);
    hl.ID = "NewControlID";
    PlaceHolder1.Controls.Add(hl);

    private static HyperLink Clone(HyperLink ctrl)
    {
    HyperLink hl = new HyperLink();
    hl.Text = ctrl.Text;
    hl.ImageUrl = ctrl.ImageUrl;
    hl.NavigateUrl = ctrl.NavigateUrl;

    foreach (string attribute in ctrl.Attributes.Keys)
    {
    hl.Attributes[attribute] = ctrl.Attributes[attribute];
    }
    return hl;
    }

    I hope this helps, maybe someone will have a better solution.

    Regards,
    Brian K. Williams

    "Yankee Imperialist Dog" <>
    wrote in message news:...
    > why does this not work: I'm trying to resign the values from one Hyperlink
    > to
    > another hyperlink
    > Dim hlnk As HyperLink =
    > Me.gvSSShot.SelectedRow.FindControl("hlnk_BPChart")
    > ' then just give it everything from the found link
    > me.myotherLink = hlnk
    >
    > It seems to me this should work and negate the need to assign text,
    > NavagateURL, target, ....
    >
    > Anyone know why?
    > Thanks
    > --
    > Share The Knowledge. I need all the help I can get and so do you!
    Brian Williams, Jan 17, 2007
    #1
    1. Advertising

  2. Objects are by value when passing them without specifying, you would need to
    specify (ref) otherwise.

    When you do me.myotherLink = hlnk both refer to the same object they are
    still however independent objects themselves. Changing the value of the
    first will not change the value of second even though their values are still
    references to the same object, changes made to the object through the first
    object will be visible through the second object.

    I hope I didnt' make this more confusing... :)

    Regards,
    Brian


    "Yankee Imperialist Dog" <>
    wrote in message news:...
    > Thanks for responsing,
    > That is exactly what i have done except not with a loop (yet). I'm
    > starting
    > create a "bag" of functions (or Subs) for situations like this.
    > 1. I thouht that objects were by value not by reference in dot.net?
    > 2. in the loop would the id be overwritten as it is an attribute?
    >
    > Still new to this so hope my question makes sense....
    > --
    > Share The Knowledge. I need all the help I can get and so do you!
    >
    >
    > "Brian Williams" wrote:
    >
    >> When you do me.myotherLink = klink they both point to the same object in
    >> memory essentially they are the same object.
    >> What I would do is create a clone method adding all the properties and
    >> attributes.
    >>
    >> I don't know VB.Net so my example will be in C#.
    >>
    >> HyperLink hlnk = this.gvSSShot.SelectedRow.FindControl("hlnk_BPChart");
    >> HyperLink hl = Clone(hlnk);
    >> hl.ID = "NewControlID";
    >> PlaceHolder1.Controls.Add(hl);
    >>
    >> private static HyperLink Clone(HyperLink ctrl)
    >> {
    >> HyperLink hl = new HyperLink();
    >> hl.Text = ctrl.Text;
    >> hl.ImageUrl = ctrl.ImageUrl;
    >> hl.NavigateUrl = ctrl.NavigateUrl;
    >>
    >> foreach (string attribute in ctrl.Attributes.Keys)
    >> {
    >> hl.Attributes[attribute] = ctrl.Attributes[attribute];
    >> }
    >> return hl;
    >> }
    >>
    >> I hope this helps, maybe someone will have a better solution.
    >>
    >> Regards,
    >> Brian K. Williams
    >>
    >> "Yankee Imperialist Dog" <>
    >> wrote in message
    >> news:...
    >> > why does this not work: I'm trying to resign the values from one
    >> > Hyperlink
    >> > to
    >> > another hyperlink
    >> > Dim hlnk As HyperLink =
    >> > Me.gvSSShot.SelectedRow.FindControl("hlnk_BPChart")
    >> > ' then just give it everything from the found link
    >> > me.myotherLink = hlnk
    >> >
    >> > It seems to me this should work and negate the need to assign text,
    >> > NavagateURL, target, ....
    >> >
    >> > Anyone know why?
    >> > Thanks
    >> > --
    >> > Share The Knowledge. I need all the help I can get and so do you!

    >>
    >>
    >>
    Brian Williams, Jan 17, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    2
    Views:
    1,203
  2. davetichenor
    Replies:
    1
    Views:
    808
    Eliyahu Goldin
    Oct 30, 2006
  3. Replies:
    26
    Views:
    2,091
    Roland Pibinger
    Sep 1, 2006
  4. Ken
    Replies:
    1
    Views:
    363
    Alvin Bruney
    Aug 16, 2003
  5. Dave
    Replies:
    0
    Views:
    927
Loading...

Share This Page