Reputation: 608
I have unknown number of rows in my dabase with columns id and text. In my code I transform this data into list of object news with parameters id and text.
HTML
<div class="middleDiv" id="mid" runat="server">
</div>
Code:
List<news> news = loadNewsFroamDbs();
String html = "";
for (int i=0;i<news.Count; i++) {
String html1 = " <div class='big'>" +
"<div class='id'>" +
"<label>" + news.id + "</label>" +
"<label>id</label>" +
"</div>" +
"<div class='text'>" +
"<a href='newsDetail.aspx?id=" + news.id + "'>"
+ news.text + "</a>" +
"</div></div>"
html = html + html1;
mid.InnerHtml = html;
}
What component shoud I use when I want on my page something starting my delete() function? I can't use button+onClick method. Do I have to learn something about controllers?
Upvotes: 0
Views: 1357
Reputation: 461
I agree with everyone here and you should use a GridView instead, now take a look at this quick example to see how to use it, you can pretty much format the table to look any way you want (and you dont really have to type all the code yourself VS will do a lot for you)
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
public class News
{
public int id { get; set; }
public string title { get; set; }
public string content { get; set; }
//as many properties as you want
}
public partial class bindGridviewToList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
News n1 = new News() { id = 1, content = "content", title = "title" };
News n2 = new News() { id = 2, content = "content", title = "title" };
News n3 = new News() { id = 3, content = "content", title = "title" };
List<News> newsList = new List<News>();
newsList.Add(n1); newsList.Add(n2); newsList.Add(n3);
GridView1.DataSource = newsList;
GridView1.DataBind();
}
}
Run this sample and you will see how easy it is to use GridView
Upvotes: 1
Reputation: 9399
Melanie is right. You should use a GridView
, if you're going to use the HTML table element.
If you're not going to use a table, then use a DataList. That's probably what you want. If that still doesn't suit you, look up the other data controls you can use (check the left pane on the DataList page).
Edit: Why can't you use buttons for deletion? I don't see a reason not to, and AFAIK all data controls will allow you to use buttons for creation, deletion and edition.
Upvotes: 1