Child repeater does not exist in current context - c#

I have a nested repeater inside of another repeater like this:
<table>
<asp:Repeater ID="RepeaterOuter" runat="server">
<ItemTemplate>
<tr>
<td><asp:TextBox Text='<%# Eval("Author") %>' /></td>
</tr>
<asp:Repeater ID="RepeaterInner" runat="server">
<ItemTemplate>
<tr>
<td><asp:TextBox Text='<%# Eval("Book") %>' /></td>
<td><asp:TextBox Text='<%# Eval("PublishDate") %>' /></td>
<td><asp:TextBox Text='<%# Eval("Pages") %>' /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</table>
However, when I try to access the child repeater, RepeaterInner, from my code behind file, it says that it does not exist in the current context. The parent repeater, RepeaterOuter, does however.
I am trying to set up a loop, to loop through my TextBox's in the child repeater but it won't let me access it:
//does not work
foreach (RepeaterItem item in RepeaterInner.Items)
{
txtBook= (TextBox)item.FindControl("Book");
txtPublishDate = (TextBox)item.FindControl("PublishDate");
txtPages = (TextBox)item.FindControl("Pages");
// do something....
}
Thank you.

At first, I very much doubt this inner repeater even exists before the outer one is data bound. So make sure you are accessing inner repeater at a right time.
At second, controls that are in templates are not visible like this on the page. To get the control in the template you need to use FindControl. Also note that FindControl works only with direct children, so your code should look something like this:
var innerRepeater = RepeaterOuter.Items[0].FindControl("RepeaterInner") as Repeater;

Related

Attempting to hide <td> from C# causes error: The name '' does not exist in the current context

Goal: I am trying to (conditionally) hide a value depending on my settings
What I've tried:
I first tried to do it like I have in other places, but I was hiding asp controls like Panel. This is just html.
When looking this up, it was mentioned to add:
1. id
2. runat=server
Problem: I tried these, but I am getting the error:
"The name 'groupid' does not exist in the current context"
CODE:
.aspx:
<td id="groupid" runat="server">
.cs:
groupid.Visible = true;
is in an ItemTemplate for a asp:ListView:
<asp:ListView>
<LayoutTemplate>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
If the Control is in a ListView (or a Repeater, GridView etc.) you have to use FindControl. And because you use a "normal" td with runat=server you need to cast it to an HtmlTableCell.
HtmlTableCell htc = ListView1.Items[1].FindControl("groupid") as HtmlTableCell;
htc.Attributes.Add("style", "background-color: red");
//or
htc.Visible = false;
Although I would recommend against hiding individual table cells. This can give weird results in a browser.
td is table cell in HTML.
In ASP.NET you can use <asp:TableCell> which is equivalent to td.
Give the Id to <asp:TableCell> instead of using <td>
Does this solve your problem?
Updates:
C#:
protected void ListView1_DataBound(object sender, EventArgs e)
{
ListView1.FindControl("tdotherItem").Visible = false;
}
ASPX:
<asp:ListView ID="ListView1" runat="server" DataSourceID="MyDataSource" ItemPlaceholderID="itemPlaceHolder">
<LayoutTemplate>
<table>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td runat="server" id="myCol" visible='<%# (bool)Eval("otherItem") %>'>
<%# Eval("other") %>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
Notice the use of visible ='<%# %>'

Repeater with textboxes programmatically

My object is to create something like table with number of columns and 5 rows.
I decided that i can do that with asp.net control repeater. Maybe there is a better way you should tell me?
So what i need as result is a number of columns in the first one there will be headers, in the next three rows it should be textboxes, with appropriete name like the txt+name of the header, and the last row should have checkboxes also with appropriete name.
So what i wanted to do is define one column as it should look like and then do the rest programmatically, or just bind IList (the names of the headers) and the other columns should be created.
Is this possible and how to do that?
What i have now is something like that:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<td>%#Container.DataItem("value")%</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:TextBox ID="TextBox1" runat="server"/></td>
<td><asp:TextBox ID="TextBox2" runat="server"/></td>
<td><asp:TextBox ID="TextBox3" runat="server"/></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

Creating a ListView Control in Default Edit Mode

I have a ListView Control in Asp.net 4.0 C#
I am attempting to make the default mode = Edit Mode with text boxes. So I took the Item Template (the default template) and replaced my databound labels with TextBoxes.
It works ok. Except the text Boxes only apply to every other row. So half the rows are text boxes on Page Load and half the Roes are still Labels. Here is my code:
<ItemTemplate>
<tr style="">
<td>
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
</td>
<td>
<asp:TextBox ID="DiscountPercentageTextBox" runat="server"
Text='<%# Bind("DiscountPercentage") %>' />
</td>
<td>
<asp:TextBox ID="CashTextBox" runat="server" Text='<%# Bind("Cash") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table>
<thead>
<tr>
<th>Title </th>
<th>DiscountPercentage</th>
<th>Cash</th>
</tr>
</thead>
<tbody>
<asp:PlaceHolder runat="server" ID="itemPlaceholder" />
</tbody>
</table>
</LayoutTemplate>
And here is the result:
You see? Every other row is a textbox. I need all rows to be textboxes. What am i doing wrong?
Huh, do you have an AlternatingItemTemplate defined? The ListVIew has no intelligence to make the original fields as labels....
Try making the ALternatingItemTemplate as the ItemTemplate too, if you don't have one defined.

How do I make a repeater with no datasource function until it gets one from a postback?

I have a repeater on my page which I use to display a list of search results. My issue is that the page keeps throwing me a
Parser Error Message: The server tag is not well formed.
error because the repeater has no datasource
Repeater:
<asp:Repeater runat="server" ID="rptSearchResults" >
<HeaderTemplate>
<h3>Search results</h3>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label runat="server" ID="lblTitle" Text="<%# Eval("title")%>"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblAdress" Text="<%# Eval("adress")%>"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblZipcode" Text="<%# Eval("zipcode")%>"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblCity" Text="<%# Eval("city")%>"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblType" Text="<%# Eval("type")%>"></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Above this repeater is a form where users can type in search words for primarily title, adress, zipcode, city and type. The repeater isn't supposed to fill out untill the user clicks the button which triggers the search and thus adds a datasource to the repeater.
Is there a way to make it work like I want it to?
I don't think the lack of a data source is the problem - it should be fine. The error says "The server tag is not well formed." - this means there's a problem with the markup. A problem with an empty data source would cause a NullReferenceException or something similar. So, maybe the problem is your Label elements - try changing the Text attributes from this:
Text="<%# Eval("type")%>"
to this:
Text='<%# Eval("type")%>'
I think all the double quotes will confuse ASP.Net. Use a combination of single and double quotes.
What happens if you disable the repeater control by default? Does it still throw the exception?
If disabling it doesn't work I'd add it dynamically as and when you need it. So that you can keep your template you can strip it out to a user control so you only have to add the user control through code and not the entire item template.

Display multiline in Repeater Control?

I am creating a Facebook Wall like structure and used repeater control for that using C#-Visual Studio 2010 amd Sql Server 2005 as database.
I am unable to display multiline text in repeater control
Script
<asp:TextBox ID="TextBox1" runat="server" Width="100%" TextMode="MultiLine"></asp:TextBox>
<asp:Repeater ID="myrepeater" runat="server">
<HeaderTemplate>
<table width="100%" style="font: 8pt verdana">
<tr style="background-color:#3C78C3">
<th>SCRAPS</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#DataBinder.Eval(Container,"DataItem.scraps") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
So, When I click on submit button, text stores in DataItem.scraps (which is column in my database table) and displays in repeater control.
But when I am inputting multiline Text in Textbox, it is displayed singleline in repeater control.
I want it to be displayed multiline repeater control.
So what is the problem exactly
1) Is it storing singleline text in database
2) or, repeater control problem
And, how to resolve it?
Please help me.
Thanks in advance,
Nikhil
The repeater control is pretty straight forward. There is no reason you can't control your output very thoroughly, just put everything in your item template:
<asp:Repeater ID="rptWall" runat="server">
<HeaderTemplate>
<h1>The wall<h1>
</HeaderTemplate>
<ItemTemplate>
<hr>
<asp:TextBox id="txtPost" runat="server" textmode="multiline" text='<%#DataBinder.Eval(Container.DataItem, "Posttext")%>'></asp:TextBox>
<hr>
</ItemTemplate>
<FooterTemplate>
<h3>put stuff here to add post</h3>
</FooterTemplate>
</asp:Repeater>

Categories

Resources