在中继器中设置自定义 ASP.NET UserControl 变量
<%@ 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您将
ParentItemID
实现为用户控件中的公共属性,那么您应该能够以声明方式设置它,例如:If you implement
ParentItemID
as a public property in your user control, then you should be able to set it declaratively, e.g:马丁是对的,你应该能够以声明的方式设置它(如果你的财产是公共的)。
但你的方法也应该有效(只要正确地投射它)
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)
最好的方法是为用户控件实现
OnDataBinding
事件。如果可能的话,我尽量避免使用 Web 表单将代码内联到 aspx 中。当中继器被绑定时,对于每个绑定的项目,
OnDataBinding
将为您的用户控件触发,并且您的处理程序可以执行其需要的操作。您不必去寻找控件。下面是一个示例:
编辑:从您的评论中注意到,您需要的数据多于数据源中找到的数据。在这种情况下,我通常所做的只是在存储数据的 .cs 中创建私有成员变量。因此,当您拥有容器 ID 时,只需将其存储在可访问的变量中。
例如,在页面的 .cs 中:
然后,当您加载数据时,只需设置
_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:
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:
Then when you load the data just set the
_containerID
property and it will be accessible in theOnDataBinding
event. Just make sure you are binding after you have set the_containerID
.