【问题标题】:DataBinding Not Working In My ASP.NET UserControl数据绑定在我的 ASP.NET 用户控件中不起作用
【发布时间】:2010-08-07 04:31:04
【问题描述】:
<asp:Repeater ID="repFilter" runat="server">
                        <HeaderTemplate>
                            <div id="posting" align="center">
                        </HeaderTemplate>

                        <ItemTemplate>
                            <div class="friends_area" id="record-">
                                <%# ((Alert)Container.DataItem).Message %>
                                <asp:Literal Visible="false" ID="litAlertId" Text='<%# ((Alert)Container.DataItem).AlertId %>' runat="server"></asp:Literal>
                                <Company:Comments ID="Comments1" SystemObjectRecordId='<%# ((Alert)Container.DataItem).AlertId %>' runat="server" SystemObjectId="7"></Company:Comments>
                            </div>
                        </ItemTemplate>

                        <FooterTemplate>
                            </div>
                        </FooterTemplate>
                    </asp:Repeater>

我在中继器中设置自定义控件的 SystemObjectRecordID 属性时遇到问题。目前,该值在数据库中最终为零。我检查过/值得指出的事情:

1) AlertId 是数据库中的主键标识列,并且该表有许多行,因此 AlertId 应该返回多个值。

2) DB 列的数据类型 (bigint) 和 AlertId 的数据类型 (long) 和 getter/setter SystemObjectRecordID 变量的数据类型 (long) 匹配。

3) 如果我将 '' 设置为文字的 Text 属性,我会得到预期的结果。因此,它与我的自定义控件有关,特别是数据如何绑定到 SystemObjectRecordID 属性。

4) 如果我将 SystemObjectRecordID 从 long 更改为 string 然后执行 ((Alert)Container.DataItem).AlertId.ToString() 我在调试中为 SystemObjectRecordId 获得的值为 null (但它在分配给文本时仍然有效文字的属性)。

5) 用户控件的位置是default.aspx,控件是cmets.ascx。代码文件不共享命名空间。

6) 我曾尝试操作中继器的 OnItemDataBound 属性,但由于我已经用文字证明数据 is 使用声明性值绑定我不确定我还能在里面做什么OnItemDataBound的方法逻辑。

一旦最终用户在 UI 中发表评论,SystemObjectRecordId 值(应该在页面加载时已经分配 - 我们知道它在文字的情况下)应该传递给自定义用户控件:

namespace Company.ProjectWeb.UserControls
{
    public partial class Comments : UserControl, IComments
    {
        private readonly IUserSession _userSession;
        private CommentsPresenter _presenter;
        public int SystemObjectId { get; set; }
        public long SystemObjectRecordId { get; set; }

无论我做什么,当通过我在 default.aspx 页面上的自定义用户控件传递时,SystemObjectRecordId 总是为零。我没有想法 - 在这一点上可以使用一些建议。谢谢。

【问题讨论】:

  • 中继器在更新面板中?

标签: asp.net data-binding custom-controls repeater


【解决方案1】:

好的,我已经通过将 SystemObjectRecordId 添加到 ViewState 中找到了解决方法:

public long SystemObjectRecordId
        {
            get
            {
                if (ViewState["SystemObjectRecordId"] != null)
                    return long.Parse(ViewState["SystemObjectRecordId"].ToString());
                else
                    return 0;
            }
            set
            {
                ViewState["SystemObjectRecordId"] = value;
            }
        }

这不是主意,但它有效。我仍然不明白为什么我不能像之前指定的那样设置声明值,但现在必须这样做。

【讨论】:

    【解决方案2】:

    您应该能够通过将Bindable 属性添加到您的 SystemObjectRecordId 属性来解决此问题。

    【讨论】:

      猜你喜欢
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-23
      • 2011-07-05
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      相关资源
      最近更新 更多