简体   繁体   English

向位置x的动态ASP.NET表控件中添加新行-回发后Viewstate的问题

[英]Adding a new row to a dynamic ASP.NET Table Control, at position x - Problems with Viewstate after postback

I am creating a dynamic table control with ASP.NET/C# and the table is being used to hold a form. 我正在使用ASP.NET/C#创建动态表控件,并且该表用于保存表单。 I need to allow users to click on a button in a row and make a duplicate of that row right below the original. 我需要允许用户单击一行中的按钮,然后在原始行的正下方复制该行。 My page is also using AJAX and jQuery. 我的页面也使用AJAX和jQuery。

I have tried to use Table.Rows.AddAt(indexoforigrow+1, newrow) but I get an error that the index is out of the range of values. 我尝试使用Table.Rows.AddAt(indexoforigrow + 1,newrow),但出现错误,表明索引超出了值的范围。 I figured maybe I wasn't getting the original row's index correctly so I just tossed in a 1 and still got the error. 我想也许我没有正确地获得原始行的索引,所以我只是扔了一个1仍然出现错误。

I also noticed that when I press the button to execute the code, the table disappears. 我还注意到,当我按下按钮执行代码时,表格消失了。 Any ideas on how to fix that? 关于如何解决该问题的任何想法?

I am thinking that if I am unable to get these issues fixed I will have to loop through the table and submit the data to a temp table, adding a new row where indicated. 我在想,如果无法解决这些问题,则必须遍历该表并将数据提交到临时表,并在指示的位置添加新行。 Then I would pull all of the data back out and display again. 然后,我将拉回所有数据并再次显示。

EDIT 编辑

I moved on when I could not get this working and tried to setup my submit functions to loop through the data and submit it to a db and realized that I am experiencing the same issues when clicking the submit button as when I click the add row button. 当我无法正常工作时,我继续前进,尝试设置提交功能以遍历数据并将其提交给数据库,并意识到单击提交按钮时与单击添加行按钮时遇到相同的问题。 It appears that my issue is really with viewstates/postback. 看来我的问题确实与viewstates / postback有关。

I did some reading on this and from what I can tell the solution is to re-create the dynamic control on page load every time. 我对此进行了一些阅读,从中可以看出解决方案是每次都在页面加载时重新创建动态控件。 But I am confused about how I can do this if I have no idea how many rows/cells I have and how is the information the user entered kept in the form? 但是,如果我不知道我拥有多少行/单元格,以及如何将用户输入的信息保存在表格中,我会感到困惑。 I have no way of saving the information to a DB or anything because as soon as submit is clicked it all disappears. 我无法将信息保存到数据库或其他任何东西,因为单击提交后,所有信息都会消失。

Have you considered using a different control? 您是否考虑过使用其他控件? One of the grid controls might better serve your purpose. 网格控件之一可能会更好地满足您的目的。

I've handled situations (that sound) similar to what you're describing by using the footer of a GridView (among other ways). 我已经通过使用GridView的页脚(其他方式)来处理与您所描述的情况类似的情况(听起来)。 The big trick is to bind the display object (Table, GridView, etc.) to a list of objects. 最大的技巧是将显示对象(表,GridView等)绑定到对象列表。 Then you manipulate the list of objects, rebinding the display object after the list has changed. 然后,您可以操作对象列表,在列表更改后重新绑定显示对象。

You will need to persist this list in some manner, or you could wind up with the list being reset to null. 您将需要以某种方式保留此列表,或者最终将列表重置为null。 One way would be to save it as part of the session. 一种方法是将其保存为会话的一部分。

At any rate, the idea is to have a display object (GridView) bound to a list of objects. 无论如何,我们的想法是将显示对象(GridView)绑定到对象列表。 When the user clicks 'add', an item is added to the list. 当用户单击“添加”时,一个项目将添加到列表中。 Save the list, then call the GridView.DataBind method. 保存列表,然后调用GridView.DataBind方法。

You are on the right track with re-creating the dynamic table each time the page loads. 您每次进入页面时重新创建动态表的方向都正确。 I have dome similar things and my solution was to keep a client state in a hidden field on the page. 我有类似的东西,我的解决方案是将客户端状态保留在页面上的隐藏字段中。 Every time you add a row to the table on the client, increment a counter or adjust the state data in the hidden field. 每次在客户端上的表中添加一行时,增加一个计数器或调整隐藏字段中的状态数据。 Then, on the server-when the page posts back, read from that hidden field to know how many rows were added on the client. 然后,在服务器上,当页面回发时,请从该隐藏字段中读取信息,以了解在客户端上添加了多少行。 You will have to use some kind of naming convention for the new rows you add(and the widgets in them) so you can re-create them on the server. 您将必须对添加的新行(及其中的小部件)使用某种命名约定,以便可以在服务器上重新创建它们。

A simpler option may be to use an UpdatePanel. 一个更简单的选项可能是使用UpdatePanel。 Rather than try to add the rows on the client, your "add row" button would cause a partial update. 而不是尝试在客户端上添加行,您的“添加行”按钮将导致部分更新。 The server side code would add the new row to the table and the viewstate would be updated. 服务器端代码会将新行添加到表中,并更新viewstate。

I ended getting my data guy to do a bit more processing on his end so that I could just bind a ListView to a sproc. 我结束了让数据专家对他进行更多处理的工作,以便可以将ListView绑定到存储过程。 Solved a few of my problems. 解决了我的一些问题。

Thanks for the helpful comments. 感谢您的有用评论。

Just returning to this almost a year later because I faced a similar issue in my current project. 差不多一年后才回到这个话题,因为我在当前的项目中也遇到了类似的问题。 I was doing a few things wrong my first time around. 第一次我做错了几件事。 First, I did indeed need to recreate the controls every load, but I also had to create them earlier in the page cycle. 首先,确实确实需要在每次加载时重新创建控件,但是我还必须在页面周期的早期创建它们。 I was trying to create them during page load, which is after the viewstate is loaded. 我试图在页面加载期间(即在加载viewstate之后)创建它们。

The controls need to be recreated with the same ID before viewstate is loaded. 在加载viewstate之前,需要使用相同的ID重新创建控件。 I went with page init and it worked fine. 我去了页面初始化,它工作正常。

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

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