Can't populate update parameters from a ListView's ItemTemplate - asp.net

I am using a ListView and would like to implement the switch from read only mode to edit mode for a ListView item on the client side. Some of this is discussed at: Inline form editing on client side
I am trying to do this by something like:
<asp:ListView ID="ListViewContactNumber" runat="server">
<LayoutTemplate>
<table cellpadding="0" cellspacing="0">
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<ItemTemplate>
<div class="readonly">
<asp:Label ID="LabelType" runat="server"
Text='<%# Server.HtmlEncode(Eval("Name").ToString()) %>'></asp:Label>
Edit
</div>
<div class="edit">
<asp:TextBox ID="TextBoxName" runat="server"
Text='<%# Eval("Name") %>'
MaxLength="256"
Columns="10"></asp:TextBox>
<asp:LinkButton ID="LinkButtonSave" runat="server"
Text="Save"
OnClick="LinkButtonSave_Click"></asp:LinkButton>
Cancel
</div>
</ItemTemplate>
</asp:ListView>
switchState is a Javascript function that simply hides/shows the DIVs with the readonly and edit classnames. In LinkButtonSave_Click I get the value from TexBoxName but it always contains the bound original value and not the edited value that was entered in the texbox.
Does ASP.NET not postback the textbox value because it is in the ItemTemplate or is it something else that's causing this problem?
Could I use a Repeater instead to accomplish this?

when using javascript to change state you must use ajax to update your data ::

It turned out that the ListView was not the source of the problem at all. I was re-databinding on Page_Load without checking if it was a postback.

Related

aspnet web forms An item with the same key has already been added

I am using asp net 4.5.2 with model binding; I have a problem that whenever I try to use model binding on DropDownList It raises the error An item with the same key has already been added but if I change it to SQLDataSource the error goes away.
Thanks in advance
Update
Sample Code
<asp:ListView runat="server" ID="lvChairItem" DataKeyNames="ChairItemId" InsertItemPosition="FirstItem" OnItemCanceling="lv_ItemCanceling" OnItemUpdated="lv_ItemUpdated" OnItemEditing="lv_ItemEditing" ClientIDMode="AutoID" OnCallingDataMethods="ChairItem_CallingDataMethods" SelectMethod="GetByChairId" InsertMethod="InsertItem" UpdateMethod="UpdateItem" DeleteMethod="DeleteItem" ItemType="App.Model.ChairItem">
.............
<InsertItemTemplate>
<tr>
........
<td>
<asp:DropDownList ID="ddlRoomId" CssClass="" DataValueField="Value" DataTextField="Text" AppendDataBoundItems="true" Text='<%# BindItem.RoomId %>' Width="100%" OnCallingDataMethods="Room_CallingDataMethods" SelectMethod="GetAllForDDL" runat="server" EnableViewState="true">
<asp:ListItem Value="">--Select--</asp:ListItem>
</asp:DropDownList>
</td>
........
</tr>
</InsertItemTemplate>
.....................
</asp:ListView>
Then the issue is likely because you are populating it on page load without checking for Page.IsPostBack. You don't want to re-populate the controls on each postback. You do want to (often) allow ViewState. So I would re-enable, and wrap your populate method in (not page.ispostback).

AsyncPostBackTrigger in nested repeater

Good day everyone,
This is my first post in here and I would like to thank you all for the great efforts in this forum by which I have already gaind a lot of skills.
I have a smalle issue with two nested repeaters. Basically, I have a dropdownlist in a child repeater which contains rating values and every time the dropdownlist is changed in the child repeater the new percentange is calculated and presented in a label in the parent repeater. This will cause full postback which is really frustrating when going through too many dropdownlists. My question is how to reflect the new calculated percentange in the label without postback. I have tried to use AsyncPostBackTriggers but no luck. Any suggestions would be appreiciated
<asp:Repeater ID="rptParent" runat="server">
<ItemTemplate>
<asp:Label ID="lblAvg" runat="server" Text='<%# Eval("TrialScore")%>'></asp:Label>
<asp:Repeater ID="rptChild" runat="server">
<ItemTemplate>
<asp:DropDownList ID="lstRate" runat="server" OnSelectedIndexChanged="lstRate_SelectedIndexChanged" />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
Wrap your aspx mark up inside update panel like this.
<asp:UpdatePanel runat="sever" ID="upParentChild" >
<ContentTemplate>
<asp:Repeater ID="rptParent" runat="server">
<ItemTemplate>
<asp:Label ID="lblAvg" runat="server" Text='<%# Eval("TrialScore")%>'></asp:Label>
<asp:Repeater ID="rptChild" runat="server">
<ItemTemplate>
<asp:DropDownList ID="lstRate" runat="server" OnSelectedIndexChanged="lstRate_SelectedIndexChanged" />
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
This will make sure that only controls inside update panel are posted back and not the whole page.

Asp.net page still flickers with UpdatePanel

I have a page which contains a Listview,. The ItemTemplate will contain many Checkboxes for each returned value from it's datasource. These Checkboxes has a OnCheckedChanged function and when a user selects the checkbox, the page flickers...I have investigated UpdatePanels, but to no avail. Also, this page contains a MasterPage.
Code:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:ListView ID="lvTypes" runat="server" DataSourceID="XXX" GroupItemCount="4">
<ItemTemplate>
<td style="background-color: #4b6c9e;" align="left">
<asp:CheckBox ID="Type" runat="server" Text='<%# Eval("Type") %>' ForeColor="White" OnCheckedChanged="chk_CheckedChanged" AutoPostBack="True" CssClass="myCheck" Font-Size="45px" />
</td>
...
...
...
Obviously, I am not grasping the concept of the UpdatePanel. Can you direct me on how to implement this for this scenario"?
Thanks.
If you use UpdateMode = "Conditional" it'll do a partial refresh.
<asp:UpdatePanel ID="UpdatePanel2" UpdateMode = "Conditional" runat="server">
Read MSDN to get an idea of the UpdatePanel control.
And, also have a look at this comprehensive explanation in MSDN

Selecting items of a Listview using checkboxes in ASP .NET

I am trying to use checkboxes to select the items in a Listview. I have added a checkbox control in the , and they are displayed properly.
The problem is that Checked property never changes when I click on them. Why does this happen? And is there a workaround?
Here is the code:
<asp:ListView ID="ListView1" runat="server"
onitemcommand="ListView1_ItemCommand"
onitemdatabound="ListView1_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBoxSelect" runat="server" OnCheckedChanged="CheckBoxSelect_checkchanged"/>
<asp:LinkButton ID="LinkButtonOpen" CommandArgument='<%#Eval("MessageID") %>' runat="server">
<asp:Label ID="Label1" Text="[]" runat="server"/>
<%#Eval("FirstName" )%> <%#Eval("LastName")%>
<%#Eval("Subject") %>
<%#Eval("Timestamp") %>
<asp:HiddenField runat="server" ID="ReadStatus" Value='<%#Eval("IsRead") %>' />
</asp:LinkButton>
</ItemTemplate>
</asp:ListView>
I think you are missing the AutoPostBack="true"
<asp:CheckBox ID="CheckBoxSelect" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBoxSelect_checkchanged"/>
The problem was that I was binding the ListView during page load. When that happened, the checkboxes would get cleared, and I got the Checked property as False in all subsequent functions.

how to bind the database value to a div?

in my application i want to bind the data to a div, which is in a datalist so how can i bind the value .thank you
Use a Panel that contains a Literal control. The Panel will render as a div and the Literal control renders as text. You will data bind to the Literal control's Text property. This will give you what you want. A div that contains text for each item displayed in a DataList control.
Be creative in combining controls to give you the layout, format, data-binding, and so on that you need. If it gets too complex, create a user-control
use this is very useful
<ItemTemplate>
<asp:Label runat="server" ID="lblLocationId" Text='<%#Eval("LocationID") %>' ></asp:Label>
<asp:Panel Wrap="true" ID="Panel1" Width="140px" Font-Underline="true" Font-Italic="true" ForeColor="Black" ScrollBars="None" runat="server">
<%-- <div id="div1" runat="server"></div>--%>
<asp:Literal runat="server" Text='<%#Eval("Solution") %>'>
</asp:Literal>
</asp:Panel>
</ItemTemplate>
Use <%# Eval("Name of your data field") %> to bind to properties in your datasource.

Resources