Reputation: 113
I have a table in database named user. The columns are id, name, phone and address
. I put a textbox
as a search box so when the user search using the search box, it will display list of users related. I want to retrieve the list of data in a HTML table
because I want to add an edit button for each line retrieved. I tried this code but it has a problem with ForEach
function. When I separate as ForEach
, it says For must end with Next.
<TABLE style="Z-INDEX: 113; POSITION: absolute; TOP: 368px; LEFT: 592px" id="Table1" border="1" cellSpacing="1" cellPadding="1" width="300">
<% for each(DataRow row in ds.Tables[0].Rows) { %>
<TR>
<td><%= row["columnName"] %></td>
</TR><% } %>
</TABLE>
I already retrieved my data into SQLDataAdapter
with this code:
Dim Adpt As New SqlDataAdapter(cm.CommandText, cnConnect)
Dim ds As New DataSet
Upvotes: 0
Views: 4299
Reputation: 39
Use this code in aspx page(form)
<asp:Repeater ID="rptCustomers" runat="server">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 80px">
Customer Id
</th>
<th scope="col" style="width: 120px">
Customer Name
</th>
<th scope="col" style="width: 100px">
Country
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId") %>' />
</td>
<td>
<asp:Label ID="lblContactName" runat="server" Text='<%# Eval("ContactName") %>' />
</td>
<td>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
and use this code in code behind means aspx.cs page
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindRepeater();
}
}
private void BindRepeater()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Customers", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
rptCustomers.DataSource = dt;
rptCustomers.DataBind();
}
}
}
}
Upvotes: 0
Reputation: 75073
In ASP.NET, you have HTML Controls, such as <asp:panel>
, <asp:button>
, etc... and one of those controls is probably the most powerful (and complex) <asp:GridView>
that allows you to bind data to the control and it will take care of display and editing.
The best way to know it, it's by example, and if you search youtube for asp gridview you will find several... give it a try, so you will be able to do something like:
<asp:GridView ID="my_gv" Runat="server"
DataSourceID="myDataSource" DataKeyNames="id"
AutoGenerateColumns="True"
AllowPaging="True" />
There is plenty things to use with this control, try using the Visual Studio UI, as it will allow you to select some options...
(image from web article)
Upvotes: 2