中继器标记:
<asp:Repeater ID="stat_Rptr" runat="server">
<ItemTemplate>
<asp:CheckBox ID="IsSelected_ChkBx" runat="server" Text='<%# Eval("Item") %>' />
<asp:TextBox ID="Value_TxtBx" runat="server"></asp:TextBox>
<asp:HiddenField ID="ID_HdnFld" runat="server" Value='<%# Eval("ID") %>' />
</ItemTemplate>
<SeparatorTemplate>
<br></br>
</SeparatorTemplate>
</asp:Repeater>
代码隐藏:
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是一个动态控件。如果您在代码隐藏中进行绑定(bind),您必须意识到项目模板中的文本框和复选框在您使用 DataBind() 之前不存在。如果您禁用 View 状态,除非您在每次页面加载时进行数据绑定(bind),否则您将看不到它们。在这种情况下,您将从 View 状态获取值。
检查这个link出。
关于c# - Repeater 在 PostBacks 上丢失了它的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6886683/