hsz
hsz

Reputation: 152206

Databinding does not work

In MainPage.aspx I have

<asp:HyperLink runat="server" NavigateUrl='<%#"http://google.pl"%>'>test</asp:HyperLink>

It does not add an href tag but only outputs <a>test</a>. When I do:

<asp:HyperLink runat="server" NavigateUrl='http://google.pl'>test</asp:HyperLink>

It works fine.

Why <%#"http://google.pl"%> does not work ?

How to debug it ?

Upvotes: 0

Views: 558

Answers (4)

m0sa
m0sa

Reputation: 10940

You cannot output HTML literals into asp control properties. You can either set the hyperlink NavigateUrl property in codebehind or output the html anchor as a literal.

Output as a literal (in this case your codebehind class must expose a protected or public property or field named UrlParams)

// site.aspx
<a href="<%="http://google.pl" + UrlParams.Google%>">test</a>

Set in codebehind:

// site.aspx
<asp:HyperLink runat="server" ID="link1">test</asp:HyperLink>
// site.aspx.cs or <script runat="server">..</script>
protected void Page_Load(..) {
    link1.NavigateUrl = "http://google.pl" + UrlParams.Google;
}

And the answer to your original question. The <# is a data-binding expression, that creates a binding between a server control property and a data source.

EDIT: As it turns out, you can actually use the <# syntax with strings too. The key for it to work is to call the Control.DataBind() method. You can call it on the HyperLink control or even on the current Page, since Page inherits from Control.

Example: (put this in an empty aspx page - no datasources required):

<asp:HyperLink ID="link1" runat="server" NavigateUrl='<%# "#Test" %>'>Test</asp:HyperLink>
<script runat="server" type="text/C#">
    protected override void OnLoad(EventArgs e)
    {
        DataBind();
        // or: 
        // link1.DataBind();
        base.OnLoad(e);
    }
</script>

Upvotes: 6

Dima Shmidt
Dima Shmidt

Reputation: 905

You can use something like this:

Html part of the page:

<asp:HyperLink ID="test" NavigateUrl="<%# GetUrl() %>" runat="server">test</asp:HyperLink>

Code behind:

protected string GetUrl()
{
    //Put your logic here to generate dynamicly url that you want
}

protected void Page_Load(object sender, EventArgs e)
{
    DataBind();
}

Hope it will help you.

Best regards, Dima.

Upvotes: 3

Stilgar
Stilgar

Reputation: 23551

Excuse me but this actually works:

<asp:ListView ID="lvTest" runat="server">
<ItemTemplate>
    <asp:HyperLink ID="test" NavigateUrl='<%#"http://google.pl"%>' runat="server">test</asp:HyperLink>
</ItemTemplate>


protected void Page_Load(object sender, EventArgs e)
{
    lvTest.DataSource = Enumerable.Range(0, 5);
    lvTest.DataBind();
}

if you want to mix the datasource with the literal you can do it like this:

<asp:HyperLink ID="test" NavigateUrl='<%#"http://google.pl?page=" + Container.DataItem %>' runat="server">test</asp:HyperLink>

You should use Eval instead of Container.DataItem if you want to access specific property. BTW all this is bad practice. You should use the ItemDataBound (or similar event) and bind using strongly typed C# code.

Upvotes: 3

jwheron
jwheron

Reputation: 2562

This is a stab in the dark, but the <%# and %> tags indicate that you want to run data binding code (as in, something you've returned from the database).

You don't need these to output a string literal, but if you did, the appropriate tag would be <%=, not <%#.

Upvotes: 0

Related Questions