In a datalist or repeater control you create an templates for header, footer and items in the datalist. The example here creates a header template and a footer template and obviously starts a html table and adds rows for each item. The item template is applied for each row in the datasource assigned to the datalist in code. The dataitem specified - "name" must be a field name in the datasource. If my datasource had name, address, email for example I could add code for that too, ><%# databinder.eval(container.dataitem, "email") %>, for example.
Code:
<table>
<asp
ataList ID="testDL" Runat="server">
<HeaderTemplate>
<tr><td><strong>My Header</strong></td></tr>
</HeaderTemplate>
<ItemTemplate>
<tr><td><strong><%# databinder.eval(container.dataitem, "name") %></strong></td></tr>
</ItemTemplate>
</asp
ataList>
</table>
<asp:literal id="errormsg" runat="server" />
Now in my code I need to create a datasource and my favorite way of doing that is with the datareader because it's nice and quick:-
Code:
dim conn as new sqlclient.sqlconnection(myConnString)
dim cmd as new sqlclient.sqlcommand()
dim dr as sqlclient.sqldatareader
try
conn.open
cmd.connection=conn
cmd.commandtype = CommandType.StoredProcedure
cmd.commandtext = "up_MyStoredProced" 'which returns a name, address, email set of records for example
dr = cmd.executereader
testDL.datasource = dr
testDL.databind
dr.close
catch ex as exception
errormsg.text = ex.message
finally
conn.close
end try
Blinking 'eck even in code tags it create a green smiley - come on James
Enter your message below
Sign in or Join us (it's free).