用户控件子控件未实例化

时间:2012-07-27 10:03:35

标签: asp.net

         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方法?我也无法在页面上看到我的用户控件。为什么

3 个答案:

答案 0 :(得分:0)

我不确定您是否尝试引用第一个标签或第二个标签。如果它是第二个标签你不能只做chatMessage.你必须做

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

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

this.rpChatMessages { chatMessage }

答案 1 :(得分:0)

我认为您只是尝试将传递给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> data的{​​{1}},但如果您可以通过强类型枚举则会更好

答案 2 :(得分:0)

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

ChatUserControl user1 = (ChatUserControl)Page.LoadControl("~/ChatUserControl.ascx");