views:

486

answers:

3
<%@ 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"

+3  A: 

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") %>' ... />
M4N
That part worked, but I've got another couple parameters that aren't part of the binding item I need to pass in - they are members of the a the parent control that is housing all this goodness. When I put the eval bit in that piece worked but the next part (ContainerID) isn't part of that particular repeater item.
tnriverfish
I ended up building a work around that allowed me to be able to drop the necessary items in this way.
tnriverfish
+2  A: 

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;
UshaP
If I paste your line into my code the "PressFileDownload" returns and error and intelesense doesn't pick it up either. How do I make one of my usercontrols showup and be recognized? Thanks
tnriverfish
+1  A: 

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.

Kelsey
When I put in this line both instances of "TestUserControl" cause error as if it doesn't know what I mean.pfd uc = (pfd)(sender);It also does this if I put in your sample.TestUserControl uc = (TestUserControl)(sender);
tnriverfish
I have the container variable set up the way you mention and it is set from outside before loading.
tnriverfish
Is the user control in the same namespace as your page? It should know about it if it is. Also I hope your not typing in `TestUserContol` as is. That is just a name I made up, replace it with the type for YOUR user control. I will update the example code here to reflect your names so that it won't be so confusing.
Kelsey
no. I understood you. The "parent" item isn't a page it is another control and the pfd control is referenced inside of it.
tnriverfish
So you need to include the namespace of your control in your parent usercontrol or use the full qualified name when doing the cast. Update your question with a note stating exactly where you are stuck now... it's getting hard to follow what you can and can't get working.
Kelsey
Kelsey, thanks. I added an update.
tnriverfish