|zyciis| 急求ASP.NET用户控件的属性如何做成子标签格式,谢谢

zyciis188 2010-05-24 01:45:16
如我原来用户控件写法如下

<YK:Header ID="Header" runat="server" MyHtml="...这里为我一般HTML值" />

因为我要赋值的是一般HTML文本,
如果用ASP.NET用户控件属性的方法比较麻烦
那如果我想做成如下格式应该怎么进行改造呢!?

<YK:Header ID="Header" runat="server">
<MyTemplate Key="MyHTML">
... 这里为我的一般HTML值
</MyTemplate>
</YK:Header>


谢谢

...全文
240 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiqishardgel 2010-05-24
  • 打赏
  • 举报
回复
学习了,帮顶!
阿非 2010-05-24
  • 打赏
  • 举报
回复
哦,那这样做

UserControl

public partial class UserControl_MyTemplateUserControl : System.Web.UI.UserControl
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);

if (MyTemplate != null)
{
Control ctrl = new Control();

MyTemplate.InstantiateIn(ctrl);

Controls.Add(ctrl);
}
}

[TemplateContainer(typeof(UserControl_MyTemplateUserControl))]
public ITemplate MyTemplate
{
get;
set;
}

}



<%@ Register Src="MyTemplateUserControl.ascx" TagName="MyTemplateUserControl" TagPrefix="uc" %>

<uc:MyTemplateUserControl ID="mtuc" runat="server">
<mytemplate>
<fieldset>
<legend>CustomTemplateControl </legend>
<br />
<div>
我是div</div>
<hr />
<span>我是span</span>
</fieldset>

</mytemplate>
</uc:MyTemplateUserControl>


yypf2540017 2010-05-24
  • 打赏
  • 举报
回复
模板控件,学习了
zyciis188 2010-05-24
  • 打赏
  • 举报
回复
RE:: CompositeControl
------------------
不能用这个吧。我现在做的是用户控件
UserControl(用户控件)System.Web.UI.UserControl
不是自定义控件
悔说话的哑巴 2010-05-24
  • 打赏
  • 举报
回复
http://www.cnblogs.com/yuxiang9999/archive/2006/06/08/421065.html
lester19872007 2010-05-24
  • 打赏
  • 举报
回复
阿非 2010-05-24
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web.UI;

namespace CustomWebControl
{
public class CustomTemplateControl : CompositeControl
{

#region//声明模板

[Browsable(false)]
[TemplateContainer(typeof(CustomTemplateControl))]
[PersistenceMode(PersistenceMode.InnerProperty)]

public ITemplate MyTemplate
{
get;
set;
}

#endregion

protected override void CreateChildControls()
{
Controls.Clear();

if (MyTemplate != null)
MyTemplate.InstantiateIn(this);
else
base.CreateChildControls();

ChildControlsCreated = true;
}
}
}




<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="UserControl_Default2" %>

<%@ Register TagPrefix="custom" Assembly="CustomWebControl" Namespace="CustomWebControl" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<custom:CustomTemplateControl ID="ctc" runat="server">
<MyTemplate>
<fieldset>
<legend>CustomTemplateControl </legend>
<br />
<div>
我是div</div>
<hr />
<span>我是span</span>
</fieldset>
</MyTemplate>
</custom:CustomTemplateControl>
</form>
</body>
</html>

zyciis188 2010-05-24
  • 打赏
  • 举报
回复
能不能发些相关的文章,谢谢

Google搜索关键字
阿非 2010-05-24
  • 打赏
  • 举报
回复
你需要将之前的自定义控件,定义成 模板控件

62,075

社区成员

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

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

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

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