服务器控件和视图状态

发布于 2024-07-11 02:02:35 字数 401 浏览 12 评论 0原文

好吧,是时候展示一下我对网络表单的所有知识的完全缺乏了,但是这里就是了。 我正在扩展 Panel 控件并在其中添加一些附加控件的 OnPreRender(为简单起见,只说 1 个文本框)。 从这里开始,我只是让面板渲染方法完成它的工作。

我遇到的问题是,显然每次重新渲染此控件时,它都会再次将同一个 TextBox 与我在 OnPreRender 方法中编码的值粘贴在面板中。 现在我实际上不想每次都重新填充面板,

我想在第一次加载时将文本框控件粘贴在那里,并从控件/视图状态缓存中重新加载它们。 在本例中,我只在面板中粘贴一个文本框,如果文本框的值发生更改并且发生回发,我希望该值保持更改后的值。

我知道非常基本的网络表单内容,但我从来没有必要创建自定义控件。 任何帮助表示赞赏。

克里斯。

Ok time to show my complete lack of knowladge for all things web forms but here goes. I am extending the Panel control and OnPreRender sticking some additional controls inside of it (lets just say 1 textbox for simplicity). From here I am just letting the Panels Render method do its thing.

The issue I am having is that obviously every time this control is rerendered it is just sticks that same TextBox in the panel again with the value I am coding in the OnPreRender method. Now I dont actually want to repopulate the panel every time,

I want to stick the textbox contorl in there on first load and have them reloaded from the control/viewstate caches. In this case with my example of just sticking a single textbox in the panel, if the value of the textbox changes and a postback occurs I want that value to to remain the changed value.

Really basic webforms stuff I know, but I have never had to create custom controls in my time. ANy help appreciated.

Chris.

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

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

发布评论

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

评论(4

森林散布 2024-07-18 02:02:35

您需要在 OnInit 中(重新)创建子控件(文本框) - 以便在调用 LoadViewState 和 ProcessPostBackData 时它就在那里。

请参阅 服务器控制生命周期了解更多信息。

You need to (re)create the child control (the textbox) in OnInit - so that it's there when LoadViewState and ProcessPostBackData is called.

See the server control lifecycle for more info.

二手情话 2024-07-18 02:02:35

ASP.NET 中的动态控件很棘手,尤其是当您不熟悉 Web 表单和页面生命周期时。 如果可以避免动态控制,那就这样做。 使用 controlName.Visible=false 和其他技巧来代替。

如果您必须阅读此文章。 根据经验,在页面生命周期的早期添加控件,在页面生命周期的后期引用它们。 PreRender 几乎是最后的部分,这是添加和使用控件的不常见的地方。

Dynamic controls in ASP.NET are tricky, especially if you are new to webforms and the page lifecycle. If you can avoid dynamic controls, do so. Use controlName.Visible=false, and other tricks instead.

If you must then read this article. Rule of thumb,add controls early in the page life cycle, reference them later in the page lifecycle. PreRender is almost the very end, an uncommon place to be adding and using controls.

仅此而已 2024-07-18 02:02:35

不确定这是否适用于 .Net 的所有版本(我认为是 2.0 或更高版本),但有一个名为 CreateChildControls 的方法,它实际上并不是生命周期的一部分,它基本上是在调用 EnsureChildControls 方法时调用的。 默认情况下,如果不是回发,它会在 PreRender 之前调用。 所以基本上你的代码看起来像这样:

public class SomeControl : WebControl, INamingContainer
{
  private TextBox someTextBox;

  protected override void CreateChildControls()
  {
    base.CreateChildControls();

    someTextBox= new TextBox();
    someTextBox.ID = "tbxMain";

    Controls.Add(textboxToCheck);
  }
}

现在需要注意的是,除非你调用 EnsureChildControls,否则在 ViewState 负载填充控件上的公共属性之前,你不能 100% 确定控件存在。 这是什么意思? 我们将使用之前的代码并为 CssClass 添加一个属性:

public class SomeControl : WebControl, INamingContainer
{
  private TextBox someTextBox;

  protected override void CreateChildControls()
  {
    base.CreateChildControls();

    someTextBox= new TextBox();
    someTextBox.ID = "tbxMain";

    Controls.Add(textboxToCheck);
  }

  public String CssClass { get; set; }
}

在 CreateChildControls 中,您不会想要这样的:

someTextBox.CssClass = CssClass;

因为还没有办法确保控件存在。 有几种方法可以处理这个问题:

public String CssClass
{
得到
{
确保ChildControls();
返回一些Textbox.CssClass;

 set
 { 
   EnsureChildControls();
   someTextbox.CssClass = value;
 }

此示例中,我调用 EnsureChildControls(假设您在 CreateChildControls 方法中的文本框上设置 CssValue)并设置或从文本框获取。

另一种方法是将任何依赖于控件的公共属性的内容放入 OnPreRender 方法中:

protected override void OnPreRender(EventArgs e)
{
  someTextbox.CssClass = CssClass;
}

从而避免担心在 ViewState 加载期间已填充属性的混乱。

注意事项:

INAmingContainer 的使用可能很重要。 基本上,所做的就是通过将父级的名称(也许还有更多)应用于 id,确保父级控件上的控件具有在页面上唯一的 id。 基本上,如果父 ID 为 Parent 并且子控件 ID 为 Child,则 ID 可能会显示为 Parent_Child。 这将解决 ViewState 未正确填充属性或根本不填充属性的问题。

Not sure if this applies to all versions of .Net, (I think 2.0 or later) but there is a method called CreateChildControls that isn't really a part of the lifecycle exactly, it's basically called anytime the EnsureChildControls method is called. By default it is called before PreRender if it's not a postback. So basically your code would look like this:

public class SomeControl : WebControl, INamingContainer
{
  private TextBox someTextBox;

  protected override void CreateChildControls()
  {
    base.CreateChildControls();

    someTextBox= new TextBox();
    someTextBox.ID = "tbxMain";

    Controls.Add(textboxToCheck);
  }
}

Now the part to not is that unless you call EnsureChildControls, you can't be 100% sure that the controls exist before the Public Properties on your control are filled by the ViewState load. What does this mean? Well take the code from before and add a property for the CssClass:

public class SomeControl : WebControl, INamingContainer
{
  private TextBox someTextBox;

  protected override void CreateChildControls()
  {
    base.CreateChildControls();

    someTextBox= new TextBox();
    someTextBox.ID = "tbxMain";

    Controls.Add(textboxToCheck);
  }

  public String CssClass { get; set; }
}

In CreateChildControls you won't want this:

someTextBox.CssClass = CssClass;

Since there is no way to be sure the control exists yet. There's a couple ways you can handle this:

public String CssClass
{
get
{
EnsureChildControls();
return someTextbox.CssClass;
}

 set
 { 
   EnsureChildControls();
   someTextbox.CssClass = value;
 }

In this example I am calling EnsureChildControls (Assuming you are setting the CssValue on the textbox in the CreateChildControls method) and setting or getting from the textbox.

Another way is putting anything that depends on the control's public properties in the OnPreRender method:

protected override void OnPreRender(EventArgs e)
{
  someTextbox.CssClass = CssClass;
}

Thus avoiding the mess of worrying about the property being filled already during the ViewState load.

One Note:

The use of INamingContainer can be important. Basically all that does is makes sure the controls on the parent control have an id that is unique on the page by applying the parent's name (And maybe more) to the id. Basically if the parent ID is Parent and the child control ID is Child the ID might show up as Parent_Child. This will solve problems with ViewState not populating the properties correctly or not at all.

弱骨蛰伏 2024-07-18 02:02:35

如果您需要视图状态服务,则在代码中您将需要管理视图状态信息的恢复。

Microsoft 的查看状态示例就是一个很好的示例。 代码示例中还引用了一些其他项目,但它应该可以帮助您走上正确的道路。

Inside your code you will need to manage the restore of viewstate information should you need the services of viewstate.

A good example here is this View State example by Microsoft. There are a few other items referenced in the code sample, but it should get you along the right path.

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