如何在自己编写的控件中添加一个已有的控件??

zyycode 2004-07-02 07:31:48
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace WebControlLibrary1
{

public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
private Button button = new Button();
protected override void Render(HtmlTextWriter output)
{
button.Text="MyButton";
output.Write("<asp:Button id=button runat=server Text=Button></asp:Button>");
output.Write("test");
}
}
}


这么做好象不行...
...全文
61 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
BillTuan 2004-07-03
  • 打赏
  • 举报
回复
我一般是在.ascx文件中加的
saucer 2004-07-03
  • 打赏
  • 举报
回复
try

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace WebControlLibrary1
{

public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
private Button button = new Button();

protected override void CreateChildControls()
{
button.Text="MyButton";
button.ID = "button";
Controls.Add(button);
}

}
}


or



using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace WebControlLibrary1
{

public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
private Button button = new Button();

protected override void Render(HtmlTextWriter output)
{
//not easy to handle events
button.Text="MyButton";
button.ID = "button";
button.RenderControl(output);
}
}
}

fly_miss 2004-07-03
  • 打赏
  • 举报
回复
用容器
然后
容器.Controls.Add(button)
Diadem 2004-07-02
  • 打赏
  • 举报
回复
当然不行。

据个简单的例子:

TableCell tableCell = 你的控件.Cells[0];
private Button button = new Button();
button .ID="它的ID"
tableCell.Contorl.add(button)
这样才能作为服务器端的控件用,多参考一下资料,以前有个分页的DataGrid是个很好的例子
孟子E章 2004-07-02
  • 打赏
  • 举报
回复
在ascx文件里加看看

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧