0
         public partial class ChatUserControl : System.Web.UI.UserControl
        {
            UserChatClass ucc = new UserChatClass();
            public ChatUserControl()
            {
                lblChatFriend = new Label();
                txtChatMessage = new TextBox();
                imgFriend = new Image();
                rpChatMessages = new Repeater();
            }
            public string ChatFriend { get { return this.lblChatFriend.Text; } set { this.lblChatFriend.Text = value; } }
            public string imgFriendUrl { get { return this.imgFriend.ImageUrl; } set { this.imgFriend.ImageUrl = value; } }
            public object rpChatDataSource { get { return this.rpChatMessages.DataSource; } set { this.rpChatMessages.DataSource = value; } }
            public Repeater rpChatMessagesToBind { get { return this.rpChatMessages; } set { this.rpChatMessages = value; } }
            }


        protected void Page_Load(object sender, EventArgs e) 
                { 
                    if (!IsPostBack) 
                    { 
ChatUserControl user1 = new ChatUserControl();
                    divChatUserControlCover.Controls.Add(user1);
                    } 
                } 
         private void BindUserControls()
                {                ChatUserControl user1 = divChatUserControlCover.Controls[1] as ChatUserControl;
                        user1.ChatFriend = row["username"].ToString();                    
                        user1.imgFriendUrl = "../../HttpImageHandler.jpg?username=" + row["username"].ToString(); 

                        DataSet dsCM = ucc.GetChatMessages(Session["username"].ToString(), row["username"].ToString());
                        user1.rpChatDataSource = dsCM;
                        user1.DataBindForRpChatMessagesToBind();
                        user1.Visible = true;

         }

    Master.aspx

    <div id="divChatUserControlCover" runat="server">

            </div>

好的,我已经编辑了代码,现在我已经创建了属性。如何调用 rpChatMessages 的 DataBind 方法?我也无法在页面上看到我的用户控件。为什么

4

3 回答 3

0

我不确定您是否尝试引用第一个标签或第二个标签。如果它是第二个标签,您不能只是这样做,chatMessage.您将不得不这样做

((Label)rpChatMessages.FindControl("chatMessage")) due to scope of controls. 

当您在另一个组件(即Repeater)中引用一个组件时,子组件不再属于文档(隐含this)而是属于控件,即

this.rpChatMessages { chatMessage }

于 2012-07-27T10:06:23.903 回答
0

我认为您只是想将传递给 a内部的一个控件,UserControl如果这是正确的,请声明一个像这样的公共属性:

背后的 ASCX 代码

public string MyProperty
{
    get
    {
        return this.lbl.Text;
    }
    set
    {
        this.lbl.Text = value;
    }
}

将值设置为 UserControl

private void BindUserControls() 
            { 
ChatUserControl user1 = divChatUserControlCover.Controls[1] as ChatUserControl; 
                    user1.MyProperty = row["username"].ToString(); 

在页面标记中设置值

<uc1:ChatUserControl MyProperty='<%# Eval("some field") %>' ...

编辑 1

删除该行

public object rpChatDataSource { get { return this.rpChatMessages.DataSource; } set { this.rpChatMessages.DataSource = value; } 

而是添加一个方法

public void BindMyRepeaterOrWhatever(IEnumerable<Yourentity> data)
{
    this.myDataBoundControl.DataSource = data;
    this.myDataBoundControl.DataBind();
}

您可以更改IEnumerable<Yourentity> dataforobject data但如果您可以传递强类型枚举会更好

于 2012-07-27T10:10:01.430 回答
0

令我惊讶的是,我发现为什么我的用户控件的子控件没有被实例化。这是因为ChatUserControl user1 = new ChatUserControl()没有初始化其子控件。创建用户控件的新实例的正确方法是这样......

ChatUserControl user1 = (ChatUserControl)Page.LoadControl("~/ChatUserControl.ascx");    
于 2012-07-27T15:39:21.837 回答