Skip to content

Контрола Table

HTML табела

        <h2>HTML tabela</h2>
        <table>
          <tr>
            <td>Red 0, Celija 0</td>
            <td>Red 0, Celija 0</td>
            <td>Red 0, Celija 0</td>
          </tr>
          <tr>
            <td>Red 0, Celija 0</td>
            <td>Red 0, Celija 0</td>
            <td>Red 0, Celija 0</td>
          </tr>
        </table> 

Табела у aspx

        <asp:Table ID="tbl1" runat="server" GridLines="Both">  
            <asp:TableRow runat="server">  
                <asp:TableCell runat="server">Red 0, Celija 0</asp:TableCell>  
                <asp:TableCell runat="server">Red 0, Celija 1</asp:TableCell>  
                <asp:TableCell runat="server">Red 0, Celija 2</asp:TableCell>  
            </asp:TableRow>  
            <asp:TableRow runat="server">  
                <asp:TableCell runat="server">Red 1, Celija 0</asp:TableCell>  
                <asp:TableCell runat="server">Red 1, Celija 1</asp:TableCell>  
                <asp:TableCell runat="server">Red 1, Celija 2</asp:TableCell>  
            </asp:TableRow>  
        </asp:Table>

Табела у aspx.cs

Дефиниција у aspx:

<asp:Table ID="tbl2" runat="server" GridLines="Both"></asp:Table>

Креирање табеле у aspx.cs:

        protected void Page_Load(object sender, EventArgs e)
        {
            int brRedova = 2;
            int brCelija = 3;
            for (int j = 0; j < brRedova; j++)
            {
                TableRow r = new TableRow();
                for (int i = 0; i < brCelija; i++)
                {
                    TableCell c = new TableCell();
                    c.Text = "Red " + j.ToString() + ", Celija " + i.ToString();
                    r.Cells.Add(c);
                }
                tbl2.Rows.Add(r);
            }
        }

Задаци

  1. Креирај веб апликацију која из дефинисаног низа оцена у табели приказује низ оцена, а испод табеле просечну оцену.

.ASPX

        <h2>Moje ocene:</h2>
        <asp:Table ID="tblOcene" runat="server" GridLines="Both"></asp:Table>
        <p>Prosek ocena je: <asp:Label ID="lblProsek" runat="server"></asp:Label></p>

.ASPX.CS

        protected void Page_Load(object sender, EventArgs e)
        {
            int[] ocene = { 5, 4, 3, 4, 5, 5, 4, 3, 4, 5, 4, 3, 5  };
            
            TableRow r0 = new TableRow(); // Prvi red u tabeli
            for (int i = 0; i < ocene.Length; i++) // Celije u prvom redu
            {
                TableCell c = new TableCell();
                c.Text = "Ocena " + (i + 1).ToString();
                r0.Cells.Add(c);
            }
            tblOcene.Rows.Add(r0);
            
            TableRow r1 = new TableRow(); // Drugi red u tabeli
            for (int i = 0; i < ocene.Length; i++) // Celije u drugom redu
            {
                TableCell c = new TableCell();
                c.Text = ocene[i].ToString();
                r1.Cells.Add(c);
            }
            tblOcene.Rows.Add(r1);

            int suma = 0;
            for (int i = 0; i <10; i++)
                suma += ocene[i];
            double prosek = (double)suma / ocene.Length;
            lblProsek.Text = prosek.ToString("0.00");
        }