在中继器中设置自定义 ASP.NET UserControl 变量

发布于 2024-08-25 22:54:38 字数 1618 浏览 2 评论 0原文

<%@ Register Src="~/Controls/PressFileDownload.ascx" TagName="pfd" TagPrefix="uc1" %>

<asp:Repeater id="Repeater1" runat="Server" OnItemDataBound="RPTLayer_OnItemDataBound">
 <ItemTemplate>
   <asp:Label ID="LBLHeader" Runat="server" Visible="false"></asp:Label>
   <asp:Image ID="IMGThumb" Runat="server" Visible="false"></asp:Image>
   <asp:Label ID="LBLBody" Runat="server" class="layerBody"></asp:Label>
   <uc1:pfd ID="pfd1" runat="server" ShowContainerName="false" ParentContentTypeId="55" />
   <asp:Literal ID="litLayerLinks" runat="server"></asp:Literal>
 </ItemTemplate>
</asp:Repeater>

System.Web.UI.WebControls.Label lbl;
System.Web.UI.WebControls.Literal lit;
System.Web.UI.WebControls.Image img;
System.Web.UI.WebControls.HyperLink hl;
System.Web.UI.UserControl uc;

我需要为中继器内列出的 uc1:pdf 设置 ParentItemID 变量。 我想我应该能够通过查看 e.Item 然后以某种方式设置它来找到 uc 。我认为这是我遗漏了一些东西的部分。

uc = (UserControl)e.Item.FindControl("pfd1");
if (uc != null) { uc.Attributes["ParentItemID"] = i.ItemID.ToString(); }

任何想法将不胜感激。

也尝试过类似的结果...当我在用户控件(pfd1)内调试时,我尝试设置的参数尚未设置。

uc = (UserControl)e.Item.FindControl("pfd1");
if (uc != null) 
{
  uc.Attributes.Add("ContainerID", _cid.ToString());
  uc.Attributes.Add("ParentItemId", i.ItemID.ToString());
}

更新:看起来我的控件没有通过名称空间连接。我已将父控件(Layer)和 PressFileDownlad 控件包装在命名空间“MyControls”中。还更新了他们在 aspx 上的继承参考以读取“MyControls.xxxxx”。我可以在 layer.aspx.cs 的代码中输入“MyControls.Layer”,但无法获取“MyControls.PressFileDownload”

<%@ Register Src="~/Controls/PressFileDownload.ascx" TagName="pfd" TagPrefix="uc1" %>

<asp:Repeater id="Repeater1" runat="Server" OnItemDataBound="RPTLayer_OnItemDataBound">
 <ItemTemplate>
   <asp:Label ID="LBLHeader" Runat="server" Visible="false"></asp:Label>
   <asp:Image ID="IMGThumb" Runat="server" Visible="false"></asp:Image>
   <asp:Label ID="LBLBody" Runat="server" class="layerBody"></asp:Label>
   <uc1:pfd ID="pfd1" runat="server" ShowContainerName="false" ParentContentTypeId="55" />
   <asp:Literal ID="litLayerLinks" runat="server"></asp:Literal>
 </ItemTemplate>
</asp:Repeater>

System.Web.UI.WebControls.Label lbl;
System.Web.UI.WebControls.Literal lit;
System.Web.UI.WebControls.Image img;
System.Web.UI.WebControls.HyperLink hl;
System.Web.UI.UserControl uc;

I need to set the ParentItemID variable for the uc1:pdf listed inside the repeater.
I thought I should be able to find uc by looking in the e.Item and then setting it somehow. I think this is the part where I'm missing something.

uc = (UserControl)e.Item.FindControl("pfd1");
if (uc != null) { uc.Attributes["ParentItemID"] = i.ItemID.ToString(); }

Any thoughts would be appreciated.

Also tried this with similar results... when I debug inside my usercontrol (pfd1) the parameters I am trying to set have not been set.

uc = (UserControl)e.Item.FindControl("pfd1");
if (uc != null) 
{
  uc.Attributes.Add("ContainerID", _cid.ToString());
  uc.Attributes.Add("ParentItemId", i.ItemID.ToString());
}

UPDATE: It looks like my controls are not connected by a namespace. I've wrapped by the parent control (Layer) and the PressFileDownlad control in a namespace "MyControls". Also updated their Inherits reference on the aspx to read "MyControls.xxxxx". I'm able to type "MyControls.Layer" inside the code on layer.aspx.cs but I'm not able to get "MyControls.PressFileDownload"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水溶 2024-09-01 22:54:38

如果您将 ParentItemID 实现为用户控件中的公共属性,那么您应该能够以声明方式设置它,例如:

<asp:Repeater id="Repeater1" ...>
 <ItemTemplate>
   <uc1:pfd ID="pfd1" runat="server" ParentItemId='<%# Eval("ItemID") %>' ... />

If you implement ParentItemID as a public property in your user control, then you should be able to set it declaratively, e.g:

<asp:Repeater id="Repeater1" ...>
 <ItemTemplate>
   <uc1:pfd ID="pfd1" runat="server" ParentItemId='<%# Eval("ItemID") %>' ... />
极致的悲 2024-09-01 22:54:38

马丁是对的,你应该能够以声明的方式设置它(如果你的财产是公共的)。
但你的方法也应该有效(只要正确地投射它)

((PressFileDownload)e.Item.FindControl("pfd1")).ParentItemId = 0;

Martin is right you should be able to set it in declarative way (in case your property is public) .
But your way should also work (just cast it properly)

((PressFileDownload)e.Item.FindControl("pfd1")).ParentItemId = 0;
執念 2024-09-01 22:54:38

最好的方法是为用户控件实现 OnDataBinding 事件。如果可能的话,我尽量避免使用 Web 表单将代码内联到 aspx 中。

当中继器被绑定时,对于每个绑定的项目,OnDataBinding 将为您的用户控件触发,并且您的处理程序可以执行其需要的操作。您不必去寻找控件。

下面是一个示例:

// in your aspx
<uc1:pfd ID="pfd1" runat="server" ShowContainerName="false" ParentContentTypeId="55"
    OnDataBinding="pfd1_DataBinding" />

// in your codebehind implement the OnDataBinding event
protected void pfd1_DataBinding(object sender, System.EventArgs e)
{
    pfd uc = (pfd)(sender);
    uc.ContainerID = _containerID.ToString();
    uc.ParentItemID = Eval("ItemID");

    // Here you can do more like access other items like hidden fields
    // or cached objects or even other controls etc... Skys the limit.
} 

编辑:从您的评论中注意到,您需要的数据多于数据源中找到的数据。在这种情况下,我通常所做的只是在存储数据的 .cs 中创建私有成员变量。因此,当您拥有容器 ID 时,只需将其存储在可访问的变量中。

例如,在页面的 .cs 中:

public partial class _TestPage : System.Web.UI.Page
{
    private int _containerID { get; set; }

然后,当您加载数据时,只需设置 _containerID 属性,即可在 OnDataBinding 事件中访问该数据。只需确保在设置 _containerID 后进行绑定即可。

The best way is to implement the OnDataBinding event for the user control. I try to stay away from putting code inline in the aspx using webforms if possible.

When the repeater gets bound, for each item that is bound, the OnDataBinding will fire for your user control and your handler can do what it needs. You don't have to go searching for the controls.

Here is an example:

// in your aspx
<uc1:pfd ID="pfd1" runat="server" ShowContainerName="false" ParentContentTypeId="55"
    OnDataBinding="pfd1_DataBinding" />

// in your codebehind implement the OnDataBinding event
protected void pfd1_DataBinding(object sender, System.EventArgs e)
{
    pfd uc = (pfd)(sender);
    uc.ContainerID = _containerID.ToString();
    uc.ParentItemID = Eval("ItemID");

    // Here you can do more like access other items like hidden fields
    // or cached objects or even other controls etc... Skys the limit.
} 

EDIT: Notice from your comment you require more data than what is found in the datasource. In this case what I usually do is just make private member variables in the .cs that I store data in. So when you have the container ID just store it in a variable that will be accessible.

Eg in your .cs for your page:

public partial class _TestPage : System.Web.UI.Page
{
    private int _containerID { get; set; }

Then when you load the data just set the _containerID property and it will be accessible in the OnDataBinding event. Just make sure you are binding after you have set the _containerID.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文