IDE:Visual Studio 2010中 框架:.NET 3.5 的WebServer:Visual Studio的開發服務器這個複合控件爲什麼不呈現其子控件?
我試圖讓自定義控件和複合控件的把握。下面的代碼或多或少直接來自我在各種教程中找到的示例。我已將控件註冊在我的頁面和我的工具箱中。我可以拖動頁面上的控件並正確顯示文本屬性,但它不會呈現我的按鈕,文本框或下拉列表。有人可以看看這段代碼,並告訴我爲什麼這些項目不會在我的頁面上呈現嗎?
感謝您閱讀
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace HancockControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:PeoplePicker runat=server></{0}:PeoplePicker>")]
public class PeoplePicker : CompositeControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
protected TextBox txtName = new TextBox();
protected DropDownList ddlResults = new DropDownList();
protected Button btnSearch = new Button();
public string Text
{
get
{
EnsureChildControls();
String s = (String)ViewState["Text"];
return ((s == null) ? "[Text]" : s);
}
set
{
EnsureChildControls();
ViewState["Text"] = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(Text);
}
protected override void CreateChildControls()
{
base.CreateChildControls();
btnSearch.Text = "Search";
this.Controls.Add(txtName);
this.Controls.Add(ddlResults);
this.Controls.Add(btnSearch);
}
public override void RenderControl(HtmlTextWriter writer)
{
base.RenderControl(writer);
}
}
}
您是否調整過大小以顯示所有這些控件?我猜你沒有使用根據控件大小調整元素大小的佈局容器... –
我沒有。我不知道該怎麼做。我剛剛爲複合控件佈局容器做了搜索,但它沒有返回對我來說非常有意義的結果。你能告訴我一個簡單的例子嗎?根據這個教程http://www.c-sharpcorner.com/uploadfile/sapnabeniwal/create-and-use-composite-custom-control-in-Asp-Net/和其他幾個人,這應該是渲染這些控件。但事實並非如此。 – Camenwolf