I've looked through several of the existing threads on this as well as looking through some examples posted online. And i can not get my nested repeater to work. I run into one of two problems generally:
1. either the ItemDataBound never gets called at all.
OR
2. I get a protection or object reference error.
Let me post some code (in C#) from the code-behind file:
private void PackageRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)I'm working with 2 repeaters. The outer one is called PackageRepeater and the inner one is CampaignRepeater. The Campaign.Findall is a method that was already in the project when i started working on it, and it works. Since if i use that as my datasource for the first repeater, i get data.
{
Trace.Write("Package Repeater Databound called");
CampaignRepeater = ((Repeater)e.Item.FindControl("CampaignRepeater"));
CampaignRepeater.DataSource = Campaign.FindAll(Expression.Eq("Package.Id", 2 ));
CampaignRepeater.DataBind();
}
My aspx page looks like this:
<asp:repeater id="PackageRepeater" runat="server">When i run the code this way, the inner repeater (CampaignRepeater) never renders. Looks like the ItemDataBound event never gets fired since my trace statement never shows up. If in the aspx page i include an OnItemDataBound=PackageRepeater_ItemDataBound, it then appears to call it but it gives me the error:
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<%# ((Model.Package)Container.DataItem).ShortName%><asp:repeater id="CampaignRepeater" runat="server" >
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<%# ((Model.Campaign)Container.DataItem).ShortName%>
<%# ((Model.Campaign)Container.DataItem).ShortName%>
<%# ((Model.Campaign)Container.DataItem).Id%>
<%# ((Model.Campaign)Container.DataItem).CountOfUsers()%>
<%# ((Model.Campaign)Container.DataItem).Id%></ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:repeater>
</ItemTemplate>
<FooterTemplate></FooterTemplate>
</asp:repeater
System.NullReferenceException: Object reference not set to an instance of an object.
That error occurs when i try to set the datasource for the inner (CampaignRepeater) repeater.
Does anyone have any idea what i am doing wrong here? I'm feeling a bit lost here.
thanks in advance for any help
I am struggling with something that should be fairly easy, but I think I am too flustered now to see...
By thekeggerman
Hello all,can anyone tell me the difference between the two connections OleDBConnection and the new ...
By mostafaadel
Does the Dispose() method of OleDbConnection automatically call OleDbConnection.ReleaseObjectPool()....
By anonymous
This code does not work it fails on Execute.::CoInitialize(NULL);_ConnectionPtr m_Connection("ADODB....
By mayankjha
Is there a way to retrieve the KeyInfo using the Microsoft Data Access Application Block for .NET? I...
By anonymous
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="OK" CancelText="Cancel" EditText="Edit...
By pradeepkonda
Is there anyway to have an editcommand inside an <itemtemplate>? I would just use the edit comman...
By jgallen_23
I know that there is unlimited of forum-treads about solutions to nested repeater problems.Still, I ...
By woppie, 1 Comments
I would like to make a editcommandcolumn either visable or hidden, based off a cookie (which is used...
By chingy, 6 Comments
Hi NewsGroup, Hope this is the right news group for this. Programming data access code in C#, .NET F...
By anonymous, 3 Comments
So I have a datagrid and it is updatable, however, I have another form on the page that has two boxe...
By cattrah, 1 Comments
my css inside in datagrid. It won't work, I don't know why.<HTML><body MS_POSITIONING="GridLay...
By hong_ma, 10 Comments
I have an editcommandcolumn of links that take on internal css attributes that are delcared in the h...
By cusoxty, 14 Comments
I am struggling with something that should be fairly easy, but I think I am too flustered now to see...
By thekeggerman, 4 Comments
Is it possible to make two separate links visible in one edit command column? I don't know much of t...
By cattrah, 1 Comments
Perhaps you are not hooking into the ItemDataBound event on the outer repeater? You should have something like this in OnInit or InitializeComponent (may vary between 1.1 and 2.0):
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...
infinitiesloop | Sat, 05 Jan 2008 09:55:00 GMT |
YANRQ
:P
infinitiesloop | Sat, 05 Jan 2008 09:56:00 GMT |
Argh sorry about that... heres the missing code
[asp:Repeater id="CampaignRepeater" runat="server" DataSource="[%# Campaign.FindAll(Expression.Eq("Package.Id", 2 )) %]"]
infinitiesloop | Sat, 05 Jan 2008 09:57:00 GMT |
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.
merk | Sat, 05 Jan 2008 09:58:00 GMT |
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
merk | Sat, 05 Jan 2008 09:59:00 GMT |
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?
infinitiesloop | Sat, 05 Jan 2008 10:00:00 GMT |
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
merk | Sat, 05 Jan 2008 10:01:00 GMT |
Have you tried
"CampaignRepeater = ((Repeater)e.Item.Controls[0].FindControl("CampaignRepeater"));"?
sophia | Sat, 05 Jan 2008 10:02:00 GMT |
no i didnt try that. Can you explain why i would need to do that?
thanks
merk | Sat, 05 Jan 2008 10:04:00 GMT |
Actually your code can work fine on my side.
sophia | Sat, 05 Jan 2008 10:05:00 GMT |
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:
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: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.
merk | Sat, 05 Jan 2008 10:06:00 GMT |
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... :)
infinitiesloop | Sat, 05 Jan 2008 10:07:00 GMT |
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?
merk | Sat, 05 Jan 2008 10:08:00 GMT |
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.
infinitiesloop | Sat, 05 Jan 2008 10:09:00 GMT |