【问题标题】:How to pass values - in a loop - to a user control如何在循环中将值传递给用户控件
【发布时间】:2015-06-26 00:33:27
【问题描述】:

用于测试动态调用用户控件的页面代码。

    <%@ 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>";
    }

}

所以,我有一个引用用户控件的页面。我想动态调用该用户控件,并且在循环一些数据时需要将用户ID 从页面传递到用户控件。在我上面的示例代码中,我从 0 循环到 4,并且用户控件被“调用”5 次 - 因为名称“Angela”被写入屏幕 5 次。

但是,如何将 UserID(在循环中)传递给 UserControl?我在用户控件中有一个 UserID 的公共属性,但是在“调用”用户控件的页面中 - 如果我在该行中发表评论...

myUserControl.UserID = i;

报告错误...“System.Web.UI.UserControl”不包含“UserID”的定义,并且没有扩展方法“UserID”接受“System.Web.UI.UserControl”类型的第一个参数可以找到(您是否缺少 using 指令或程序集引用?)

如何将 UserID 传递给我的用户控件 - 在我拥有的循环内?

【问题讨论】:

    标签: c# asp.net user-controls


    【解决方案1】:

    您将控制权投射到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);
        }
    }
    

    【讨论】:

    • 非常感谢。已经到了只见树木不见森林的地步。
    • 不客气 :)。我们都有过这样的时刻。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多