user3181941
user3181941

Reputation: 55

How to change the value of a cell in an ASP.NET table using C# code behind

I want to be able to use the code behind to set the value of a cell in a table

<asp:Table ID="Table1" runat="server" CellPadding="10"
GridLines="Both" HorizontalAlign="Center" BackColor="Cornsilk">
<asp:TableRow ID="Row1">
<asp:TableCell ID="Cell1"></asp:TableCell>

Is this possible?

Upvotes: 2

Views: 10293

Answers (2)

Pragnesh Khalas
Pragnesh Khalas

Reputation: 2898

Below code will help you to set the value in table cell dynamically

for (int i = 0; i <= this.Table1.Rows.Count - 1; i++)
    {
        TableCell tc = this.Table1.Rows[i].FindControl("Cell1") as TableCell;
        if (tc != null)
        {
            tc.Text = "New Value";
        }
    }

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460268

Make it runat=server:

<asp:TableCell runat="server" ID="Cell1"></asp:TableCell>

and you can access it from codebehind directly:

Cell1.Text = "New Value"; 

otherwise you can access it via Rows and Cells of the table:

Table1.Rows[0].Cells[0].Text = "New Value";

Upvotes: 5

Related Questions