Row data cannot be retrieved correctly in ASP.NET HtmlTable.

Asked 1 years ago, Updated 1 years ago, 50 views

There are actually two lines in the table, but I can only read one line from the HtmlTable control.

First, the aspx file has a table.

<table runat="server" id="datatable" border="1">
        <tbody>
        <tr>
            <td>Name</td> Age>/td> Department>/td>Notes>/td>
        </tr>
        </tbody>
    </table>

When loading the page, the server will add the data as follows:

HtmlTableRow=newHtmlTableRow();
// store data in row
datatable.Rows.Add(row);

The data is now displayed in two lines.

But at POST,

 intn=datatable.Rows.Count;

If you get the number of lines in , you get a 1,

c# .net asp.net

2022-09-30 16:55

1 Answers

It probably follows ASP.NET's specifications.If you add HtmlTableRow in code behind, the previous data is not automatically restored.

The reason for this is that the information that the web browser sends to the server is limited to certain elements such as input.Therefore, in ASP.NET, in order to save the WebControl properties that output table and span, we output information called view state to restore properties in the early stages of the next request.However, the HtmlTable is not intended for such behavior, so you do not restore rows from the view state.

A possible response is

    Using more advanced web controls such as
  • GridView
  • Get the information to output to the HtmlTable on each request
  • Restore the HtmlTable state by entering and leaving the required information into and out of the ViewState yourself

and so on.


2022-09-30 16:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.