My (mostly technical) blog

Data binding from a property in the code behind

Posted on: February 17, 2010

I’ve run across this situation multiple times, where I have for example, an ID that I get in the code behind, and I want to append it to some href in the aspx, and each time I forget how to do it, so as a reminder for myself, here is how to do it and problems I ran into:

Assuming I have a property in the code behind that returns a GUID

protected Guid CustomerId {
 get { return new Guid(); }
}

And assuming I have a link in my aspx page that I want to append this property to, here is how to do it

<a href="upgrade.aspx?id=<%= CustomerId .ToString() %>">Upgrade</a>

Note that this will not work if you specify a runat=server to the href for any reason, for example. to show the link or hide it according to specific logic.

If you need to show and hide the link, you can enclose it in a place holder and access that placeholder from the code behind

<asp:PlaceHolder ID="UpdgradeLinkPlaceholder" runat="server">
<a href="upgrade.aspx?id=<%= CustomerId .ToString() %>">Upgrade</a>
</asp:PlaceHolder>
Advertisements
Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Ahmed Sabbour's Facebook profile
February 2010
S M T W T F S
« Apr   Apr »
 123456
78910111213
14151617181920
21222324252627
28  

Tweets

RSS StackOverflow

Recently bookmarked

%d bloggers like this: