ASP.NET Tutorial/HTML Controls/Table

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Dynamically create a HTML table (C#)

File:
<%@ Page language="c#" Inherits="TablePictures" CodeFile="Default.aspx.cs" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Table Test</title>
</head>
<body>
  <form runat="server">
  <div>
    Rows:
    <asp:TextBox ID="txtRows" runat="server" />&nbsp;
    Cols:
    <asp:TextBox ID="txtCols" runat="server" />
    <br /><br />
    <asp:CheckBox ID="chkBorder" runat="server"
         Text="Put Border Around Cells" />
    <br /><br />
    <asp:Button ID="cmdCreate" OnClick="cmdCreate_Click" runat="server"
     Text="Create" />
    <br /><br />
    <asp:Table ID="tbl" runat="server" />
  </div>
  </form>
</body>
</html>

File: Default.aspx.cs
using System;
using System.Collections;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class TablePictures : System.Web.UI.Page
{
  protected void Page_Load(object sender, System.EventArgs e)
  {
    tbl.BorderStyle = BorderStyle.Inset;
    tbl.BorderWidth = Unit.Pixel(1);
  }
  protected void cmdCreate_Click(object sender, System.EventArgs e)
  {
    tbl.Controls.Clear();
    int rows = Int32.Parse(txtRows.Text);
    int cols = Int32.Parse(txtCols.Text);
    
    for (int i = 0; i < rows; i++)
    {
      TableRow rowNew = new TableRow();
      tbl.Controls.Add(rowNew);
      for (int j = 0; j < cols; j++)
      {
        TableCell cellNew = new TableCell();
        Label lblNew = new Label();
        lblNew.Text = "(" + i.ToString() + "," + j.ToString() + ")<br />";
        System.Web.UI.WebControls.Image imgNew = new System.Web.UI.WebControls.Image();
        imgNew.ImageUrl = "cellpic.png";
        cellNew.Controls.Add(lblNew);
        cellNew.Controls.Add(imgNew);
        if (chkBorder.Checked == true)
        {
          cellNew.BorderStyle = BorderStyle.Inset;
            cellNew.BorderWidth = Unit.Pixel(1);
          }
        rowNew.Controls.Add(cellNew);
      }
    }
  }
}


Dynamic html table

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="DynamicTable" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DynamicTable : System.Web.UI.Page
{
     protected void Page_Load(object sender, System.EventArgs e)
  {
    HtmlTable table1 = new HtmlTable();
    table1.Border = 1;
    table1.CellPadding = 3;
    table1.CellSpacing = 3;
    table1.BorderColor = "red";
    HtmlTableRow row;
    HtmlTableCell cell;
    for (int i = 1; i <= 5; i++)
    {
      row = new HtmlTableRow();
      row.BgColor = (i % 2 == 0 ? "lightyellow" : "lightcyan");
      for (int j = 1; j <= 4; j++)
      {
        cell = new HtmlTableCell();
        cell.InnerHtml = "Row: " + i.ToString() + "<br>Cell: " + j.ToString();
        row.Cells.Add(cell);
      }
      table1.Rows.Add(row);
    }
    this.Controls.Add(table1);
  }
}


InnerHtml

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>BulletedList Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h1>BulletedList Control</h1>
    
     <asp:BulletedList ID="bltList" runat="server" 
          OnClick="bltList_Click" 
          Target="_blank">
      <asp:ListItem Value="http://www.nfex.ru/">nfex.ru</asp:ListItem>
      <asp:ListItem Value="http://www.nfex.ru">nfex.ru</asp:ListItem>
      <asp:ListItem Value="http://www.nfex.ru" Text="nfex"></asp:ListItem>
     </asp:BulletedList>
    
     <table>
      <tr>
        <td colspan=3 id="tdMessage" runat="server">
        </td>
      </tr>
       <tr>
         <td>
          <u>BulletStyle</u>
         </td>
         <td>
          <u>FirstBulletNumber</u>
         </td>
         <td>
          <u>DisplayMode</u>
         </td>
       </tr>
       <tr>
         <td>
          <asp:ListBox ID="lbBulletStyle" runat="server" AutoPostBack=true OnSelectedIndexChanged="lb_SelectedIndexChanged">
            <asp:ListItem>NotSet</asp:ListItem>
            <asp:ListItem>Numbered</asp:ListItem>
            <asp:ListItem>LowerAlpha</asp:ListItem>
            <asp:ListItem>UpperAlpha</asp:ListItem>
            <asp:ListItem>LowerRoman</asp:ListItem>
            <asp:ListItem>UpperRoman</asp:ListItem>
            <asp:ListItem>Disc</asp:ListItem>
            <asp:ListItem>Circle</asp:ListItem>
            <asp:ListItem>Square</asp:ListItem>
            <asp:ListItem>CustomImage</asp:ListItem>
          </asp:ListBox>

         </td>
         <td>
          <asp:ListBox ID="lbFirstBulletNumber" runat="server" 
                AutoPostBack=true 
                Width=50
                OnSelectedIndexChanged="lb_SelectedIndexChanged">
            <asp:ListItem Selected="True">1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
            <asp:ListItem>4</asp:ListItem>
            <asp:ListItem>5</asp:ListItem>
            <asp:ListItem>6</asp:ListItem>
          </asp:ListBox>
         </td>
         <td>
            <asp:ListBox ID="lbDisplayMode" runat="server" AutoPostBack=true OnSelectedIndexChanged="lb_SelectedIndexChanged">
              <asp:ListItem>NotSet</asp:ListItem>
              <asp:ListItem>Text</asp:ListItem>
              <asp:ListItem>HyperLink</asp:ListItem>
              <asp:ListItem>LinkButton</asp:ListItem>
            </asp:ListBox>
         </td>
       </tr>
     </table>
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page 
{
    protected void lb_SelectedIndexChanged(object sender, EventArgs e){
     ListBox lb = (ListBox)sender;
     string strID = lb.ID;
     string strValue = lb.SelectedValue;
     switch (strID){
       case "lbBulletStyle": BulletStyle style = (BulletStyle)Enum.Parse(typeof(BulletStyle), strValue);
             bltList.BulletStyle = style;
             if (style == BulletStyle.CustomImage)
             {
                bltList.BulletImageUrl = "A.bmp";
             }
             break;
       case "lbFirstBulletNumber":
         bltList.FirstBulletNumber = Convert.ToInt32(strValue);
         break;
       case "lbDisplayMode":
             BulletedListDisplayMode displayMode = (BulletedListDisplayMode)Enum.Parse(typeof(BulletedListDisplayMode), strValue);
             bltList.DisplayMode = displayMode;
             break;
          default:
             break;
     }
   }
  protected void bltList_Click(object sender, BulletedListEventArgs e){
     BulletedList b = (BulletedList)sender;
     tdMessage.InnerHtml = "Selected index: " + e.Index.ToString() +
        "<br>" +
        "Selected value: " + b.Items[e.Index].Value +
        "<br>";
  }
 }