开发者

ASP.NET / UserControl, ViewState and losing Property values

this is about ASP.NET, ViewState, UserControls and losing the values of my properties. It is an classic question by know, I know, and even though I have searched here and on Google for a resolution to this problem, I havent succeeded. On the contrary, the more I test different things, the less I understand it seems.

On top of this I am using ext.net and their so called DirectMethod's but I dont think that has much to do with this problem. There are numerous questions I have and I hope that this text will be fairly readable and understandable =)

The UserControl

I have a UserControl, Customers.ascx, that contains some Properties. The "most" important is *_CustomerId*. The _CustomerId is set in code-behind, in ext.net's "DirectMethod" like this (code below from the Page Customers.aspx):

    [DirectMethod]
    public void SetCustomer()
    {
        RowSelectionModel sm = GridPanel2.SelectionModel.Primary as RowSelectionModel;

        if (sm.SelectedRow != null)
        {
            uint customerId = uint.Parse(sm.SelectedRow.RecordID);
            customer_modify._CustomerId = customerId;
        }
    }

The "customer_modify" is the instance of the UserControl as defined in Customers.aspx (a Page, not a UserControl):

<asp:Content ID="Content2" ContentPlaceHolderID="CPH_center" runat="server">
    <CP:Customer ID="customer_modify" runat="server" _IsCreateMode="false" ViewStateMode="Enabled" />   
</asp:Content>

In that Page, Customers.aspx, I have ext.net GridPanel that lists the Customers and when I click the rows in the GridPanel the (Direct)method SetCustomer is executed.

As you can see in the SetCustomer-method, the property "_CustomerId" is updated with an uint, so lets take a look at the Property in the UserControl:

    public uint _CustomerId 
    { 
        get
        {
            object o = ViewState["_CustomerId"];
            if (o == null)
                return 0;
            return (uint)o;
        }
        set
        {
            object o = ViewState["_CustomerId"];
            ViewState["_CustomerId"] = value;
            SetCustomer();
        }
    }

As you can see, I am using the ViewState-thingie and it was my hope that the state, ie the value of the properties would be remembered. They are not.

When I click a Row in the GridPanel the first time I can see (by breaking) that the ViewState["_CustomerId"] == null which is alright. I see thereafter that the setter for _CustomerId is executed and that the ViewState["_CustomerId"] is assigned the RecordID (uint).

I now click another Row in the GridPanel and the SetCustomer-method is executed again. I break in the setter for _CustomerId and take a look at what the ViewState["_CustomerId"] is BEFORE assigning it. It is 0, but I expect it to be the value from the previous Row clicked.

Why is it 0? What am I missing?

Also:

I actually have two of there UserControls on the Page, the other one named "customer_create":

<ext:Window 
        ID="Window_CreateNewCustomer" 
        runat="server" 
        Icon="New"
        Title="Skapa ny kund" 
        Hidden="true"
        Width="480"
        Height="370"
        Modal="true">

        <Content>
            <CP:Customer ID="customer_create" runat="server" Title="Skapa ny kund" _IsCreateMode="true" />
        </Content>
</ext:Window>

What disturbs me a lot is that each and every time I click on a Row in the GridPanel the Page_Load in the UserControl is executed twice, even though I am only updating one of them in the SetCustomer()-method.

Why is that?

Any tips, ideas and help would be greatl开发者_JAVA技巧y appreciated.


ViewState is serialized, enciphered and sent to the client as a hidden form field so that it may be reconstituted when something triggers a form submission. A [DirectMethod] does not go through form submission (it's processed as an AJAX call), so it can neither read nor write ViewState data, unless round-tripping of this data is specifically requested with ViewStateMode.Enabled.

On any given request directed to a form, the Load event will be fired from the Page, and thus received by every user control on the page that subscribes to the event at the expected phase of the page processing lifecycle -- whether it's two instances of one UserControlor two different UserControls, it's two subscribers and therefore two calls (one to each instance).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜