Satır ve Sütun Sayısına Göre Tablo Oluşturma
Asp.net table kontrolü ile kullanıcının girmiş olduğu satır ve sütun sayısına göre otomatik olarak tablo oluşturan bir örnek yapalım ilk olarak sayfamıza iki buton, iki textbox ve bir table nesnesi yerleştiriyoruz kodlarımız..
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="tabloolustur.aspx.cs" Inherits="aspnetornekleri.tablooluştur" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h2 style="color:Green">Satır ve Sütün Sayısı ile Tablo Oluşturma</h2> <asp:Label ID="Label1" runat="server" Font-Bold="true" ForeColor="HotPink" Text="Satır Sayısı" > </asp:Label> <asp:TextBox ID="TextBox1" runat="server" BackColor="HotPink" ForeColor="FloralWhite" > </asp:TextBox> <br /> <asp:Label ID="Label2" runat="server" Font-Bold="true" ForeColor="HotPink" Text="Sütun Sayısı" > </asp:Label> <asp:TextBox ID="TextBox2" runat="server" BackColor="HotPink" ForeColor="FloralWhite" > </asp:TextBox> <br /><br /> <asp:Button ID="Button1" runat="server" Font-Bold="true" ForeColor="HotPink" OnClick="Button1_Click" Text="Tablo Oluştur" /> <br /><br /> <asp:Table ID="Table1" runat="server" BorderWidth="1" BorderColor="DodgerBlue" > </asp:Table> </div> </form> </body> </html>
İşlem esnasında girilen satır ve sütun değerleri alınıyor buna göre yeni hücreler eklenerek tablo oluşturuluyor
protected void Button1_Click(object sender, System.EventArgs e) { int rows = Int32.Parse(TextBox1.Text); int columns = Int32.Parse(TextBox2.Text); for (int row = 0; row < rows; row++) { TableRow newRow = new TableRow(); Table1.Controls.Add(newRow); for (int column = 0; column < columns; column++) { TableCell newCell = new TableCell(); newCell.Text = " Satır " + row.ToString(); newCell.Text += "; Sütun" + column.ToString(); newCell.BorderStyle = BorderStyle.Solid; newCell.BorderWidth = Unit.Pixel(1); newCell.BorderColor = System.Drawing.Color.DodgerBlue; newRow.Controls.Add(newCell); } } }