简体   繁体   English

无法使用ASP / C#动态向一个表添加多行

[英]Cannot add more than one row to a table dynamically using ASP/C#

I have a web app with HTML tables containing input boxes everywhere in it. 我有一个带有HTML表的Web应用程序,其中包含无处不在的输入框。 I want to be able to add rows to these tables from the C# side of things. 我希望能够从C#方面向这些表添加行。 To accomplish this, I use an asp button that calls this method: 为此,我使用一个调用此方法的asp按钮:

private void AddRow()
{
    HtmlTableRow tRow = new HtmlTableRow();

    HtmlTableCell cell = new HtmlTableCell();
    HtmlTableCell cell2 = new HtmlTableCell();
    HtmlTableCell cell3 = new HtmlTableCell();
    HtmlTableCell cell4 = new HtmlTableCell();

    cell.InnerHtml = "<input type=\"text\" size=\"29\">";
    tRow.Cells.Add(cell);

    cell2.InnerHtml = "<input type=\"text\" size=\"9\" class=\"units commas\" />";
    cell2.Attributes.Add("class", "leftBorder textCenter");
    tRow.Cells.Add(cell2);

    cell3.InnerHtml = "<input type=\"text\" size=\"8\" class=\"value commas\" />";
    cell3.Attributes.Add("class", "leftBorder textCenter");
    tRow.Cells.Add(cell3);

    cell4.InnerHtml = "<input type=\"text\" size=\"11\" readonly=\"readonly\" tabindex=\"-1\" class=\"totalA1 autoTotal\" />&nbsp;";
    cell4.Attributes.Add("class", "leftBorder rightBorder textRight");
    tRow.Cells.Add(cell4);

    someTable.Rows.Add(tRow);
}

This works beautifully...for exactly one row. 这很漂亮...仅适用于一行。 I can click the button, it adds a row. 我可以单击按钮,它会添加一行。 If I click it again, it doesn't do anything. 如果我再次单击它,它什么也没做。 More specifically, I suspect it's removing the currently added row, restoring the document to the 'default' state, and then adds a row (effectively doing nothing). 更具体地说,我怀疑它正在删除当前添加的行,将文档还原为“默认”状态,然后添加一行(实际上不执行任何操作)。

Assuming I'm right, I need to somehow be able to append a row to another dynamically created row, instead of just replace it. 假设我是对的,我需要以某种方式能够将一行添加到另一个动态创建的行,而不是仅仅替换它。 If I'm not right, I just need a means to be able to continually add rows on a button press. 如果我做错了,我只需要一种方法就可以在按下按钮时连续添加行。

How would I go about doing this? 我将如何去做呢?

EDIT: I should specify, all this could be done in a loop, all at once. 编辑:我应该指定,所有这些都可以在循环中一次完成。 I was hoping to get it to work on a button press just for the sake of testing, but it can all be neatly tucked into a loop of some kind. 我只是为了测试而希望它能够在按钮按下时起作用,但是可以将它们整齐地塞进某种循环中。 I've had (some) success dropping it in one. 我已经成功完成了一些尝试。

The absolute best way to do what you need is client side, NOT server side. 做您所需的绝对最佳方法是客户端,而不是服务器端。 For what you're doing, it makes no sense to post back to the server EACH TIME you insert a new row (in my opinion). 对于您正在执行的操作,每次插入新行时,回传到服务器都是没有意义的(我认为)。 Is there a specific reason you must do this server side (C#)? 您是否有特定原因必须执行此服务器端(C#)?

Use Javascript (jQuery) to dynamically insert new rows into your table upon the button click. 单击按钮后,使用Javascript(jQuery)将新行动态插入表中。 The page won't refresh, it's faster, and makes for a LOT better user experience. 该页面不会刷新,速度更快,并且可以为用户带来更好的体验。

I assume you're familiar with Javascript, but you can tie a client side event to your server side button - just add onclientclick="insertRow();" 我假设您熟悉Javascript,但是您可以将客户端事件与服务器端按钮相关联-只需添加onclientclick="insertRow();" where onclientclick is an attribute of the button control and insertRow() is a Javascript method defined in your page between <script></script> tags. 其中onclientclick是按钮控件的属性, insertRow()是页面中<script></script>标记之间定义的Javascript方法。

If you need me to write out an example on how to do this, please let me know and I can edit my answer. 如果您需要我写出执行此操作的示例,请让我知道,我可以编辑答案。

The sentiments expressed by the other answers is completely correct in that this architecture is very flawed (it does things the way they were done in the mid 2000's, a lifetime ago in web). 其他答案所表达的观点是完全正确的,因为该体系结构非常有缺陷(它的工作方式与2000年代中期(网络中的一生以前)一样)。 That being said, you have constraints and you can't change them. 话虽这么说,但您有约束条件,无法更改。 Here are two things I would check, one of which was already suggested in a comment: 这是我要检查的两件事,其中之一已经在注释中建议:

  1. Postback is interfering with table initialization. 回发会干扰表初始化。 When a visitor first loads your aspx page, the browser will perform an HTTP GET request to your page. 访客首次加载您的aspx页面时,浏览器将对您的页面执行HTTP GET请求。 The IsPostback property of your aspx page class will be false. aspx页面类的IsPostback属性为false。 When the user clicks your button, they will make a POST request to your aspx page, passing along a bunch of variables for the current state of the page if they have modified it on their browser as well as a .NET-specific set of properties indicating what was pressed and what event handler the server should execute (in your case, the event handler calling AddRow will be called, but only after Page_Load is executed first). 当用户单击您的按钮时,他们将向您的aspx页面发出POST请求,并传递一堆用于页面当前状态的变量(如果他们在浏览器中进行了修改)以及.NET特定的属性集指示被按下的内容以及服务器应执行的事件处理程序(在您的情况下,将调用调用AddRow的事件处理程序,但仅在首先执行Page_Load之后)。 This is why they suggested wrapping your Page_Load logic in an if(!IsPostback){}. 这就是为什么他们建议将您的Page_Load逻辑包装在if(!IsPostback){}中。

  2. ViewState is not enabled for the HtmlTable control. HtmlTable控件未启用ViewState。 The ViewState is the .NET-specific implementation of serializing all that the server knows about the HTML (input boxes, etc.) into a hidden field in your output HTML. ViewState是特定于.NET的实现,用于将服务器所知的有关HTML的所有信息(输入框等)序列化为输出HTML中的隐藏字段。 If you want .NET to remember what the state of various HTML tags are (what they contain, what the user filled out in each, etc.), then they need to have an entry in the ViewState, which is passed from postback to postback. 如果您希望.NET记住各种HTML标记的状态(它们包含的内容,用户在每个HTML标记中填写的内容等),则它们需要在ViewState中具有一个条目,该条目从回发传递到回发。 Be warned, though, as soon as someone refreshes a page without a button click or using back and forward buttons on the browser (because remember they correspond to HTTP GET requests, not HTTP POST requests) the viewstate will be reinitialized anyways. 但是请注意,只要有人刷新页面时没有单击按钮或使用浏览器上的后退和前进按钮(因为记住它们对应于HTTP GET请求,而不是HTTP POST请求),无论如何都会重新初始化viewstate。 The only way around this is stuffing stuff into the Session. 解决此问题的唯一方法是将内容塞入Session。 If you're ok with that, then to enable the viewstate, use the .EnableViewState() method on the HtmlTable control, although keep in mind this will increase your web app's page size since .NET will serialize what the table contains into a string and put it in a hidden input variable. 如果可以,则要启用viewstate,请在HtmlTable控件上使用.EnableViewState()方法,尽管请记住,这将增加Web应用程序的页面大小,因为.NET会将表中包含的内容序列化为字符串并将其放在隐藏的输入变量中。 This will make it "easy" to write all the server side logic, but at a huge cost if the table becomes very big. 这将使编写所有服务器端逻辑变得“容易”,但是如果表很大,则会付出巨大的代价。

It may seem right now that the path of least resistance is to just make the existing infrastructure work, but believe me you are incurring ever increasing amounts of technical debt that either you or someone else will have to pay up down the line. 现在看来,阻力最小的途径就是使现有基础架构正常工作,但是相信我,您承担的技术债务越来越多,您或其他任何人都必须为此支付费用。 I would highly recommend moving to a client-side add row method, it's the only way around this postback-viewstate-model madness in webforms. 我强烈建议移至客户端添加行方法,这是解决Webforms中这种postback-viewstate-model疯狂的唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM