Selecting items of a Listview using checkboxes in ASP .NET - 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.

Related

Access model bound item inside nested UpdatePanel

What is the correct method to access the data-bound item inside of a nested UpdatePanel? I've recently discovered model binding and love the strongly-typed binding it offers, but I have a scattering of UpdatePanels throughout the page, and it seems to break access to the Item object. If the entire databound control is inside the UpdatePanel I have no problem. This works:
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Repeater runat="server" ID="myRepeater" ItemType="Test.Person">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Item.Name %>' /><br />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
But if the UpdatePanel is inside the bound control, I no longer have access to Item. This does not work:
<asp:Repeater runat="server" ID="myRepeater" ItemType="Test.Person">
<ItemTemplate>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Label runat="server" Text='<%# Item.Name %>' /><br /> // <-- Problem line
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:Repeater>
CS0103: The name 'Item' does not exist in the current context
I fumbled my way onto a clunky solution, which is this
((Person)GetDataItem()).Name
But I desperately hope this is not the best way of accomplishing the task.

Radio button selection in asp.net listview

I want use to prevent user to multiple selection in radio button. I'm using the ListView control to bind the radio button in asp.net.
ListView Design
<asp:ListView ID="lvColorName" runat="server" GroupItemCount="2"
GroupPlaceholderID="groupPlaceHolder">
<LayoutTemplate>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder"></asp:PlaceHolder>
</LayoutTemplate>
<GroupTemplate>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder>
</GroupTemplate>
<GroupSeparatorTemplate>
</GroupSeparatorTemplate>
<ItemTemplate>
<asp:RadioButton ID="cbColorName" Text='<%# Eval("colorName")%>' runat="server" />
</ItemTemplate>
</asp:ListView>
Out Put
I don't want to select multiple. while using normal its happen but not in listview...

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.

ListBox selection change does postback even though AutoPostBack is set to false for the ListBox

The problem is described in the title. It does postbacking also when I specifically set AutoPostBack property of the ListBox to false, though it should be like that by default.
ListBox selection mode is set to multiple.
There is a similar question but with a different background.
What am I missing? Is this some well known issue?
Thank you for replying,
please tell if you need the exact code
EDIT:
Here's the aspx page. Codebehind is much larger, since this page is the target of redirection, please tell if you need additional info from there.
<%# Page Language="C#" AutoEventWireup="true" CodeFile="ReportsPreviewViewAddParams.aspx.cs" Inherits="Admin_ReportsPreviewViewAddParams" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
<body>
<form runat="server">
<asp:HiddenField ID="SqlSelect" runat="server" />
<asp:HiddenField ID="SqlColumnSize" runat="server" />
<asp:HiddenField ID="SqlGroup" runat="server" />
<asp:HiddenField ID="SqlOrder" runat="server" />
<asp:HiddenField ID="PageSize" runat="server" />
<asp:HiddenField ID="SumColumns" runat="server" />
<asp:Repeater ID="RepeaterInput" runat="server" OnItemDataBound="OnRepeaterItemDataBound" >
<HeaderTemplate>
<table>
<tr><th>
<asp:Label ID="LabelHeader" runat="server" Text='<%# Webcom.Configurator.Core.MLPersistentManager.GetKeyValue("Admin.Reports.EnterParams") %>' />
</th></tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:TextBox ID="TextBoxValueOne" runat="server" AutoPostBack="false" />
<asp:ListBox ID="ListBoxControlOne" runat="server" AutoPostBack="false" />
</td>
<td><asp:Label ID="LabelOperatorOne" runat="server" /></td>
<td><asp:Label ID="LabelColumnName" runat="server" /></td>
<td><asp:Label ID="LabelOperatorTwo" runat="server" /></td>
<td>
<asp:TextBox ID="TextBoxValueTwo" runat="server" AutoPostBack="false" />
<asp:ListBox ID="ListBoxControlTwo" runat="server" AutoPostBack="false" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="buttonSubmit" runat="server" Visible="true" OnClick="OnSubmit" />
</form>
</body>
</html>
The repeater does not have a way of tracking autopostbacks that happen from controls within the repeater. This may offer you a clue:
Handling autopostbacks within a repeater
You could try initializing the AutoPostBack property early in the code-behind page life-cycle, perhaps in Page Load method (after page controls have been created):
ListBox ListBoxControlOne = (ListBox)RepeaterInput.FindControl("ListBoxControlOne");
ListBoxControlOne.AutoPostBack = false;

How to set a download counter on Hyperlink Control inside Repeater? asp.net

I have a hyperlink inside a repeater control for the list of pdf. I want to set a download counter on each click on each hyperlink. The FileName must be a parameter. My code is basically like below. There is also code that calls stored procedure and bind it to the repeater on page_load.
<asp:Repeater ID="rptPDF" runat="server">
<ItemTemplate>
<div class="repeaterResources">
<b><%# Eval("Name") %></b><br />
<b>Description</b> <%# Eval("Description") %><br />
<asp:HyperLink ID="HyperLink2" runat="server" class="downloadLink" NavigateUrl='<%# "~/PDF/" & Eval("Filename") %>' Target="_blank">Download</asp:HyperLink><br /><br />
</div>
</ItemTemplate>
</asp:Repeater>
The mystery bit is how to get a button click event from here. Thanks.
You can use the OnCommand event and set the CommandArgument attribute with a value using
<%# Eval('myvalue') %>.
MSDN has an example minus the repeater: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.oncommand.aspx
Sample:
<asp:Repeater ID="repeater" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="button1" OnCommand="button1_command" CommandArgument='<%# Eval("myvalue") %>' />
</ItemTemplate>
</asp:Repeater>

Resources