i'm just learning asp.net with c#. with the little knowledge of php, i think i'm able to do somethings but i'm having problem displaying records from my database.
i have a table called resources, what i want to do is output all the data in the table. this is the output i want to write in c# in php...
<?php
while($row = mysql_fetch_object($tablename))
{
echo "<tr><td>$row->image</td><td><a href='#'>download link</a></td><a href='http://'".$row->url."'>'".$row->url."'</a></tr>";
}
?>
basically i don't have problem with the php but i want the output this code will give me same in C#(with image, download link, and url in a table). Please help,...
i have this code in c# to output data in a label control
var cmd = new MySqlCommand("SELECT * FROM resources");
Knct dal = new Knct();
MySqlDataReader dr = dal.DatareaderMethod(cmd, Knct.executemethods.ExecuteReader);
while (dr.Read())
{
label1.Text = dr[2].ToString();
}
the while loop is not working, it only display one value. how do i make this a display all records in the database and which asp control can i use to output the records and also make a url link in the table output.
If you're using ASP.Net, a repeater is preferred for simple displays.
In your code behind (or server-side), you'll have to bind the data to the repeater:
...
m_Repeater.DataSource = GetImages(); //Function that returns Image records
m_Repeater.DataBind();
...
The mark up on your ASPX page will look something like:
<asp:Repeater id="m_Repeater" runat="server">
<HeaderTemplate>
<table>
<tr>
<td>Image</td>
<td>Link</td>
<td>Url</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><img src='<%# Container.DataItem("ImageSrc") %>'/></td>
<td><a href='<%# Container.DataItem("DownloandLink") %>'>Download Link</a></td>
<td><a href='<%# Container.DataItem("ImageUrl") %>'>Url</a></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
ASP.Net MVC
If you're using ASP.Net MVC you can use the Razor syntax, it's more elegant. Assuming Model
is strongly-typed to an enumerable list of Image
objects, the markup on your view page might look something like:
@foreach(var image in Model)
{
<tr>
<td><img src='@image.ImageSrc'/></td>
<td><a href='@image.DownloadLink'>Download Link</a></td>
<td><a href='@image.ImageUrl'>Url</a></td>
</tr>
}
Some useful Articles/Resources:
If this is webforms, you can use a repeater to display each of your values. It'd look something like this:
aspx:
<asp:Repeater ID="repeater" runat="server">
<ItemTemplate>
<div><%# Eval("columnName")%></div>
</ItemTemplate>
</asp:Repeater>
aspx.cs:
repeater.DataSource = dr;
repeater.DataBind();
Think of it as a for
loop.
Use like this
<asp:GridView ID="grdPivot" runat="server" AutoGenerateColumns="True">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MySql %>" ProviderName="<%$ ConnectionStrings:MySql.ProviderName %>" SelectCommand="SELECT * FROM resources"></asp:SqlDataSource>
This is the simplest way to bind data to a gridview or any other data control.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.