Creating a ListView Control in Default Edit Mode - c#

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.

Related

Trying to add Update panel in Item template.Getting 500 error

Pop-up is created as given in image.
In given image ListView is used. In each row last dropdown depends on the second dropdown, and the second depends on the first. Entire dropdown hierarchy is kept in one Update panel for each row. If I select first dropdown I am able to change second.
Dropdowns in layout template and insert template are kept in Update panel and it's working properly, but if I keep dropdowns in item template in Update panel I am getting 500 error.
Tried code is as follows-
<ItemTemplate>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server" />
</td>
<td>
<asp:Image ID="UserRoleImage" runat="server" />
</td>
<td>
--CheckBox Code--
</td>
<td>
--CheckBox Code--
</td>
<td>
--CheckBox Code--
</td>
<asp:updatepanel id="Updatepanel1" runat="server" updatemode="Conditional">
<ContentTemplate>
<td style="width:10%">
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</td>
<td style="width:10%">
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</td>
<td style="width:10%">
<asp:DropDownList ID="DropDownList3" runat="server">
</asp:DropDownList>
</td>
</ContentTemplate>
</asp:updatepanel>
<td>
<asp:ImageButton ID="DeleteButton" runat="server" CommandName="Delete" />
</td>
<td></td>
</tr>
</ItemTemplate>
If I remove Update panel it works fine but on dropdown change my pop-up is getting closed. I want to update only one row of listview of pop-up.
Exact same design is added in layout template and insert template.
Got the cause of crash. Since same id of Update panel was using in Item template and Alternate template. I removed Alternate template from list view and Table is rendering perfectly how I was looking for.

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>

ASP.NET Repeater Control Checkbox issue

Overview: Clicking on text of first CheckBox in a repeater control clicks the last one. I know why this is happening; association of label tag with element id chkMarkedForDeletion and when that label is clicked it selects the last CheckBox, as all the CheckBoxes have same ID.. damn you repeater control!! I am wondering if there is any way to avoid this? Easy way would be to create individual labels and associate correctly to each CheckBox but that defeats the supposed default behaviour of a CheckBox.
Repeater code:
<asp:Repeater ID="childNodesDataRepeater" runat="server">
<ItemTemplate>
<table style="width: 100%;" cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="width: 200px;">
<asp:CheckBox ID="chkMarkedForDeletion" runat="server" EnableViewState="true" Text="Remove"
Checked='<%# DataBinder.Eval(Container.DataItem, "IsMarkedForDeletion") %>' />
</td>
<td>
</td>
<td style="width: 200px;">
<asp:CheckBox ID="chkHighImpactCause" runat="server" EnableViewState="true" Text="High Impact Cause"
Checked='<%# DataBinder.Eval(Container.DataItem, "IsHighPriority") %>' />
</td>
</tr>
<tr>
<td colspan="3">
<asp:Label ID="header002" runat="server" Text="What caused this problem?" />
</td>
</tr>
<tr>
<td colspan="3">
<asp:TextBox ID="txtProblemCausedBy" runat="server" EnableViewState="true" Width="100%"
Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' /><br />
</td>
</tr>
<tr>
<td colspan="3" style="height: 5px;">
<hr />
<asp:HiddenField ID="nodeIdentifier" runat="server" EnableViewState="true" Value='<%# DataBinder.Eval(Container.DataItem, "AnalysisID") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Generated HTML Code for CheckBox:
<INPUT id=chkMarkedForDeletion type=checkbox name=TabContainer$tabProblemResolution$frmProblemResolution1$childNodesDataRepeater$ctl00$chkMarkedForDeletion>
<LABEL for=chkMarkedForDeletion>Remove</LABEL>
...
...
<INPUT id=chkMarkedForDeletion type=checkbox name=TabContainer$tabProblemResolution$frmProblemResolution1$childNodesDataRepeater$ctl02$chkMarkedForDeletion>
<LABEL for=chkMarkedForDeletion>Remove</LABEL>
Issue:
Thanks,
Abhi
Make sure the ClientIDMode on the repeater is not set to "Static".
This setting is inheritable from the parent, which goes all the way up to web.config. So if you have to set the ClientIDMode explicitly on the repeater, it means that some parent overrides the default (Predictable) to "Static".

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