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方法?我也无法在页面上看到我的用户控件。为什么
答案 0 :(得分:0)
我不确定您是否尝试引用第一个标签或第二个标签。如果它是第二个标签你不能只做chatMessage.
你必须做
((Label)rpChatMessages.FindControl("chatMessage")) due to scope of controls.
当您引用另一个组件内的组件(即Repeater
)时,子组件不再属于文档(隐含this
),而是属于控件,即
this.rpChatMessages { chatMessage }
答案 1 :(得分:0)
我认为您只是尝试将值传递给UserControl
内的一个控件,如果这是正确的,请声明这样的公共属性:
public string MyProperty
{
get
{
return this.lbl.Text;
}
set
{
this.lbl.Text = value;
}
}
private void BindUserControls()
{
ChatUserControl user1 = divChatUserControlCover.Controls[1] as ChatUserControl;
user1.MyProperty = row["username"].ToString();
<uc1:ChatUserControl MyProperty='<%# Eval("some field") %>' ...
删除该行
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");