如我有用户控件<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Employee.ascx.cs" Inherits="WebApplication3.Employee" %>
public partial class Employee : System.Web.UI.UserControl
{
public string FullName { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(FullName);
}
}然后有页面:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %><%@ Register Src="Employee.ascx" TagName="Employee" TagPrefix="uc1" %>
<!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>
<title></title>
</head>
<body>
<uc1:Employee ID="Employee1" runat="server" FullName="123" />
<uc1:Employee ID="Employee2" runat="server" FullName='<%= FullName %>' />
</body>
</html>
public partial class WebForm1 : System.Web.UI.Page
{
public string FullName = "Jerry Zhang";
protected void Page_Load(object sender, EventArgs e)
{ }
}以上内容只能对WebForm1.aspx进行修改,那如何将cs中的FullName赋值给Employee2谢谢
public partial class Employee : System.Web.UI.UserControl
{
public string FullName { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(FullName);
}
}然后有页面:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %><%@ Register Src="Employee.ascx" TagName="Employee" TagPrefix="uc1" %>
<!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>
<title></title>
</head>
<body>
<uc1:Employee ID="Employee1" runat="server" FullName="123" />
<uc1:Employee ID="Employee2" runat="server" FullName='<%= FullName %>' />
</body>
</html>
public partial class WebForm1 : System.Web.UI.Page
{
public string FullName = "Jerry Zhang";
protected void Page_Load(object sender, EventArgs e)
{ }
}以上内容只能对WebForm1.aspx进行修改,那如何将cs中的FullName赋值给Employee2谢谢
<head>
<title></title>
<script language="c#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Employee2.FullName = this.FullName;
}
</script>
</head>
<body>
<uc1:Employee ID="Employee1" runat="server" FullName="123" />
<uc1:Employee ID="Employee2" runat="server"/>
</body>
</html>
<head>
<title></title>
<script language="c#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
this.DataBind();
}
</script>
</head>
<body>
<uc1:Employee ID="Employee1" runat="server" FullName="123" />
<uc1:Employee ID="Employee2" runat="server" FullName='<%# FullName %>'/>
</body>
</html>
在asp.net中给此属性赋值.
<script language="c#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Employee2.FullName = this.FullName;
}
</script>这种方法 他会把我在cs中的Page_Load替换掉,而不去执行我cs的Page_Load
所以不行
protected void Page_Init(object sender, EventArgs e)
{
this.Load += new System.EventHandler(this.Page_Load2);
}
<uc1:Employee ID="Employee1" runat="server" FullName="123" />
<script language="c#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
foreach (var control in this.Page.Controls)
{
var p = control.GetType().GetProperty("ID");
var pf = control.GetType().GetProperty("FullName");
if (pf != null)
{
pf.SetValue(control, this.FullName, null);
}
}
}
</script>
<uc1:Employee ID="Employee2" runat="server" />
</body>但是我后台的 protected void Page_Load(object sender, EventArgs e)
{
Response.Write("哈哈");
}就没有执行了
<!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>
<title></title>
<script language="c#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
this.DataBind();
}
</script>
</head>
<body>
<uc1:Employee ID="Employee1" runat="server" FullName="123" />
<uc1:Employee ID="Employee2" runat="server" FullName='<%# FullName %>'/>
</body>
</html>