如何在循环中将值传递给用户控件
How to pass values - in a loop - to a user control
用于测试动态调用用户控件的页面代码。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestUC.aspx.cs" Inherits="TestUC" %>
<%@ Register TagPrefix="UC" TagName="TestUC" Src="NCCsByRole.ascx" %>
<!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">
<div>
<asp:Placeholder runat="server" ID="PlaceHolder1"></asp:Placeholder>
</div>
</form>
</body>
</html>
此页面的隐藏代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TestUC : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
UserControl myUserControl = (UserControl)LoadControl("TeamsByRole.ascx");
//myUserControl.UserID = i; ******************** NOT WORKING
PlaceHolder1.Controls.Add(myUserControl);
}
}
}
用户控件的代码。
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TeamsByRole.ascx.cs" Inherits="TeamsByRole" %>
<asp:Literal ID="ltlName" runat="server"></asp:Literal>
以及用户控件的隐藏代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TeamsByRole : System.Web.UI.UserControl
{
private int _UserID;
public int UserID
{
get { return _UserID; }
set { _UserID = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
string myName = "Angela";
ltlName.Text = "<p>" + myName + "</p>";
}
}
所以,我有一个引用了用户控件的页面。我想动态调用该用户控件,并且我需要在遍历某些数据时将 UserID 从页面传递到用户控件。在我上面的示例代码中,我从 0 循环到 4 并且用户控件被 'called' 5 次 - 因为名称 'Angela' 被写入屏幕 5 次。
但是,如何将 UserID(在循环中)传递给 UserControl?我在用户控件中有一个 public 属性 的 UserID,但是在 'calling' 用户控件的页面中 - 如果我在 ...
行中发表评论
myUserControl.UserID = i;
报告了一个错误...'System.Web.UI.UserControl' 不包含 'UserID' 的定义并且没有扩展方法 'UserID' 接受类型 'System.Web.UI.UserControl' 的第一个参数可以是找到(您是否缺少 using 指令或程序集引用?)
如何将 UserID 传递到我的用户控件 - 在我拥有的循环中?
您正在将控件投射到 UserControl
,但 UserID
未在 UserControl
上声明。相反,它是 TeamsByRole
的成员,这是您应该转换为的成员。
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
TeamsByRole myUserControl = (TeamsByRole)LoadControl("TeamsByRole.ascx");
myUserControl.UserID = i;
PlaceHolder1.Controls.Add(myUserControl);
}
}
用于测试动态调用用户控件的页面代码。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestUC.aspx.cs" Inherits="TestUC" %>
<%@ Register TagPrefix="UC" TagName="TestUC" Src="NCCsByRole.ascx" %>
<!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">
<div>
<asp:Placeholder runat="server" ID="PlaceHolder1"></asp:Placeholder>
</div>
</form>
</body>
</html>
此页面的隐藏代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TestUC : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
UserControl myUserControl = (UserControl)LoadControl("TeamsByRole.ascx");
//myUserControl.UserID = i; ******************** NOT WORKING
PlaceHolder1.Controls.Add(myUserControl);
}
}
}
用户控件的代码。
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="TeamsByRole.ascx.cs" Inherits="TeamsByRole" %>
<asp:Literal ID="ltlName" runat="server"></asp:Literal>
以及用户控件的隐藏代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TeamsByRole : System.Web.UI.UserControl
{
private int _UserID;
public int UserID
{
get { return _UserID; }
set { _UserID = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
string myName = "Angela";
ltlName.Text = "<p>" + myName + "</p>";
}
}
所以,我有一个引用了用户控件的页面。我想动态调用该用户控件,并且我需要在遍历某些数据时将 UserID 从页面传递到用户控件。在我上面的示例代码中,我从 0 循环到 4 并且用户控件被 'called' 5 次 - 因为名称 'Angela' 被写入屏幕 5 次。
但是,如何将 UserID(在循环中)传递给 UserControl?我在用户控件中有一个 public 属性 的 UserID,但是在 'calling' 用户控件的页面中 - 如果我在 ...
行中发表评论myUserControl.UserID = i;
报告了一个错误...'System.Web.UI.UserControl' 不包含 'UserID' 的定义并且没有扩展方法 'UserID' 接受类型 'System.Web.UI.UserControl' 的第一个参数可以是找到(您是否缺少 using 指令或程序集引用?)
如何将 UserID 传递到我的用户控件 - 在我拥有的循环中?
您正在将控件投射到 UserControl
,但 UserID
未在 UserControl
上声明。相反,它是 TeamsByRole
的成员,这是您应该转换为的成员。
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
TeamsByRole myUserControl = (TeamsByRole)LoadControl("TeamsByRole.ascx");
myUserControl.UserID = i;
PlaceHolder1.Controls.Add(myUserControl);
}
}