动态添加的 ascx 控件 怎么给它传值啊!

hy86459 2009-04-09 06:06:48
动态添加的 ascx 控件 怎么给它传值啊!

wc.ascx 里面有个 name 属性


Control myControl = Page.LoadControl("~/control/wc.ascx");

PlaceHolder1.Controls.Add(myControl);
这里添加了 不知道怎么给它传值谢谢!
...全文
280 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
losingrose 2009-04-13
  • 打赏
  • 举报
回复
string control = Request.Form["Control"] == null ? "Rss" : Request.Form["Control"];
string name = Request.Form["Name"] == null ? "模块" : Request.Form["Name"];
int column = Request.Form["Column"] == null ? 3 : int.Parse(Request.Form["Column"]);
Control userControl = this.LoadControl(control);
this.Controls.Add(userControl);
if (userControl is PartialCachingControl)
{
PartialCachingControl uc = userControl as PartialCachingControl;
if (uc.CachePolicy != null) userControl = uc.CachedControl;
}
if (userControl != null)
{
Type controlType;
if(userControl.GetType().BaseType.GetProperty("name")!=null)
controlType = userControl.GetType().BaseType;
else
controlType = userControl.GetType().BaseType.BaseType;

PropertyInfo field = controlType.GetProperty("name");

if (field != null)
{
field.SetValue(userControl, name, null);
}

field = controlType.GetProperty("column");

if (field != null)
{
field.SetValue(userControl, column, null);
}
}
userControl.DataBind();

*****************************************************************************
河南地区求职.net程序开发,底薪3000低于勿扰。
欢迎长期技术学习交流朋友,尽力组建一只技术研究的网络团体,研发资料大家共享不外放,新手勿扰。
工作QQ:303864496
http://losingrose.cnblogs.com/
阿非 2009-04-13
  • 打赏
  • 举报
回复

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class temp_uc : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{

}
}


temp_uc 就是类名
jscn123789abc 2009-04-10
  • 打赏
  • 举报
回复
ding...
Lxpd 2009-04-10
  • 打赏
  • 举报
回复
用属性的方式
阿非 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hy86459 的回复:]
找不到类型或命名空间名称“control_wc”(是否缺少 using 指令或程序集引用?)
[/Quote]

你 的用户控件的类名叫什么
dl_wang 2009-04-10
  • 打赏
  • 举报
回复
赞同添加属性。在调用程序的page_load 里给属性赋值。我就是那样做的
hy86459 2009-04-10
  • 打赏
  • 举报
回复
找不到类型或命名空间名称“control_wc”(是否缺少 using 指令或程序集引用?)
LZWNEWSWAY96088 2009-04-09
  • 打赏
  • 举报
回复
为这个用户控件添加属性才是正解.
辰爸 2009-04-09
  • 打赏
  • 举报
回复
就像使用类的属性一样引用就成,3楼的方法可行!!!
zpcoder 2009-04-09
  • 打赏
  • 举报
回复
在ascx 控件里放一个 public 的参数就可以了

例如放个


private int _age=18;

public int Age
{
get {return _age;}
set {_age = (value>120) ? 18 : value }
}
阿非 2009-04-09
  • 打赏
  • 举报
回复

control_wc myControl = LoadControl("~/control/wc.ascx") as control_wc;

if(myControl !=null)
{
myControl.xxx="";
}
PlaceHolder1.Controls.Add(myControl);


control_wc 为ASCX 控件的类名

xxx为 它的属性
我姓区不姓区 2009-04-09
  • 打赏
  • 举报
回复
为它添加一个属性就可以了
opou 2009-04-09
  • 打赏
  • 举报
回复
mark

62,267

社区成员

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

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

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

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