开发者

Is it possible to set a new asp Hyperlink from Code Behind

I am wondering if it is possible to have an asp hyperlink on an aspx page and in the code behind set it to a new hyperlink in the Page_Load section. I've tried this and it appears not to work, but wanted to know more about the 开发者_开发技巧mechanics (is this something that just doesn't work, or could work earlier in the page lifecycle, or why it doesn't work).

Example Code

Aspx Page

<asp:HyperLink ID="myHyperLink" runat="server" />

Code Behind

var newHyperLink = new HyperLink();
newHyperLink.NavigateUrl= url;
newHyperLink.Text = "Hello World";
myHyperLink = newHyperLink;


You don't need to create a new control, just use the one you declared. You're also not using the right property.

Replace your code behind with this:

myHyperLink.NavigateUrl = "http://url.com";

I don't believe you can override a control like how you mentioned, I think it's a case of adding or removing a control through a parent control's control collection.

Ie,

<form id="form1" runat="server">
<div>
    <asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>
</div>
</form>

    protected void Page_Load(object sender, EventArgs e)
    {
        HyperLink newHyperLink = new HyperLink();
        newHyperLink.NavigateUrl = "http://google.com";
        newHyperLink.Text = "Hello World";
        Page.Form.Controls.Remove(HyperLink1);
        Page.Form.Controls.Add(newHyperLink);
    }


during the page lifecycle, it will instantiate variables for the controls on the ASPX page and add them to the Page.Controls collection. when it renders the page, it renders each child control in the Page.Controls collection.

what you're doing is reassigning the variable that used to be assigned to the lifecycle instantiated control, but is now assigned to your user instantiated control, however, the original control is still present in the Page.Controls collection, and you no longer have a reference to it.

what you'd need to do is remove the old control from Page.Controls, instantiate your new control and add it to the Page.Controls collection at the same point as the old one.

however, I would not recommend doing this. there is probably a better way to do what you're trying to achieve, but that is why it's not working.


.NET has a garbage collector taking care of its code cleanup. That's why you don't (generally) need destructors.

What is relevant in this case is that something "goes away" when nothing references it.

What is also relevant is what myHyperLink represents. myHyperLink isn't the object itself, it's a pointer to the object. There are certain base types such as int where this is not the case, but when you deal with complex objects, that's what's going on. So in this case you have both the myHyperLink pointer and the object that myHyperLink points to. This distinction is very important.

In your code we have the hyperlink itself and the myHyperLink pointer. The hyperlink object itself is referenced by both Page.Form.Controls and your local reference to myHyperLink.

When you said

myHyperLink = newHyperLink;

What you did was change what the myHyperLink pointer was pointing to. The original hyperlink still existed in Page.Form.Controls. This means the original hyperlink doesn't go away - it's still being referenced.

Next, why was the original one still displayed and not the new one? Well, when the page renders it mainly cares about what exists in Page.Form.Controls. Since the original reference is still in page.form.controls, that's what gets rendered. While the new hyperlink exists, the new hyperlink was never added to Page.Form.Controls. It was simply refrenced by your pointer and manipulated. Unless you add it to Page.Form.Controls (or override render manually/some other weird thing) it won't display.


You can try this too:

Hyperlink HL1 = new Hyperlink();
HL1.Text = "Hyperlink";
HL1.TabIndex = 1;
HL1.AccessKey = "w";
HL1.NavigateUrl = "http://stackoverflow.com/";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜