Repeater with textboxes programmatically - c#

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>

Related

how to display a single column values in multiple columns in gridview

I display the list of table names when a particular database is selected from the drop down list. Number of tables in my database is more than 100. I don't want to display it as a single column and scroll the page. I want to display it as a table. Also each table name is a link to view its corresponding table details. can anyone help me with the technique to achieve it ?
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<td colspan="5">
Tables
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<%--<tr>--%>
<td><asp:Label ID="lbl_TableName" runat="server" Text='<%#Eval("table_name")%>' /></td>
<%-- </tr>--%>
</ItemTemplate>
<%-- <SeparatorTemplate>
<td colspan="4"> name</td>
</SeparatorTemplate>--%>
<%-- <AlternatingItemTemplate>
<td> break</td>
</AlternatingItemTemplate>--%>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

Child repeater does not exist in current context

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;

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.

ListView styling

I am currently fighting against a Listview, I'm trying to have two images (with headlines on etc.) beside each other, but I can't figure it out.
My code looks like this:
<asp:ListView runat="server" ID="LWArticleList">
<ItemTemplate>
<table id="TableArticleList" class="repclass">
<tr>
<td>
<div class="thumb main">
<p class="thumb-comment">
<asp:HyperLink ID="CommentHyperLink" runat="server" CssClass="comment-count">10</asp:HyperLink></p>
<asp:HyperLink ID="ArticleLink" runat="server" NavigateUrl='<%# string.Format("~/Article.aspx?id={0}", Eval("ID")) %>'>
<asp:Image ID="Image1" Width="250px" Height="170px" runat="server" ImageUrl='<%# string.Format("{0}/{1}", ImageService.ImageLocation, Eval("Image")) %>' />
</asp:HyperLink>
<div class="thumb-title">
<h2><asp:HyperLink ID="LinkTitle" runat="server" Text='<%# Eval("Headline") %>' /></h2>
<p><asp:Literal ID="LitSummary" runat="server" Text='<%# Eval("Summary") %>' /></p>
</div>
</div>
</td>
</tr>
</table>
</ItemTemplate>
</asp:ListView>
I wish my content to be shown like this:
Article 1 | Article 2
Article 3 | Article 4
But at the moment mine looks like this:
Article 1
Article 2
Article 3
Article 4
How can I change this?
It could be because you only have one table cell <td> so this will only ever be a 1 column listview.
For example, to display a two-column table, you would render three table cells (<td>) in each table row (<tr>), like so:
<table ...>
<tr>
<td>Article 1</td>
<td>Article 2</td>
</tr>
...
<tr>
<td>Article N - 1</td>
<td>Article N</td>
</tr>
</table>
You are displaying your data in a table for each item in your data source. You should rethink that approach.
Tables' display property is block by default, so this is why your items display as they do.

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.

Categories

Resources