Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在自定义控件中绑定转发器,将读取器传递给公共类。OnItemDataBound未点火_C#_Data Binding_User Controls_Repeater_Ascx - Fatal编程技术网

C# 在自定义控件中绑定转发器,将读取器传递给公共类。OnItemDataBound未点火

C# 在自定义控件中绑定转发器,将读取器传递给公共类。OnItemDataBound未点火,c#,data-binding,user-controls,repeater,ascx,C#,Data Binding,User Controls,Repeater,Ascx,我被赋予了一些独特的任务,但在我的研究中没有发现类似的例子。我有一个ascx,在这个ascx中我注册了一个自定义控件 ascx打开一个SQL连接,执行一个读卡器,然后将该读卡器传递给具有中继器的自定义控件 自定义控件已加载,但在中继器上出现“对象引用未设置为对象实例”错误。我尝试了所有正常的事情(有些不太正常),试图绑定。这是提纲(为了简洁起见,省略了一些内容) 主要ascx: <%@ Control Language="c#" AutoEventWireup="false" Codebe

我被赋予了一些独特的任务,但在我的研究中没有发现类似的例子。我有一个ascx,在这个ascx中我注册了一个自定义控件

ascx打开一个SQL连接,执行一个读卡器,然后将该读卡器传递给具有中继器的自定义控件

自定义控件已加载,但在中继器上出现“对象引用未设置为对象实例”错误。我尝试了所有正常的事情(有些不太正常),试图绑定。这是提纲(为了简洁起见,省略了一些内容)

主要ascx:

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="card_caller.ascx.cs" Inherits="mynamespace.card_caller" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
..
..
<%@ register Tagprefix="club" Tagname="card_display" Src="/somefolder/card_display.ascx"%>
<h1> <%# _firstName %> <%# _lastName%></h1>


<div style="float:right;">
    <club:card_display ID="committee_cardView" runat="server" />
</div>
打开自定义控件。这里是用户控件的相关部分-中继器。这里没什么奇怪的事。我正在使用OnItemDataBound

<asp:repeater id="card_repeater" EnableViewState="true" OnItemDataBound="card_repeater_ItemDataBound" Visible="True" runat="server">
    <itemtemplate>
      <!-- stuff goes in here -->
    </itemtemplate>
</asp:repeater>

下面是自定义控件的相关部分

namespace mynamespace {
/// <summary>
/// card view display
/// </summary>
[Serializable]
public class card_display : BaseModuleControl {

        private void Page_Load(object sender, EventArgs e) {
        card_repeater.ItemDataBound +=new RepeaterItemEventHandler(card_repeater_ItemDataBound);
        }

        public void setDataSource(SqlDataReader reader) {
            card_repeater.DataSource = reader;
            card_repeater.DataBind();

        }
    }

        protected void directory_repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) {

                _currentRecord = e.Item.DataItem as DbDataRecord;
                <!--Do some stuff -->
            }
        }
    }
}
名称空间mynamespace{
/// 
///卡片显示
/// 
[可序列化]
公共类卡显示:BaseModuleControl{
私有无效页面加载(对象发送方,事件参数e){
card_repeater.ItemDataBound+=新的RepeaterItemEventHandler(card_repeater_ItemDataBound);
}
public void setDataSource(SqlDataReader){
card_repeater.DataSource=读卡器;
card_repeater.DataBind();
}
}
受保护的无效目录\u repeater\u ItemDataBound(对象发送方,RepeaterItemEventArgs e){
if((e.Item.ItemType==ListItemType.Item)| |(e.Item.ItemType==ListItemType.AlternatingItem)){
_currentRecord=e.Item.DataItem作为DbDataRecord;
}
}
}
}
我不完全确定页面加载中是否需要事件处理程序。可能是因为可访问性问题而没有绑定

调试器通过setDataSource方法,在card_repeater.DataBind()处爆炸

如果我声明card_repeater=new repeater();在setDataSource中,不再存在未设置为对象错误实例的对象引用,但这显然是不正确的

这里有人看到了中继器没有绑定的明显原因吗?非常感谢您的帮助或指点。

解决了此问题。 有几个问题

在卡片显示中,页面加载

card_repeater.ItemDataBound +=new RepeaterItemEventHandler(card_repeater_ItemDataBound);
不需要

在getCards中

card_display cardpanel;
cardpanel = new card_display();
不需要,而是在setDataSource中需要像

committee_cardView.DataSource=reader;
committee_cardView.DataBind();
committee_cardView.DataSource=reader;
committee_cardView.DataBind();