简体   繁体   English

删除动态创建的 HTML 表格行 - 生命周期/视图状态问题

[英]Removing dynamically created HTML table rows - Life Cycle / Viewstate problem

so i have an HTML table with dynamically added rows and ASP.NET text boxes.所以我有一个 HTML 表,其中包含动态添加的行和 ASP.NET 文本框。 I have the rows and controls re-instantiated on page_load if the viewstate[dataonpage] = true, and I'm declaring it as true in the method that adds the rows and controls.如果 viewstate[dataonpage] = true,我会在 page_load 上重新实例化行和控件,并且我在添加行和控件的方法中将其声明为 true。 (I need them to persist on other postbacks) (我需要他们坚持其他回发)

The problem is that I'm now I've added a CLEAR button that removes all of the html rows (excluding the headers) when it's clicked, and for some reason on button click it gets an index error, or if using Try/Catch it only removes half of the rows (every other row).问题是我现在添加了一个CLEAR 按钮,该按钮在单击时删除所有 html 行(不包括标题),并且由于某种原因单击按钮时会出现索引错误,或者如果使用 Try/Catch它只删除一半的行(每隔一行)。 I believe the problem is something to do with that the viewstate[dataonpage] is still "true", and the data is being re-added on page load.我认为问题与 viewstate[dataonpage] 仍然是“true”有关,并且在页面加载时重新添加数据。 If i add viewstate["dataonpage"] = "false" into the clear button method, the same happens but at least this way on the second click it removes the second half of the rows.如果我将 viewstate["dataonpage"] = "false" 添加到清除按钮方法中,也会发生同样的情况,但至少在第二次单击时会以这种方式删除行的后半部分。

I understand this happens because the button event handler isn't fired until after the page_load which is why it doesn't work on the first click.我知道发生这种情况是因为按钮事件处理程序直到 page_load 之后才会触发,这就是它在第一次点击时不起作用的原因。 But what I don't fully understand is why even without this my clear button code doesn't clear all of the rows in the first place.但我不完全理解的是,为什么即使没有这个,我的清除按钮代码也不会首先清除所有行。

Any help on understanding why it doesn't work, and a work around will be greatly appreciated!任何有助于理解为什么它不起作用的帮助,以及解决方法将不胜感激!

protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToString(ViewState["DataOnPage"]) == "true")
                {
                    Getmarketdata();
                }
        }
protected void Getdatabtn_Click(object sender, EventArgs e)
        {
            ViewState["DataOnPage"] = "true";
            Getmarketdata();
        }

Below is method that creates adds table rows and controls:下面是创建添加表行和控件的方法:

public void Getmarketdata()
        {
            String url = "https://api.rightmove.co.uk/api/rent/find?index=0&sortType=1&maxDaysSinceAdded=" + Dayssinceuploadtext.Text + "&locationIdentifier=OUTCODE%5e" + Outcodetext.Text + "&apiApplication=IPAD";
            Response.Write(url);

            using (var webclient = new WebClient())
            {
                String Rawjson = webclient.DownloadString(url);
                    ViewState["VSMarketDataJSONString"] = Rawjson;
                dynamic dobj = JsonConvert.DeserializeObject<dynamic>(Rawjson);
                int NoOfHouses = dobj["properties"].Count;
                Response.Write("<br />" + NoOfHouses);
                for (int i = 0; i < NoOfHouses; i++)
                {
                    System.Web.UI.HtmlControls.HtmlTableRow tRow = new System.Web.UI.HtmlControls.HtmlTableRow();
                    GeneratorTable.Rows.Add(tRow);
                    String RMlink = String.Format("https://www.rightmove.co.uk/property-to-rent/property-" + dobj["properties"][i]["identifier"].ToString()) + ".html";
                    HyperLink hypLink = new HyperLink();
                    hypLink.Text = dobj["properties"][i]["identifier"].ToString();
                    hypLink.Target = "_blank";
                    hypLink.NavigateUrl = RMlink;
                    using (System.Web.UI.HtmlControls.HtmlTableCell tb1 = new System.Web.UI.HtmlControls.HtmlTableCell())
                    {
                        tRow.Cells.Add(tb1);
                        tb1.Controls.Add(hypLink);
                    }
                    using (System.Web.UI.HtmlControls.HtmlTableCell tb2 = new System.Web.UI.HtmlControls.HtmlTableCell())
                    {
                        TextBox tbEPCe = new TextBox();
                        tRow.Cells.Add(tb2);
                        tb2.Controls.Add(tbEPCe);
                        String txtboxID = (("EPCETxtBox") + i);
                        tbEPCe.ID = txtboxID;
                        tbEPCe.Style.Add("background", "none"); tbEPCe.Style.Add("border", "1px solid black"); tbEPCe.Style.Add("border-radius", "2px");
                    }
                    using (System.Web.UI.HtmlControls.HtmlTableCell tb3 = new System.Web.UI.HtmlControls.HtmlTableCell())
                    {
                        TextBox tbEPCp = new TextBox();
                        tRow.Cells.Add(tb3);
                        tb3.Controls.Add(tbEPCp);
                        String txtboxID = (("EPCPTxtBox") + i);
                        tbEPCp.ID = txtboxID;
                        tbEPCp.Style.Add("background", "none"); tbEPCp.Style.Add("border", "1px solid black"); tbEPCp.Style.Add("border-radius", "2px");

                    }
                    using (System.Web.UI.HtmlControls.HtmlTableCell tb4 = new System.Web.UI.HtmlControls.HtmlTableCell())
                    {
                        TextBox tbBbl = new TextBox();
                        tRow.Cells.Add(tb4);
                        tb4.Controls.Add(tbBbl);
                        String txtboxID = (("BblTxtBox") + i);
                        tbBbl.ID = txtboxID;
                        tbBbl.Style.Add("background", "none"); tbBbl.Style.Add("border", "1px solid black"); tbBbl.Style.Add("border-radius", "2px");
                    }
                }
            }
        }

Below is clear table rows method: (this is the one that isn't working)下面是清除表格行的方法:(这是一个不起作用的方法)

public void ClearTableRows()
        {
            System.Web.UI.HtmlControls.HtmlTable Htmlgeneratortable = ((System.Web.UI.HtmlControls.HtmlTable)GeneratorTable);
            int NoOfRows = Htmlgeneratortable.Rows.Count;
            for (int j = 1; j < NoOfRows; j++)
            {
                try
                {
                    Htmlgeneratortable.Rows.RemoveAt(j);
                }
                catch
                { }
            }
        }

I'm going to explain what's going on as you have the code written now;我将解释发生了什么,因为您现在已经编写了代码; I don't have faith in my ability to provide an answer including the exact code changes to be made, so here is what is wrong with your current approach:我不相信我有能力提供答案,包括要进行的确切代码更改,因此您当前的方法有什么问题:

Your table, GeneratorTable exists for all clients.您的表GeneratorTable对所有客户端都存在。 That doesn't mean every time someone navigates to your website a table is generated, it means that there is one table, and every client that logs in is getting that one table.这并不意味着每次有人导航到你的网站产生的表格,就意味着有一个表,每一个客户端登录时获得一个表。

So if you add rows to it for one client, then send the table to another client, both clients will see the same table (with the rows added).因此,如果您为一个客户端向其中添加行,然后将表发送到另一个客户端,则两个客户端都会看到同一个表(添加了行)。

The problem is that emptying out a table is logic that has nothing to do with your back-end server.问题是清空表是与后端服务器无关的逻辑。 There's no reason for your server to be handling emptying a table, your server should only handle page navigations and AJAX calls pretty much, it shouldn't be changing how the webpage looks, because the server can only respond to each client one time.没有理由为您的服务器被处理清空一个表,你的服务器应该只处理页面导航和AJAX调用好看多了,它不应该被如何改变网页的外观,因为服务器只能每个客户一次回应。

What's the point in responding to a client with GeneratorTable and then updating GeneratorTable on the server?GeneratorTable响应客户端然后在服务器上更新GeneratorTable有什么意义? The client will never see the updates made to the table unless they're resent from the server.客户端永远不会看到对表所做的更新,除非它们是从服务器重新发送的。

You stated that you are new to this and need to learn about JS and client-side, this exercise should serve as an example of why you need to put certain code on the front-end and some code on the back-end, as there isn't really an elegeant way to do what you're looking to do with just the server.你说你是新手,需要学习 JS 和客户端,这个练习应该作为一个例子,说明为什么你需要在前端放一些代码,在后端放一些代码,因为那里不是真正优雅的方式来做你想要做的只是服务器。

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

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