ViewState:我做错了吗? [英] ViewState: Am I doing something wrong?

查看:62
本文介绍了ViewState:我做错了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。



我有2级控制。该网站托管面向公众的.ascx继承了它的服务器端.cs(继承了UserControl)。



我的所有数据访问都由我的服务器端控件处理,并且所有控件都由我的面向公众的控件填充。



此控件处理网站上的用户角色。用户可以通过url [host] / Roles访问此页面,并从下拉列表中选择一个用户。他们也可以通过url [host] / Roles / [username]访问它,它会自动从下拉列表中选择用户。



当页面加载了用户名,或者从下拉列表中选择用户名时,服务器端控件将用户名保存到viewstate以供日后参考。



从下拉列表中选择用户名时然后一切正常。



当从网址中选择用户名时,它不会维护视图状态



我确定这与我的双层控制有关,但我不知道如何解决它。



我测试了将test:test添加到viewstate if!IsPostback在面向公众的一面并保持该项目。



我做错了什么?



用户名属性:

 ... 
// 服务器端控制
protected string 用户名
{
获取
{
return ViewState.GetStateItem< string>( 用户名);
}
set
{
ViewState.SetStateItem( 用户名 value );
}
}
...
// 延伸:
public static T GetStateItem< T>( StateBag viewState,字符串键)
{
object obj = viewState [key];

if (obj == null
返回 默认(T);

if (obj.GetType()。IsAssignableFrom( typeof (T) ))
return (T)obj;
return 默认(T);
}
public static void SetStateItem< T>( StateBag viewState, string 键,T value
{

viewState [key] = value ;
}

解决方案

啊 - 我在第一次加载时在OnInit中添加了viewstate,然后在事件处理程序之后。



在OnInit之后的 InitComplete 期间启用了Viewstate跟踪。



我已将代码移至OnLoad并且工作正常



只是doh!

Hi.

I have a 2 tier control. The Website hosts the public facing .ascx which inherits it's server side .cs (which inherits UserControl).

All of my data access is handled by my server side control, and all of the controls are populated by my public facing control.

This control handles the users roles on the website. The user can access this page via the url [host]/Roles and select a user from the dropdown. They can also access it via the url [host]/Roles/[username] which will automatically select the user from the dropdown.

When the page is loaded with a username, or when a username is selected from the dropdown, the server side control saves the username to the viewstate for later reference.

When the username is selected from the dropdown then all works ok.

When the username is selected from the url then it does not maintain the viewstate

I'm sure this has to do with my two tier control, but I don't know how to fix it.

I tested adding "test":"test" to the viewstate if !IsPostback on the public facing side and that item is maintained.

What am I doing wrong?

Username Property:

...
        //Server side control
        protected string Username
        {
            get
            {
                return ViewState.GetStateItem<string>("Username");
            }
            set
            {
                ViewState.SetStateItem("Username", value);
            }
        }
...
// Extention:
        public static T GetStateItem<T>(this StateBag viewState, string key)
        {
            object obj = viewState[key];

            if (obj == null)
                return default(T);

            if (obj.GetType().IsAssignableFrom(typeof(T)))
                return (T)obj;
            return default(T);
        }
        public static void  SetStateItem<T>(this StateBag viewState, string key, T value)
        {
            
            viewState[key] = value;
        }

解决方案

Ah - I was adding to the viewstate in the OnInit on first load, then after the event handlers subsequently.

Viewstate tracking is turned on during InitComplete after OnInit.

I have moved the code to the OnLoad and it works fine

just doh!


这篇关于ViewState:我做错了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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