Repeater在PostBacks上丢失了它的数据

中继器标记:

  <asp:CheckBox ID="IsSelected_ChkBx" runat="server" Text='' />   <asp:HiddenField ID="ID_HdnFld" runat="server" Value='' />   

代码隐藏:

  protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { PopulateStatRptr(); } } private void PopulateStatRptr() { SqlConnection conn; SqlCommand comm; SqlDataReader reader; string _connString = "Data Source=localhost\\SqlExpress;Initial Catalog=MyDb;Integrated Security=True"; conn = new SqlConnection(ConString); comm = new SqlCommand("SELECT ID, Item FROM Stats", conn); try { conn.Open(); reader = comm.ExecuteReader(); stat_Rptr.DataSource = reader; stat_Rptr.DataBind(); reader.Close(); } finally { conn.Close(); } } 

好吧,似乎Repeater是一个动态控件。 如果您在代码隐藏中绑定,则必须意识到在您使用DataBind()之前,itemtemplate中的文本框和复选框不存在。 如果禁用viewstate,除非在每个页面加载时进行数据绑定,否则不会看到它们。 在这种情况下,您从viewstate获取值。

检查此链接 。

绑定在Page_Init而不是Page_Load。

摆脱if (!IsPostBack)代码并每次都调用你的函数。