需要页面生命周期的帮助(我认为这把我搞砸了)

发布于 2024-08-26 07:37:02 字数 1458 浏览 6 评论 0原文

我已将一个空的 asp.net 表拖到我的 Web 表单上。我在这些后面的代码中生成所有行。

现在我的桌子被填满了,并且有下拉列表。当用户点击“保存”时,我会浏览所有行并更新数据库中下拉列表中的值。

这一切都很好。但是,如果 2 列各有“当前”,那么这 2 列不应再显示,并且 2 个新列将与其他下拉列表一起放置在其位置。

这一切都有效。但是,您必须刷新整个页面才能使应该消失的两列消失。

所以我试图做的是在按钮单击事件结束时。清除整个表,然后重新生成它。但是,当我这样做时,无论出于何种原因,我的值都不会再保存到数据库中。

if (IsPostBack == false)
{
    // check if dummy variables exist in db- If true just generate tables with values in db. If not generate them. 

}
else
{
   // grab the values from the database
   // generate tables with the values
}

btn click event
{
   go through all rows in table(foreach loop)
   update each column in the database with cells in each row. while in foreach loop.
   //done
}

所以这就是它的运行方式和工作方式(保存所有正确的值)表只是不更新​​给用户。

不起作用

    if (IsPostBack == false)
    {
        // same code as above
    }

   // if postback is true do nothing. By the time it gets to the click event it says there is zero rows in the table so nothing happens.
    btn click event
    {
       // same code
    }

也失败。

if (IsPostBack == false)
{
   // same code as above
}
else
{
   // same code as above but moved into its own method.
   gernerateTable();
}

btn click event
{
   // update all rows
   // once done clear the Tables rows
   // call generateTable()
}

最后一个没有任何作用,因为由于某种原因它没有更新任何内容。我不明白为什么。

那么我在这个生命周期中做错了什么,我的过程中出现了错误。当我希望立即更新表时,该代码不起作用。

I have dragged a empty asp.net table onto my webform. I generate all the rows in the code behind those.

Now my table gets filled up and has dropdown lists. When the user hits save I go through all the rows and update the values from the dropdownlist in the db.

This works all great. However if 2 columns have each have "Present" then those 2 columns should be not be shown anymore and 2 new columns get put in its place with other dropdown lists.

This all works. However you have to refresh the entire page to for the 2 columns that should go away to go away.

So what I tried to do is at the end of the button click event. Clear the whole table and then regenerate it. However when I do this then my values are not saved to the database anymore for whatever reason.

if (IsPostBack == false)
{
    // check if dummy variables exist in db- If true just generate tables with values in db. If not generate them. 

}
else
{
   // grab the values from the database
   // generate tables with the values
}

btn click event
{
   go through all rows in table(foreach loop)
   update each column in the database with cells in each row. while in foreach loop.
   //done
}

So this is how it goes and it works expect(all correct values are saved) the table is just not updated to the user.

Does not work

    if (IsPostBack == false)
    {
        // same code as above
    }

   // if postback is true do nothing. By the time it gets to the click event it says there is zero rows in the table so nothing happens.
    btn click event
    {
       // same code
    }

Fails also.

if (IsPostBack == false)
{
   // same code as above
}
else
{
   // same code as above but moved into its own method.
   gernerateTable();
}

btn click event
{
   // update all rows
   // once done clear the Tables rows
   // call generateTable()
}

This last one does nothing as for some reason it does not update anything. I don't understand why.

So what am I doing wrong with this life cycle something in my process is wrong. The code works just not when I want the table to be updated right away.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

下雨或天晴 2024-09-02 07:37:02

在构建页面时,ASP.NET 在创建控件时生成控件 ID。这些 ID 被呈现为 HTML 并由浏览器提交回服务器。在回发期间,ASP.NET 重新生成页面上的控件以确定 ID,然后将请求数据中的数据值分配给控件。

因此,如果您动态生成控件(带有下拉列表的表行),那么您需要在检查、保存或修改它们之前生成所有这些行。我过去使用过的常见模式是这样的:

OnPageLoad() {
  get data;
  build controls;
  bind data; 
}

OnButtonClick() {
  save data();
  build controls; // if changed by 'remove' or 'add'
  bind data;
}

In building a page, ASP.NET generates control ID's as the controls are created. These ID's are rendered to the HTML and submitted by the browser back to the server. During postback, ASP.NET re-generates the controls on the page to determine ID's, then assigns data values to the controls from the Request data.

So, if you dynamically generate controls (table rows with drop-down lists) then you need to generate all those rows before you inspect, save, or modify them. A common pattern that I have used in the past is something like this:

OnPageLoad() {
  get data;
  build controls;
  bind data; 
}

OnButtonClick() {
  save data();
  build controls; // if changed by 'remove' or 'add'
  bind data;
}
臻嫒无言 2024-09-02 07:37:02

如果将控件动态添加到页面,则需要先重建它们,然后才能对它们执行任何操作。

为了完全绕过这些类型的问题,我通常会使用中继器。然后,您仍然可以在 RepeaterItems 上执行 foreach 循环。它很干净,也相对简单。没有页面生命周期问题。

If you add controls dynamically to the page, you need to rebuild them before you can do anything with them.

What I usually do to completely bypass these types of issues is to use a repeater. You can then do your foreach loop still on the RepeaterItems. It's clean and it's relatively simple as well. No page lifecycle issues.

幻梦 2024-09-02 07:37:02

尝试将动态控件的创建移至 Page Init 事件。

Try moving the creation of dynamic controls to Page Init event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文