一个用户控件 叫 labelbox
也就是一个label加一个textbox
a页中应用此控件
我想可以设置我的用户控件的值,也就是可以分别设置和取得label textbox的值。
((Label)Textbox1.FindControl("Label1")).Text = "发货人"; 是可以的。
我想实现: lb.Text = "发货人"; 谢谢!
比如先写个类:
public LabelBox()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
然后: LabelBox lb = new LabelBox()
也就是一个label加一个textbox
a页中应用此控件
我想可以设置我的用户控件的值,也就是可以分别设置和取得label textbox的值。
((Label)Textbox1.FindControl("Label1")).Text = "发货人"; 是可以的。
我想实现: lb.Text = "发货人"; 谢谢!
比如先写个类:
public LabelBox()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
然后: LabelBox lb = new LabelBox()
<asp:Panel ID="Panel1" runat="server" Height="3px" Width="110px">
<asp:Label ID="Label1" runat="server" Text="Label" Width="50px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="45px"></asp:TextBox></asp:Panel>
{
public string LabelText
{
get { return Label1.Text; }
set { Label1.Text = value;}
}public string BoxText
{
get { return TextBox1.Text; }
set { TextBox1.Text = value;}
}}
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace SampleControls
{
public class LabelTextBox : CompositeControl
{
#region Custom Properties
// ***************************************************************
// Text
public string Text
{
get
{
object o = ViewState["Text"];
if (o == null)
return String.Empty;
return (string) o;
}
set
{
ViewState["Text"] = value;
}
} // ***************************************************************
// Title
public string Title
{
get
{
object o = ViewState["Title"];
if (o == null)
return String.Empty;
return (string)o;
}
set
{
ViewState["Title"] = value;
}
} #endregion
#region Rendering // ***************************************************************
// CreateChildControls
protected override void CreateChildControls()
{
// Clears child controls
Controls.Clear(); // Build the control tree
CreateControlHierarchy();
ClearChildViewState();
} // ***************************************************************
// CreateControlHierarchy
protected virtual void CreateControlHierarchy()
{
TextBox t = new TextBox();
Label l = new Label(); // Configure controls
t.Text = Text;
l.Text = Title; // Connect to the parent
Controls.Add(l);
Controls.Add(t);
} #endregion
} public class TextBoxLabel : WebControl, INamingContainer, IPostBackDataHandler
{
#region Custom Properties
// ***************************************************************
// Text
public string Text
{
get
{
object o = ViewState["Text"];
if (o == null)
return String.Empty;
return (string)o;
}
set
{
ViewState["Text"] = value;
}
} // ***************************************************************
// Title
public string Title
{
get
{
object o = ViewState["Title"];
if (o == null)
return String.Empty;
return (string)o;
}
set
{
ViewState["Title"] = value;
}
} #endregion #region IPostBackDataHandler
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
{
string currentText = this.Text;
string postedText = postCollection[postDataKey];
if (!currentText.Equals(postedText, StringComparison.Ordinal))
{
Text = postedText;
return true;
}
return false;
}
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
return;
}
#endregion
#region Rendering // ***************************************************************
// Render
protected override void Render(HtmlTextWriter writer)
{
string code = String.Format("<span>{0}</span><input name='{2}' type='text' value='{1}'>",
Title, Text, ClientID);
writer.Write(code);
} #endregion
}
}
这个不行啊. 提示找不到label1 .再说了,我的usercontrol多了.继承哪一个?