c# inline code reminder

Just a short reminder for inline code. While I do agree that it should be avoided as much as possible, it comes pretty handy for databindable controls or to quickly test something without recompiling everything.

So here are some quick notes :

Some code that must be executed

    void Page_Load( object sender, EventArgs e )   {  }

<% int id = int.Parse(Request["productID"]); %>

<%= writes out the code inside which means that

<%=productID %>

is equivalent to :

<% Response.Write(productID); %>

Please beware that there is no ";" at the end of your <%= … %> statement in that case.

Databinding management

<%# Eval("productID") %>

your datasource must obviously have the element selected with the right name.

Within a Repeater control, in order to create a customized link :

<asp:Hyperlink runat="server" ID="linkProduct"  NavigateUrl="product.aspx?id=<%# Eval("productID") %>" />

This is always handy to avoid dynamic user control, most of the time, a repeater with the right controls inside will do the job (and its easier to manager)


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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s