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

前端 未结 4 2111
无人共我
无人共我 2020-12-31 13:00

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

4条回答
  •  梦毁少年i
    2020-12-31 13:47

    You have a couple of different tags:

    <% executes the code inside:

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

    <%= writes out the code inside:

    <%=id %> 
    
    
    <% 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 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:

    
    " />
    
    <%  //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 <%= ... %>

提交回复
热议问题