Display multiline in Repeater Control? - c#

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>

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 ='<%# %>'

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;

Is it possible to edit a GridView cell without an edititemtemplate?

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="lclick">Buy</asp:LinkButton>
</ItemTemplate>
<edititemtemplate>
<asp:Textbox runat="server" id="txt"/>
<edititemtemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Is it possible to edit the GridView without the edititem Template?
I have done this before and I can only explain what I did
To make sure my edit controls is part of viewstate I added an empty form to the bottom of my page
<div id="myeditform">
<table>
<tr>
<td><asp:HiddenField runat="server" ID="myRowId" />
</td>
<td>... other controls </td>
<td>... Save button -- </td>
</tr>
<table>
</div>
The tr(s) has to match the number of columns in your grid or you can use colspan
Then
use jQuery or javascript to get the row (e.g. the nearest parent to the edit link clicked),
get the td(s) and pass the value of each td to the respective control in your edit form.
replace your <tr> with the content of the table in your edit form
when save is clicked, refresh the page to update gridview

Creating a repeater dynamically inside an accordion pane

My goal is to nest a Repeater inside an ASP.Net AJAX Accordion's AccordionPane.
So there is one Accordion which I am programmatically adding AccordionPanes to. The amount of panes I add depends on my particular dataset's count value, usually no more than 5. I've managed to do this successfully.
The thing I am having difficulty with is creating and adding a Repeater per each AccordionPane.
I've glanced over http://iridescence.no/post/Using-Templated-Controls-Programmatically.aspx but this is not exactly what I had in mind. Instead, I would rather declare a single Repeater as static HTML that I could then "clone" when I need. How can I achieve this? Obviously I would want each control's ID (within this declared Repeater) to be generated automatically each time i "clone" it.
The repeater looks like this:
<asp:Repeater ID="rptForum" runat="server">
<ItemTemplate>
<div runat="server" style="border:solid #d3d3d3 1px; border-bottom-width:0px;">
<table width="100%">
<tr><td align="left">
<asp:Label runat="server" Font-Size="12px" />
</td></tr>
<tr><td align="left">
>> <asp:Label runat="server" Font-Size="12px" Text='<%# Eval("query") %>' />
</td></tr>
</table>
</div>
</ItemTemplate>
</asp:Repeater>
I might add more fields to be databound as I progress.
Any ideas appreciated..
You should be able to do that by adding the repeater to the Content template:
<cc1:AccordionPane ID="AccordionPane1" runat="server">
<Header>
Foo
</Header>
<Content>
<asp:Repeater ID="Repeater1" runat="server" ...>
...
</asp:Repeater>
</Content>
</cc1:AccordionPane>
You can do a hierarchical data bind with the accordion, as illustrated here: http://aspalliance.com/1674_complex_data_binding_with_the_accordion_control

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