问题:在.NET动态控件 [英] Problem with dynamic controls in .NET

查看:88
本文介绍了问题:在.NET动态控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的动态控件

您好所有,

我想创造一些动态控件,并让他们在整个页面加载坚持自己的视图状态。够方便吧?在每次页面加载所有我需要做的就是重新创建控件,使用相同的ID。然而,这里的渔获物 - 在我的preRender事件,我想清除的控件集合,然后重新用新值动态控制。造成这种情况的原因是复杂的,它可能会带我大约一个页面左右来解释我为什么要这么做。所以,在简洁的利益,让我们姑且认为我是绝对必须这样做,那有没有其他办法。

I'm wanting to create some dynamic controls, and have them persist their viewstate across page loads. Easy enough, right? All I have to do is re-create the controls upon each page load, using the same IDs. HOWEVER, here's the catch - in my PreRender event, I'm wanting to clear the controls collection, and then recreate the dynamic controls with new values. The reasons for this are complicated, and it would probably take me about a page or so to explain why I want to do it. So, in the interests of brevity, let's just assume that I absolutely must do this, and that there's no other way.

问题出现在我的preRender事件后,我重新创建控件。重新创建的控件永远不会绑定到视图状态,它们的值不跨页面加载持续。我不明白为什么会这样。在我的OnLoad事件我已经重新创建控件。当我这样做,新创建控件绑定到ViewState中就好了,只要我每次都使用相同的ID。然而,当我试图做同样的事情在preRender事件,它失败。

The problem comes in after I re-create the controls in my PreRender event. The re-created controls never bind to the viewstate, and their values do not persist across page loads. I don't understand why this happens. I'm already re-creating the controls in my OnLoad event. When I do this, the newly created controls bind to the ViewState just fine, provided that I use the same IDs every time. However, when I try to do the same thing in the PreRender event, it fails.

在任何情况下,这里是我的榜样code:

In any case, here is my example code :

命名空间TestFramework.WebControls
{

namespace TestFramework.WebControls {

public class ValueLinkButton : LinkButton
{
    public string Value
    {
        get
        {
            return (string)ViewState[ID + "vlbValue"];
        }

        set
        {
            ViewState[ID + "vlbValue"] = value;
        }
    }
}

public class TestControl : WebControl
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        Controls.Clear();

        ValueLinkButton tempLink = null;

        tempLink = new ValueLinkButton();
        tempLink.ID = "valueLinkButton";
        tempLink.Click += new EventHandler(Value_Click);

        if (!Page.IsPostBack)
        {
            tempLink.Value = "old value";
        }

        Controls.Add(tempLink);
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        ValueLinkButton tempLink = ((ValueLinkButton)FindControl("valueLinkButton"));  //[CASE 1]

        //ValueLinkButton tempLink = new ValueLinkButton();  [CASE 2]

        tempLink.ID = "valueLinkButton";
        tempLink.Value = "new value";
        tempLink.Text = "Click";            

        Controls.Clear();
        Controls.Add(tempLink);
    }

    void Value_Click(object sender, EventArgs e)
    {
        Page.Response.Write("[" + ((ValueLinkButton)sender).Value + "]");
    }
}

}

那么,让我们来看看情况1,其中线旁[案例1]没有被注释掉,但旁边[案例2]行注释掉。在这里,一切都运行得很好。当我把这个控件的页面上并加载网页,我看到一个链接,说点击。当我点击链接时,页面输出文本[新值],并在下一行,我们看到了熟悉的点击链接。每subesquent一次我点击点击链接,我们看到了同样的事情。到目前为止,一切都很好。

So, let's examine case 1, where the line next to [CASE 1] is not commented out, but the line next to [CASE 2] is commented out. Here, everything works just fine. When I put this control on a page and load the page, I see a link that says "Click". When I click the link, the page outputs the text "[new value]", and on the next line, we see the familiar "Click" link. Every subesquent time I click on the "Click" link, we see the same thing. So far, so good.

但是,现在让我们来看看情况2,其中线旁[案例1]被注释掉了,但旁边[案例2]该行没有被注释掉。在这里,我们遇到的问题。当我们载入页面,我们看到了点击链接。然而,当我点击链接时,页面输出文本[],而不是[新值]。 click事件通常是射击。然而,我分配给该控件的Value属性的新价值的文字不会被保留。再次,这是一个谜给我。为什么,当我重新创建的OnLoad,一切都很好,很正常,但是当我重新在preRender控制,价值没有得到坚持?对照

But now let's examine case 2, where the line next to [CASE 1] is commented out, but the line next to [CASE 2] is not commented out. Here we run into problems. When we load the page, we see the "Click" link. However, when I click on the link, the page outputs the text "[]" instead of "[new value]". The click event is firing normally. However, the "new value" text that I assigned to the Value attribute of the control does not get persisted. Once again, this is a bit of a mystery to me. How come, when I recreate the control in OnLoad, everything's fine and dandy, but when I recreate the control in PreRender, the value doesn't get persisted?

我觉得有根本必须有一个办法做到这一点。当我重新创建preRender控制,有没有新创建的控制权交给的ViewState?

I feel like there simply has to be a way to do this. When I re-create the control in PreRender, is there some way to bind the newly created control to the ViewState?

我这个奋斗了天。任何帮助,你可以给我将AP preciated。

I've struggled with this for days. Any help that you can give me will be appreciated.

感谢。

推荐答案

ViewState的支持的属性只保留到ViewState中,如果控制正在跟踪的ViewState。这是由设计来保持的ViewState尽可能小:它应该只包含是真正的动态数据。这样做的结果是:

ViewState-backed properties are only persisted to ViewState if the control is currently tracking ViewState. This is by design to keep ViewState as small as possible: it should only contain data that is truly dynamic. The upshot of this is that:

Init事件过程中设置的ViewState化子性质是的的后盾,ViewState中(因为网页还没有开始跟踪的ViewState)。因此初始化是个好地方添加控件并设置(一)属性不会回发之间更改(ID,的CssClass ......)以及为动态属性的初始值(然后可以通过code在被修改页面生命周期的其余部分 - 负载,事件处理程序,preRender)

ViewState propeties set during the Init event are not backed to ViewState (because the Page has not yet started tracking ViewState). Thus Init is a good place to add controls and set (a) properties that won't change between postbacks (ID, CssClass...) as well as initial values for dynamic properties (which can then be modified by code in the rest of the page lifecycle - Load, event handlers, PreRender).

当在负载或preRender动态添加控件,ViewState是被跟踪。那么开发商可以控制哪些化子性质是持续了动态添加控件如下:

When dynamically adding controls in Load or PreRender, ViewState is being tracked. The developer can then control which propeties are persisted for dynamically added controls as follows:


  • 控制之前设置

    属性添加到页面的控件树不会被持久化到ViewState中。通常,您可以添加控件到控件树之前设置没有动态属性(身份证等)。

  • Properties set before the control is added to the page's control tree are not persisted to ViewState. You typically set properties that are not dynamic (ID etc) before adding a control to the control tree.

在控制后设置的属性添加到页面的控件树被保存到视图状态(ViewState的跟踪是从Load事件之前启用了preRender事件之后)。

Properties set after the control is added to the page's control tree are persisted to ViewState (ViewState tracking is enabled from before the Load Event to after the PreRender event).

在你的情况,你的preRender处理程序添加控件到页面的控件树之前设置属性。为了得到你想要的结果,加入控制控件树后,设置动态属性:

In your case, your PreRender handler is setting properties before adding the control to the page's control tree. To get the result you want, set dynamic properties after adding the control to the control tree: .

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    ValueLinkButton tempLink = new ValueLinkButton(); // [CASE 2]        
    tempLink.ID = "valueLinkButton"; // Not persisted to ViewState
    Controls.Clear();
    Controls.Add(tempLink);
    tempLink.Value = "new value";  // Persisted to ViewState
    tempLink.Text = "Click";       // Persisted to ViewState
}

这篇关于问题:在.NET动态控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆