hyperlink and dynamically formed NavigateURL problem

T

Tomek R.

Hello !

This post does'nt regard column hyperlink.
I just have single hyperlink and want to create it's NavigateUrl
dynamically.

This is my test page

<form id="Form1" method="post" runat="server">
<asp:HyperLink id="MyHLink" NavigateUrl='<%# geturl("123456") %>'
style="Z-INDEX: 101; LEFT: 280px; POSITION: absolute; TOP: 56px"
runat="server" >MyHLink</asp:HyperLink>
</form>

and my class behind

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.HyperLink MyHLink;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
//this.MyHLink.NavigateUrl = MyString();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
protected string geturl(string s)
{ return "www.mypage.com "+s; }
}

Resulting page includes hyperlink, but inactive - not href tag attached. The
syntax is O'K, I believe (no errors on page load).
I've spent a couple of hours for browsing the newsgroups but haven't find
working solution for HTML designing.
What means, I can do this but only when setting NavigateUrl in Page_Load
like this:

private void Page_Load(object sender, System.EventArgs e)
{
MyHLink.NavigateUrl = geturl("123456");
}

No matter what kind of Url I put (absolute, relative) - Page_Load works
every time.

The only reason I can think about, is that one: geturl and class-behind is
not accessible, when hyperlink is rendered based on design values.
Am I right ?
If yes, how about datagrid and TemplateItem column with hyperlink. In that
case settings like above work perfectly. What is the differences between
datagrid column and hyperlink on page ?

Regards,
 
K

Karl Seguin

instead of doing NavigateUrl='<%# geturl("12345")%>' do NavigateUrl='<%=
getUrl("12345")%>'

<%# is for databinding, you aren'd databinding and therefore need touse <%=

doing it form your Page_Load, ala:
MyHLink.NavigateUrl = geturl("123456");

should work (And is how you should do it, no clumsy method calls in your
presentation), but if you still had the <%# in there, dunno how that would
behave...

Karl
 
T

Tomek R.

Karl !
Thanks for reply.
<%# is for databinding, you aren'd databinding and therefore need touse
<%=
Yeah, I found some posts with this info, but <%= doesn't work. The resulting
link is the whole phrase, inlcuding <% .
 
K

Karl Seguin

Ya, was being an idiot when I just woke up :)

The correct way to go, as I originally said, is via the Page_Load and doing
MyHLink.NavigateUrl = geturl("123456");

if you are certain Page_Load is running, and you don' thave NavigateUrl set
to anything in the HTML (don't even specifiy the problem), it should work.

karl
 
Joined
May 28, 2008
Messages
2
Reaction score
0
do you still have the isue

Hi,
I have the same issue...is it the only way to load it on page load
 
Joined
Feb 13, 2009
Messages
3
Reaction score
0
For the problem of absolute url binding to NavigateURL field of asp:HyperLink
Solution is to use Page_PreRender method to do the binding because it will be overwritten if you do it Page_OnLoad

can find more here
asp:HyperLink NavigateUrl Problem Absolute URL

Link: sobot-software.blogspot.com/2009/02/asphyperlink-navigateurl-problem.html

Sobot Software DotNetNuke Outsourcing
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top