Click here to Skip to main content
15,914,109 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi...
I am new to ASP.NET. And I am trying to create a dynamic table and delete a particular row.
I am creating buttons and when user click the button the corresponding row will be deleted.
I have done the adding buttons and adding the button's function. but i am confused what to write to delete the row for clicking a particular button...

asptable.aspx

<
XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        Rows:
        <asp:TextBox ID="tbxRows" runat="server" Width="60px"></asp:TextBox>
        <br />
        Columns:
        <asp:TextBox ID="tbxColumns" runat="server" Width="53px"></asp:TextBox>
        <br />
        <asp:Button ID="btnCreateTable" runat="server" onclick="btnCreateTable_Click"
            Text="Create Table" />
        <br />
        <asp:Table ID="tblDynamicTable" runat="server" Caption="Dynamic Table">
        </asp:Table>

    </div>
    </form>
</body>
</html>
&gt;



asptable.cs

public partial class ASPTable : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnCreateTable_Click(object sender, EventArgs e)
{
for (int i = 0; i < Convert.ToInt16(tbxRows.Text); i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < Convert.ToInt16(tbxColumns.Text); j++)
{
TableCell tcID = new TableCell();
TableCell tcValue = new TableCell();
TableCell tcButton = new TableCell();
tcID.Text = (i%7).ToString();
tcValue.Text = Enum.GetNames(typeof( DayOfWeek))[i%7];
Button btnDeleteButton = new Button();
btnDeleteButton.Text = "Delete"+(i%7).ToString()+j.ToString();
btnDeleteButton.ID = "btnDelete" + i.ToString() + j.ToString();
btnDeleteButton.Click += new EventHandler(btnDeleteButton_Click);
tcButton.Controls.Add(btnDeleteButton);
tr.Cells.Add(tcID);
tr.Cells.Add(tcValue);
tr.Cells.Add(tcButton);
}
tblDynamicTable.Rows.Add(tr);
}
}

void btnDeleteButton_Click(object sender, EventArgs e)
{

//throw new NotImplementedException();

}
}
Posted

How it is possible that ur creating table dynamical and want to delete row from .cs. file.If u try to do so it is not possible because postback is occur and ur table will disappear.Because of that u have to use javascript try following code..
protected void btnCreateTable_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Convert.ToInt16(tbxRows.Text); i++)
        {
            TableRow tr = new TableRow();
            for (int j = 0; j < Convert.ToInt16(tbxColumns.Text); j++)
            {
                TableCell tcID = new TableCell();
                TableCell tcValue = new TableCell();
                TableCell tcButton = new TableCell();
                tcID.Text = (i % 7).ToString();
                tcValue.Text = Enum.GetNames(typeof(DayOfWeek))[i % 7];
                Button btnDeleteButton = new Button();
                btnDeleteButton.Text = "Delete" + (i % 7).ToString() + j.ToString();
                btnDeleteButton.ID = "btnDelete" + i.ToString() + j.ToString();
               // btnDeleteButton.Click += new EventHandler(btnDeleteButton_Click);
                btnDeleteButton.Attributes.Add("onclick", "DeleteRow("+i.ToString()+");");
                tcButton.Controls.Add(btnDeleteButton);
                tr.Cells.Add(tcID);
                tr.Cells.Add(tcValue);
                tr.Cells.Add(tcButton);
            }
            tblDynamicTable.Rows.Add(tr);
        }
    }

XML
<script type="text/javascript">
    function DeleteRow(rowDel)
{
var dyTable=document.getElementById("tblDynamicTable");
dyTable.deleteRow(rowDel);
}
    </script>
 
Share this answer
 
i need a solution very badly..pls help me
 
Share this answer
 
Comments
Richard C Bishop 23-Apr-13 10:45am    
Be careful not to post comments or questions as solutions to your own thread.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900