直放站失去对回传它的数据 [英] Repeater Loses it's data on PostBacks

查看:62
本文介绍了直放站失去对回传它的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直放站标记:

 < ASP:直放站ID =stat_Rptr=服务器>
                <&ItemTemplate中GT;
                    < ASP:复选框ID =IsSelected_ChkBx=服务器文本='<%#的eval(项目)%>' />
                    &安培; NBSP;< ASP:文本框ID =Value_TxtBx=服务器>< / ASP:文本框>
                    < ASP:HiddenField ID =ID_HdnFld=服务器值='<%#的eval(ID)%GT;' />
                < / ItemTemplate中>
                < SeparatorTemplate>
                    < BR>< / BR>
                < / SeparatorTemplate>
            < / ASP:直放站>

code-背后:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        如果(!的IsPostBack)
        {
            PopulateStatRptr();
        }
     }
    私人无效PopulateStatRptr()
    {
        康涅狄格州的SqlConnection;
        的SqlCommand COMM;
        SqlDataReader的阅读器;        字符串_connString =数据源=本地主机\\\\ SqlEx preSS;初始目录= MYDB;集成安全性=真;        康恩=新的SqlConnection(ConString);
        COMM =新的SqlCommand(SELECT ID,产品从统计,康恩);        尝试
        {
            conn.Open();
            读者= comm.ExecuteReader();
            stat_Rptr.DataSource =阅读器;
            stat_Rptr.DataBind();
            reader.Close();
        }        最后
        {
            conn.Close();
        }
    }


解决方案

好吧,似乎直放站是一个动态的控制。如果您在codebehind绑定你必须认识到,在ItemTemplate的文本框和复选框不存在,直到你的DataBind()。如果禁用ViewState中,你将看不到它们,除非你数据绑定在每一页上的负载。你是从视图状态越来越在这种情况下,你的价值观。

勾选此<一个href=\"http://codinglifestyle.word$p$pss.com/2009/10/08/repeaters-and-lost-data-after-postback-viewstate/\">link出去。

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)
        {
            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.

这篇关于直放站失去对回传它的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆