Binding Hyperlink.NavigateUrl declaratively

  • Thread starter Thread starter Dmitry Duginov
  • Start date Start date
D

Dmitry Duginov

I have a FormView and a HyperLink on the page.

If I use the following code behind (hooked up to Hyperlink's OnLoad event),
it renders fine

protected void link_Load(object sender, EventArgs e)
{
link.NavigateUrl = String.Format("~/Carrier/AddNew.aspx?Id={0}",
FormView1.DataKey.Value);
}

However, I was not able to achieve the same goal declaratively, just using
<%# ... %> syntax in .aspx

Where's the catch?



Dmitry
 
I have a FormView and a HyperLink on the page.

If I use the following code behind (hooked up to Hyperlink's OnLoad event),
it renders fine

protected void link_Load(object sender, EventArgs e)
{
link.NavigateUrl = String.Format("~/Carrier/AddNew.aspx?Id={0}",
FormView1.DataKey.Value);
}

However, I was not able to achieve the same goal declaratively, just using
<%# ... %> syntax in .aspx

Where's the catch?

Dmitry

show us your code for the second one
 
densial said:
show us your code for the second one

Any of the following renders just text instead of hyperlink:
NavigateUrl='<%# String.Format("~/Carrier/AddNew.aspx?Id={0}",
Eval("FormView1.DataKey.Value")) %>'

NavigateUrl='<%# String.Format("~/Carrier/AddNew.aspx?Id={0}",111) %>'

NavigateUrl='<%# Eval("FormView1.DataKey.Value") %>'

D.
 
Hi Dmitry,

I've done some test and both of following approaches should work for you:


<asp:HyperLink ID="link1" runat="server" Text="Link" NavigateUrl='<%#
String.Format("~/Carrier/AddNew.aspx?id={0}", Eval("Id"))
%>'></asp:HyperLink>
<br />
<asp:HyperLink ID="link2" runat="server" Text="Link2" NavigateUrl='<%#
String.Format("~/Carrier/AddNew.aspx?id={0}", FormView1.DataKey.Value)
%>'></asp:HyperLink>



The first one is assuming you need to use some field as the id; the second
one is the exact case as yours.

Please feel free to let me know if there's anything I can help.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
"Walter Wang [MSFT]" said:
Hi Dmitry,

I've done some test and both of following approaches should work for you:


<asp:HyperLink ID="link1" runat="server" Text="Link" NavigateUrl='<%#
String.Format("~/Carrier/AddNew.aspx?id={0}", Eval("Id"))
%>'></asp:HyperLink>
<br />

First one doesn't woudn't since I dont have Id control
<asp:HyperLink ID="link2" runat="server" Text="Link2" NavigateUrl='<%#
String.Format("~/Carrier/AddNew.aspx?id={0}", FormView1.DataKey.Value)
%>'></asp:HyperLink>

Second one "just" doesn't work. It renders text only:
<a id="ctl00_ContentPlaceHolder1_link2">Link2</a>

D.
 
"Walter Wang [MSFT]" said:
Hi Dmitry,

First one is just an example, just to describe an approach if you need to
use some filed instead of the DataKey.

Are you using a multi fields DataKey? Would you please post your complete
code or send me a complete project?

Nope, it is single-field DataKey. The significant difference between your
code and mine is that my link is outside the FormView (see a new line for
link3). This way is doesn't work.

<asp:HyperLink ID="link1" runat="server" Text="Link"
NavigateUrl='<%# String.Format("~/Carrier/AddNew.aspx?id={0}", Eval("Id"))
%>'></asp:HyperLink>
<br />
<asp:HyperLink ID="link2" runat="server" Text="Link2"
NavigateUrl='<%# String.Format("~/Carrier/AddNew.aspx?id={0}",
FormView1.DataKey.Value) %>'></asp:HyperLink>
</ItemTemplate>
</asp:FormView>
<asp:HyperLink ID="link3" runat="server" Text="Link3"
NavigateUrl='<%# String.Format("~/Carrier/AddNew.aspx?id={0}",
FormView1.DataKey.Value) %>'></asp:HyperLink>

Dmitry
 
Hi Dmitry,

The data binding expression <%# %> must be used inside a control that whose
DataBind() method will be called at run-time, otherwise it will not work.

#
http://msdn2.microsoft.com/en-us/library/ms178366.aspx
<quote>
Data-binding expressions are resolved when the DataBind method of a control
or of the Page class is called. For controls such as the GridView,
DetailsView, and FormView controls, data-binding expressions are resolved
automatically during the control's PreRender event and you are not required
to call the DataBind method explicitly.
</quote>

#Digging Into Data Binding Expressions
http://www.odetocode.com/Articles/278.aspx
<quote>
Under The Covers Of The Data Binding Expression

In order to really understand what happens inside of the data binding
expression, let¡¯s take a look at the code the runtime generates for the
ASPX file.
...
</quote>

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Dmitry,

I'm writing to check the status of this post. Please feel free to let me
know if there's anything else I can help. Thanks.



Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top