asp.net – 用户控件的属性在回发后失去价值

这是 HTML.我有一个包含用户控件的中继器.
<asp:Repeater ID="rpnewHire" runat="server">
 <HeaderTemplate>
  <table>            
 </HeaderTemplate>
 <ItemTemplate>
  <tr>
     <td>
         <user:CKListByDeprtment ID = "ucCheckList" 
          DepartmentID= '<%# Eval("DepID")%>' 
          BlockTitle = '<%# Eval("DepName")%>' 
          runat = "server"></user:CKListByDeprtment>
     </td>
   </tr>
  </ItemTemplate>
  <FooterTemplate>
   </table>
  </FooterTemplate>
</asp:Repeater>

DepartmentID是我在用户控件内定义的一个属性.

int departmentID;

public int DepartmentID
{
  get { return departmentID; }
  set { departmentID = value; }
}

这就是我正在尝试访问它

protected void Page_Load(object sender,EventArgs e)
{
   int test = departmentID;
}

页面第一次加载时,departmentID有一个值.但是,当页面返回时,该值始终为0.

解决方法

所有变量(和控件)都处于页面生命周期的末尾.所以你需要一种方法来保持你的变量,例如在ViewState中.
public int DepartmentID
{
    get {
        if (ViewState["departmentID"] == null)
            return int.MinValue;
        else 
            return (int)ViewState["departmentID"]; 
    }
    set { ViewState["departmentID"] = value; }
}

MSDN杂志文章“在ASP.NET应用程序中管理持久用户状态的九个选项”很有用,但在MSDN网站上不再可见.在2003年4月的版本中,您可以使用download as a Windows Help file (.chm).(不要忘记调出属性并解除“这是从互联网下载”的东西,否则您无法查看文章.)

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....