User control's property loses value after a postback

All variables (and controls) are disposed at the end of the page's lifecycle. So you need a way to persist your variable, e.g. in the ViewState.

public int DepartmentID
{
get {
if (ViewState["departmentID"] == null)
return int.MinValue;
else
return (int)ViewState["departmentID"];
}
set { ViewState["departmentID"] = value; }
}

The MSDN Magazine article "Nine Options for Managing Persistent User State in Your ASP.NET Application" is useful, but it's no longer viewable on the MSDN website. It's in the April 2003 edition, which you can download as a Windows Help file (.chm). (Don't forget to bring up the properties and unblock the "this was downloaded from the internet" thing, otherwise you can't view the articles.)

根据How to choose from Viewstate, SessionState, Cookies and Cache,对于配置文件中的数据,还是直接写在cache或者application级别比较好。不用考虑数据丢失的问题

User control's property loses value after a postback的更多相关文章

  1. Writing a Reusable Custom Control in WPF

    In my previous post, I have already defined how you can inherit from an existing control and define ...

  2. Shape Control for .NET

    Shape Control for .NET Yang Kok Wah, 23 Mar 2017 CPOL    4.83 (155 votes)   Rate this: vote 1vote 2v ...

  3. [No000012E]WPF(6/7):概念绑定

    WPF 的体系结构,标记扩展,依赖属性,逻辑树/可视化树,布局,转换等.今天,我们将讨论 WPF 最重要的一部分——绑定.WPF 带来了优秀的数据绑定方式,可以让我们绑定数据对象,这样每次对象发生更改 ...

  4. 微软原文翻译:适用于.Net Core的WPF数据绑定概述

    原文链接,大部分是机器翻译,仅做了小部分修改.英.中文对照,看不懂的看英文. Data binding overview in WPF 2019/09/19 Data binding in Windo ...

  5. spring源码分析之<context:property-placeholder/>和<property-override/>

    在一个spring xml配置文件中,NamespaceHandler是DefaultBeanDefinitionDocumentReader用来处理自定义命名空间的基础接口.其层次结构如下: < ...

  6. WPF's Style BasedOn

    <Style x:Key="BasedStyle" BasedOn="{x:Null}" TargetType="{x:Type Control ...

  7. spring boot源码分析之SpringApplication

    spring boot提供了sample程序,学习spring boot之前先跑一个最简单的示例: /* * Copyright 2012-2016 the original author or au ...

  8. WPFTookit Chart 入门

    如何使用WPFToolKit Chart private void button1_Click(object sender, EventArgs e) { var s = new Series(); ...

  9. 关于 ant 不同渠道自动打包的笔记

    必要的java.android.ant文件及循环打包用到的ant的jar 下载Ant(这里的Ant不是eclipse和android SDk里面自带的ant)      官方下载地址:http://a ...

随机推荐

  1. https中引用http

    https里引用http不安全,会报错 解决方案 1.可以部署在http中,http中引用https不会存在安全问题报错 2.https中引用https,把需要引用的http部署成https

  2. django-1-新手如何使用django

    工具: python,django 流程: 1.安装python 安装python,配置python环境变量, 2.安装django django安装有两种方式: 1.通过pip安装(python2叫 ...

  3. ubuntu 使用pecl 安装 PHP AMQP Extension

    下载扩展: sudo apt-get -y install gcc make autoconf libc-dev pkg-config sudo apt-get -y install libssl-d ...

  4. Java总结之Java简介

    一.序言 1.软件的介绍 软件是指一系列按照特定顺序组织的计算机数据和指令的集合. 2.人机交互 实现人与计算机的交互,主要有两种方式: 图形界面方式(Graphical User Interface ...

  5. MSF——Payload模块(二)

    MSF系列: MSF——基本使用和Exploit模块(一) MSF——Payload模块(二) MSF——Meterpreter(三) MSF——信息收集(四) 一.exploit和payload e ...

  6. 三种JavaScript 消息框

    prompt 提示框 <html><head><script type="text/javascript">function disp_prom ...

  7. Python 学习笔记13 类 - 创建和简单使用

    介绍: 面向对象编程是一种非常有效的软件编写方法之一,在面向对象编程中,我们会编写表示现实世界中的事物或者情景的类,并基于类来创建对象. 在编写类的的时候,这些类对象一般都有通用的行为或者属性.基于类 ...

  8. python 大小写转换函数

    capitalize() 首字母大写,其余全部小写 upper() 全转换成大写 lower() 全转换成小写 title()  标题首字大写,如"i love python".t ...

  9. Mac读写NTFS硬盘

    简明教程: 1.插上硬盘后,查看你的硬盘名称,这里假设名称是Untitled,牢记 2.在终端输入sudo nano /etc/fstab 敲击回车 3.现在你看到了一个编辑界面,输入LABEL=Un ...

  10. 转载:jQuery的deferred对象详解

    一.什么是deferred对象? 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作.其中,既有异步的操作(比如ajax读取服务器数据),也有同步的操作(比如遍历一个大型数组),它们 ...