PackageRepeater.ItemDataBound += new RepeaterItemEventHandler(PackageRepeater_ItemDataBound);
Also you might consider a little trick.. you could set the DataSource declaratively. I can't use the code insert feature at the moment so bare with me... substitute brackets for angle brackets:
[asp:Repeater id="CampaignRepeater" runat="server" DataSource="
Then you wouldnt need the databound event at all. One other thing.. I notice the parameter you are passing for the inner repeater doesnt have anything to do with the dataitem of the outer one. Did you mean to hard code the '2' there? You can get to the dataitem within that declaritive code to get a foreign key off it if thats what you meant...
YANRQ
:P
Argh sorry about that... heres the missing code
[asp:Repeater id="CampaignRepeater" runat="server" DataSource="[%# Campaign.FindAll(Expression.Eq("Package.Id", 2 )) %]"]
this is for 1.1 ... i should have mentioned that.
I tried doing it declaritively but that also gave me an error message (which i dont remember at the moment).
I do not have anything like the eventhandler code... i didnt see that mentioned in the msdn site or any other the online samples i found. I'll give that a try.
as for the hardcode value... thats just there temporaily. I want to make sure the datasource actually has data. Once i get the repeater working, my next question would probably be how to grab a value from the packagerepeater and use it in the data source call for the campaignrepeater ;) But i think i know how to do that.
is it possible the FindControl isnt finding anything? Because i put a break point just after that line. and i assume i should see the CampaignRepeater listed as a control of the PackageRepeater, but i dont. Its listed along side it, but with an undefined value. And I'm getting a null reference error when i try to do the set the datasource. so maybe the findcontrol isnt finding anything, which is where i am getting the null refernce.
If so, how can i test to see what FindControl is finding (if anything), and if its not finding something, what should i look at to see why its not finding it?
thanks for the help
Well if FindControl isn't finding it then thats definitely a clue... but it should, I dont see any issues with what you've got. If your CampaignRepeater variable reference is still null after you assign it to FindControl, then FindControl isn't finding it. Only way that could happen is if you have misspelled the ID, or if the repeater is actually within another control that implements INamingContainer. Is the source you posted complete or abbreviated?
the source was appreviated. I didnt want to clutter it up if people were going to try and read it and offer some advice.
i still dont know what was wrong, but i got it working by just starting the page over. So i guess there was some sort of syntax error somewhere.
thanks for the help
Have you tried
"CampaignRepeater = ((Repeater)e.Item.Controls[0].FindControl("CampaignRepeater"));"?
sophia:
Have you tried
"CampaignRepeater = ((Repeater)e.Item.Controls[0].FindControl("CampaignRepeater"));"?
no i didnt try that. Can you explain why i would need to do that?
thanks
Actually your code can work fine on my side.
Ok ... i know how to set an eventhandler for the repeater ... next question - how do i add an event handler for the nested repeater?
i tried doing the same thing i did for the outer repeater:
this.PackageRepeater.ItemDataBound +=new System.Web.UI.WebControls.RepeaterItemEventHandler(this.PackageRepeater_ItemDataBound);The first line is the outer level repeater and the 2nd one is the nested one. When i try to run the page i get the error:
this.CampaignRepeater.ItemDataBound +=new System.Web.UI.WebControls.RepeaterItemEventHandler(this.CampaignRepeater_ItemDataBound);
this.Load +=new System.EventHandler(this.Page_Load);
System.NullReferenceException: Object reference not set to an instance of an object.
I assume i have to reference the control through the outer repeater somehow? not sure how though.
There's only one outer repeater, but there's N-number of inner repeaters, so you can't hook into it's event in the traditional sense. You'll have to hook into the ItemCreated event in the outer repeater, find the inner one, then hook into its event there. You'll be hooking into each of the inner repeaters.
One thing... why do you have a this.CampaignRepeater at all? That's only going to cause you confusion... you cant refer to the inner repeater that way. It isn't a control on the form like the outer repeater is, its just a control that is part of a TEMPLATE. And that template is instantiated once for every item in the outer repeater. It doesn't exist until the template is instantiated (when you databind), and even then, it's created multiple times. Having a single reference to it on the form doesn't make sense, so just get rid of that reference entirely, it's just going to make you think you can do things like this. That's also why its null by the way... it doesn't exist.
I hope that makes sense... :)
i was just using "this." because thats what was there previously and it worked. So i was guessing it would work again (i guessed wrong).
here's the layout of what the page looks like (and its working):
<package repeater>
<campaign repeater>
</campaign repeater>
</package repeater
<campaigns with no package repeater>
</campaigns with no package repeater
and here's is something like what i am trying to do:
<package repeater>
<campaign repeater>
<checkbox id="campaign#">
</campaign repeater>
</package repeater
<campaigns with no package repeater>
<checkbox id="campaign#">
</campaigns with no package repeater
Basically, for each row in the 2 campaign repeaters, i need to include a checkbox which is not going to have the same datasource as the repeaters since i need to use a different function to get their values. And i will then need to go through each checkbox when the page is submitted, gather the values and do my data processing.
Any suggestions on how to go about doing this?
Well you're going to just iterate over the repeater items, find the checkbox, and take appropriate action. For each item in the package repeater you will FindControl to get the campaign repeater, then for each item in that repeater, find the checkbox. Then the "NoPackageCampaignRepeater" you do the same.
Perhaps where you are getting messed up is that the "CampaignRepeater" control you have declared is referring to the one that is outside of the PackageRepeater. When you need to deal with one of the inner repeaters, use a new local variable, dont use the "this." one since thats referring to the one outside of the repeater...
You're totally on the right track with the ItemDatabound event, I just think you may have some issues with confused references or something. If you want to send me the complete source perhaps I can help in more detail. Either link through to my blog to get my email or send me a private message. I'll try to look at it when I have some time.
No comments:
Post a Comment