Jump to content
chell515

Adicionar uma nova linha com o evento OnClick

Recommended Posts

chell515

Bom dia,

Tenho andado às voltas com uma dúvida que não sei como resolver.

Tou a criar uma página ASP e preciso de ir inserindo dados numa tabela e depois clicar num botão e adicionar uma nova linha à tabela mas não o estou a conseguir fazer.

Será que alguém me pode ajudar?

Deixo aqui o código da página e o Code Behind.

Código da página

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RulesEngineControl.ascx.cs"
    Inherits="RepositorioForms.RulesEngine.RulesEngineControl" %>

<updatepanel>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<table border="1" id="RF" runat="server">
<tr>
    <td>Campo</td>
    <td>Operação</td>
    <td>Valor</td>
    <td>Op. Lógico</td>
    <td></td>
</tr>
<updatepanel>
<tr>
    <td>
        <asp:DropDownList ID="campo" runat="server">
            <asp:ListItem Selected="True" Text="Sel. Campo"></asp:ListItem>
            <asp:ListItem Text="Número Cliente"></asp:ListItem>
            <asp:ListItem Text="Total Pagar"></asp:ListItem>
        </asp:DropDownList>
    </td>
    <td>
        <asp:DropDownList ID="Operacao" runat="server">
            <asp:ListItem Selected="True" Text="Sel. Operação"></asp:ListItem>
            <asp:ListItem Text="="></asp:ListItem>
            <asp:listItem Text=">"></asp:listItem>
            <asp:ListItem Text="<"></asp:ListItem>   
        </asp:DropDownList>
    </td>
    <td>
        <asp:TextBox ID="Valor" runat="server"></asp:TextBox>
    </td>
    <td>
        <asp:DropDownList ID="OpLogico" runat="server">
            <asp:ListItem Selected="True" Text="Sel. Op. Lógico"></asp:ListItem>
            <asp:ListItem Text="E"></asp:ListItem>
            <asp:ListItem Text="OU"></asp:ListItem>
        </asp:DropDownList>
    </td>
<td></td>
</tr>
</updatepanel>
</table>
</updatepanel>
<updatepanel>
<asp:Table BorderWidth="1" ID="tblBtn" runat="server">
    <asp:TableRow>
        <asp:TableCell>
            <asp:Button ID="btnAdd" Text="Adicionar" OnClick="AddLine_Click" runat="server"/>
        </asp:TableCell>
        <asp:TableCell>
            <asp:Button ID="btnSubm" Text="Submeter" runat="server"/>
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>
</updatepanel>

Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RepositorioForms.RulesEngine
{
    public partial class RulesEngineControl : System.Web.UI.UserControl
    {
        private int numOfRows = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GenerateTable(numOfRows);
            }
        }

        protected void AddLine_Click(object sender, EventArgs e)
        {
            if (ViewState["RowsCount"] != null)
            {
                numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
                GenerateTable(numOfRows);
            }
        }

        protected void Submit_Click(object sender, EventArgs e)
        {

        }

        private void SetPreviousData(int rowsCount, int colsCount)
        {
            Table table = (Table)Page.FindControl("RF");
            if (table != null)
            {
                for (int i = 0; i < rowsCount; i++)
                {
                    for (int j = 0; j < colsCount; j++)
                    {
                        TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);
                        tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];
                    }
                }
            }
        }

        private void GenerateTable(int rowsCount)
        {
            Table table = new Table();
            table.ID = "TableAdd";
            Page.Controls.Add(table);

            const int colsCount = 4;

            for (int i = 0; i < rowsCount; i++)
            {
                TableRow row = new TableRow();
                for (int j = 0; j < colsCount; j++)
                {
                    TableCell cell = new TableCell();
                    TextBox tb = new TextBox();

                    tb.ID = "TextBoxRow_" + i + "Col_" + j;
                    cell.Controls.Add(tb);
                    row.Cells.Add(cell);

                    table.Rows.Add(row);
                }

                SetPreviousData(rowsCount, colsCount);
                rowsCount++;
                ViewState["RowsCount"] = rowsCount;
            }
        }
    }
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.