0

If I want to combine asp.net code with html (mark up) file, I need to open <% %> and execute teh code.

What If I want to iterate over a database with select query while feeding information and creating rows. For example:

<table>

<%
   foreach(DataRow dr in dataset.Tables["empoloyees"].Rows)
   {
%>
<tr>
<td>
      <asp:Label runat="Server" Text="<% dr[FirstName].toString(); %>"/>
</td>
<td>
      <asp:Label runat="Server" Text="<%dr[LastName].toString();%>"/>
</td>
</tr>
<%
   }
%>
</table>

Is the syntax correct..and is that practice good (it is always used in php) ? or should I bind the data to the label somehow?(no idea how. but somehow)?

4
  • You can't use <%%> inside the markup of a server side control. Commented Oct 27, 2011 at 13:01
  • this is so ASP classic old fashion approach, almost... as geekchic suggested use a repeater or Gridview and avoid this kind of mixing of markup and logic as ASP.NET has the advantage of separating markup from code-behind. Commented Oct 27, 2011 at 13:04
  • Looking at some of your comments, it seems you're maybe trying to avoid using ASP.NET controls, have you looked into ASP.NET MVC at all? Commented Oct 27, 2011 at 13:11
  • lol..I know VBscript..ahahaah.. MVC by what I saw is a whole different animal to webform design..Do I readlly need to learn MVC..I mean if i know webforms..why to make the conversion in desing..I also heard that MVC has got performance disadvantages..or other limits..I cant remember Commented Oct 27, 2011 at 13:12

6 Answers 6

3

If you're trying to take a set of data and display it in a table then try using control like the GridView or the Repeater.

Sign up to request clarification or add additional context in comments.

5 Comments

no thank you..dont trust thsoe 100%..I want to program freely from sql controls
@Matrix001 Why reinvent the wheel?
I am just not familiar with every control so much..and I also might want to be more flexible in my design than be limited to every tag that those controls have
@Matrix001 A repeater allows you to have full control over the output. They don't spit out any HTML without you specifically putting it in there. GridViews are very structured.
yeah, It reminds,,..the grid..the headache it caused me with all its tags
2

First and most important thing - do not mix business logic and data access functionality with the data representation markup!

Supposing you are using WebForms, you can use Repeater control which is bound in the code behind of a page/control (aspx.cs/ascx.cs) so View stay decoupled and just bound to specific properties of data source:

ASPX:

<asp:Repeater ID="employees" runat="server">   
<HeaderTemplate>   
<table> 
</HeaderTemplate>   
<ItemTemplate>   
<tr>   
<td>   
   <asp:Label runat="Server" 
              Text="<%# DataBinder.Eval(Container.DataItem, "FirstName") %>"/> 
</td> 
<td>        

   <asp:Label runat="Server" 
              Text="<%# DataBinder.Eval(Container.DataItem, "LastName") %>"/> 
</td>   
</tr>   
</ItemTemplate>   
<FooterTemplate>   
</table>
</FooterTemplate>   
</asp:Repeater>   

Code Behind: (Page_Load() for instance)

employees.DataSource = dataset.Tables["empoloyees"].Rows;
employees.DataBind();

Comments

2

I suggest using a Repeater control if you want a piece of markup to iterate over and bind to it.

   <asp:Repeater id="Repeater1" runat="server">
      <ItemTemplate>
         <tr>
            <td> <%# DataBinder.Eval(Container.DataItem, "FirstName") %> </td>
            <td> <%# DataBinder.Eval(Container.DataItem, "LastName") %> </td>
         </tr>
      </ItemTemplate>
   </asp:Repeater>

Alternatively, use a GridView, though I find that Repeater gives you more control over the emitted markup.

2 Comments

I need to read more abou tthat Databinder..so are you saying that binding is best.. and not what I wrote?
@Matrix001 - Data binding is more idiomatic ASP.NET. You can use foreach in the manner you have, but this is deemed more cluttered (mixing iteration logic with markup).
1

You are almost there. Try this:

<table>

<%
   foreach(DataRow dr in dataset.Tables["empoloyees"].Rows)
   {
%>
<tr>
<td>
      <%= dr[FirstName].toString(); %>
</td>
<td>
      <%= dr[LastName].toString();%>
</td>
</tr>
<%
   }
%>
</table>

1 Comment

hmm..thanks for the syntax... <%= that means simply print on the screen..right? Do people use it by convention or not at all?!?!
1

You should use a repeater instead something like this

 <table>
    <asp:Repeater runat="server" ID="userRepeater" >
        <ItemTemplate>
               <tr>
                  <td><%#DataBinder.Eval(Container.DataItem,"FirstName")%></td>
                  <td><%#DataBinder.Eval(Container.DataItem,"LastName")%></td>
               </tr>
        </ItemTemplate>
    </asp:Repeater>
 </table>

and in your codebehind

userRepeater.DataSource = dataset.Tables["empoloyees"];
userRepeater.DataBind();

for a list of all types of <% %> take a look here

Comments

0

You don't have to use <% %> at all. Just put your code in between tags with runat="server", and then go ahead and use standard ASP.NET tags (e.g. asp:TextBox runat="server"). Just make sure it's an *.aspx page.

FWIW, your question seems a little backwards to me. If you are creating *.aspx pages then you can just use standard HTML anywhere you want. If you are trying to somehow squish ASP.NET into an *.html page - then you are misunderstanding how ASP.NET works.

1 Comment

yeah, I know,,.I know it is not the classic asp.net. But assuming if I wanted to do that..could I ,,that way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.