简体   繁体   English

Repeater中的用户控件

[英]User Controls in Repeater

I have a usercontrol that provides voting buttons (for a SO type voting model) - it contains a private int member that retains the id of the record. 我有一个提供投票按钮的用户控件(对于SO类型的投票模型) - 它包含一个保留记录ID的私有int成员。 Outside a repeater, it functions just fine - postbacks work, and the correct id is retained in the user control. 在转发器之外,它的功能很好 - 回发工作,并且正确的id保留在用户控件中。

Inside the repeater, an itemdatabound event handler associates the correct ID with the usercontrol and it works correctly - displays the correct vote count from the database. 在转发器内部,itemdatabound事件处理程序将正确的ID与usercontrol关联,并且它可以正常工作 - 从数据库中显示正确的投票计数。 When one of the voting buttons is pressed, though, it fires a postback to the usercontrol and the control has lost the contents of its private int member so it no longer functions. 但是,当按下其中一个投票按钮时,它会触发对用户控件的回发,并且控件已丢失其私有int成员的内容,因此它不再起作用。

I've tried both re-databinding the usercontrol on postback, and binding it only on the initial load - the problem is the same both ways. 我已经尝试在回发时重新数据绑定usercontrol,并仅在初始加载时绑定它 - 问题是两种方式相同。

How do I get the usercontrol to retain the value of that int across postbacks? 如何让usercontrol在回发中保留该int的值?

Have you tried putting the value in viewstate ? 您是否尝试将该值置于viewstate中?

public string Id{
    get
    {
        return this.ViewState["Value"] == null ?
            0 :
            (int)this.ViewState["Value"];
    }
    set { this.ViewState["Value"] = value; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM