开发者

Repeater Loses it's data on PostBacks

开发者 https://www.devze.com 2023-03-24 05:41 出处:网络
Repeater Markup: <asp:Repeater ID=\"stat_Rptr\" runat=\"server\"> <ItemTemplate> <asp:CheckBox ID=\"IsSelected_ChkBx\" runat=\"server\" Text=\'<%# Eval(\"Item\") %>\' />

Repeater Markup:

<asp:Repeater ID="stat_Rptr" runat="server">
                <ItemTemplate>
                    <asp:CheckBox ID="IsSelected_ChkBx" runat="server" Text='<%# Eval("Item") %>' />
                    &nbsp;<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>

Code-Behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
  开发者_StackOverflow          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();
        }
    }


Ok, it seems that Repeater is a dynamic control. If you are binding in the codebehind you have to realize that the textbox and checkboxes in the itemtemplate do not exist until you DataBind(). If you disable viewstate, you won't see them unless you databind on every page load. You are getting your values from viewstate in this case.

Check this link out.


Bind on Page_Init instead of Page_Load.


Get rid of the if (!IsPostBack) code and call your function every time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号