Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use c# code inside <% ... %> tags on asp.net page?

Tags:

c#

asp.net

I'm writing an asp.net user control. It has a property, FurtherReadingPage, and two controls bound to it: ObjectDataSource and a Repeater. Inside the Repeater I would like to display a hyperlink with an href property set to something like FurtherReadingPage + "?id=" + Eval("Id"). I don't know how to do it inside the page's markup. I can use <% Eval("Id") %> or <% Response.Write(FurtherReadingPage + "?id=") %> alone but I don't know how to mix them.

like image 472
agnieszka Avatar asked Jul 21 '09 10:07

agnieszka


People also ask

How is C used?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C easy for beginners?

C is not just what students use to learn programming. It's not an academic language. And I would say it's not the easiest language, because C is a rather low level programming language. Today, C is widely used in embedded devices, and it powers most of the Internet servers, which are built using Linux.

What does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

How can I learn C language perfectly?

Get started with C. Official C documentation - Might be hard to follow and understand for beginners. Visit official C Programming documentation. Write a lot of C programming code - The only way you can learn programming is by writing a lot of code.


1 Answers

You have a couple of different tags:

<% executes the code inside:

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

<%= writes out the code inside:

<%=id %> <!-- note no ; -->

<!-- this is shorthand for: -->
<% Response.Write(id); %> 

Both of these break up the normal flow when rendered on a page, for instance if you use them in a normal Asp.net <head runat="server"> you'll get problems.

<%# databinding:

<%# Eval("id") %>

This allows you to specify the bindings for controls that Asp.net WebForms render as a collection (rather than the literal controls that you can use <%= with), for instance:

<!-- this could be inside a repeater or another control -->
<asp:Hyperlink runat="server" ID="demo" 
     NavigateUrl="page.aspx?id=<%# Eval("id") %>" />

<%  //without this bind the <%# will be ignored
    void Page_Load( object sender, EventArgs e ) {
        demo.DataBind(); 
        //or
        repeaterWithManyLinks.DataBind(); 
    } 
%>

For your specific case you either:

  • Use a repeater and <%# Eval(...) %> with repeater.DataBind();

or

  • Use a foreach loop (<% foreach(... %>) with <%= ... %>
like image 156
Keith Avatar answered Oct 18 '22 22:10

Keith