Asked by:
Request for Tutorial - or info on Listview with Datapagers and Changes in SqlDatasource Parameters

Question
-
User-1193791088 posted
Setup:
Listview with a Top and Bottom Datapager
Separate ListViews that are used as filters for AlphaSeparate Buttons to select Display by Types- These work Fine
Code:I use viewstate to add parameters
if (!IsPostBack)
{
//ViewState["SqlData_CustomerAccounts"] = null;
ViewState["Filter_AccountTypeId"] = "";
ViewState["Filter_AccountStatusId"] = "";
ViewState["Paging_AccountStatusId"] = "";
ViewState["Paging_AccountTypeId"] = "";
ViewState["AccountName"] = "";
DataBind_ListView_Accounts();
}
if (IsPostBack)
{
DataBind_ListView_Accounts();
}
private void DataBind_ListView_Accounts() { SDS_Paging.SelectCommand = "SELECT DISTINCT SUBSTRING(AccountName, 1, 1) AS AccountName FROM CuAccount WHERE AccountId <> -1" + ViewState["Paging_AccountTypeId"].ToString() + ViewState["Paging_AccountStatusId"].ToString(); SDS_Paging.DataBind(); SqlData_CustomerAccounts.SelectCommand = "SELECT DPL.PriceLevel, CuA.PriceLevelId, DSA.AccountStatus, CuA.AccountStatusId, CuA.AccountName, CuA.DateCreated, DCT.CustomerType, DAT.AccountType, CuA.CreditRating, CuA.AccountId FROM CuAccount AS CuA INNER JOIN Data_CuAccount_PriceLevels AS DPL ON CuA.PriceLevelId = DPL.PriceLevelId INNER JOIN Data_Status_CuAccount AS DSA ON CuA.AccountStatusId = DSA.AccountStatusId INNER JOIN Data_CuAccount_AccountTypes AS DAT ON CuA.AccountTypeId = DAT.AccountTypeId INNER JOIN Data_CuAccount_CustomerTypes AS DCT ON CuA.CustomerTypeId = DCT.CustomerTypeId WHERE CuA.AccountId <> -1" + ViewState["Filter_AccountTypeId"].ToString() + ViewState["Filter_AccountStatusId"].ToString() + " ORDER BY CuA.AccountName "; SqlData_CustomerAccounts.DataBind(); ListView_CustomerAccounts.DataSourceID = "SqlData_CustomerAccounts"; ListView_CustomerAccounts.DataBind(); ListView_PagerTop.DataBind(); ListView_PagerBottom.DataBind(); }Error Message (Browser Console) when trying to Page Data
Error: Sys.WebForms.PageRequestManagerServerErrorException: Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.But I have always had a problem with updating the DataSource Select in Code and when you page it resets
I tried addingprotected void ListView_CustomerAccounts_PagePropertiesChanging(Object sender, PagePropertiesChangingEventArgs e) { (ListView_CustomerAccounts.FindControl("DataPager_Accounts_Top") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false); DataBind_ListView_Accounts(); }
Doesn't work
I use ListViews over Gridview due to ease of Display manipulation for features i want, but if it doesn't function properly???
<asp:ListView ID="ListView_CustomerAccounts" runat="server" DataSourceID="SqlData_CustomerAccounts" DataKeyNames="AccountId" EnablePersistedSelection="true" OnItemDataBound="ListView_CustomerAccounts_ItemDataBound" OnPagePropertiesChanging="ListView_CustomerAccounts_PagePropertiesChanging"> <EmptyDataTemplate> <table runat="server" style=""> <tr> <td> <h3 class="text-warning m-1">THERE ARE NO ACCOUNTS OF THIS TYPE!</h3> </td> </tr> </table> </EmptyDataTemplate> <EmptyItemTemplate> <td runat="server" /> </EmptyItemTemplate> <ItemTemplate> <tr> <td> <asp:Button runat="server" PostBackUrl='<%# Eval("AccountId","~/Admin/Customer/Account/Manage.aspx?AccountId={0}") %>' ID="Btn_Accounts_Manage" CssClass="btn btn-primary-outline fas" Text=" MANAGE" ToolTip="Manage" /> </td> <td> <asp:Label Text='<%# Eval("AccountId") %>' runat="server" ID="Lab_AccountId" /> </td> <td> <asp:Label Text='<%# Eval("AccountName") %>' runat="server" ID="Lab_AccountName" /> </td> <td> <asp:Label Text='<%# Eval("CustomerType") %>' runat="server" ID="Lab_ZipCode" /> </td> <td> <asp:Label Text='<%# Eval("AccountType") %>' runat="server" ID="Lab_AccountType" /> </td> <td> <asp:Label Text='<%# Eval("AccountStatus") %>' runat="server" ID="AccountStatusLabel" /> <asp:HiddenField Value='<%# Eval("AccountStatusId") %>' runat="server" ID="HF_AccountStatusId" /> </td> <td> <asp:HiddenField Value='<%# Eval("PriceLevelId") %>' runat="server" ID="HF_PriceLevelId" /> <asp:Label Text='<%# Eval("PriceLevel") %>' runat="server" ID="Lab_PriceLevel" /> </td> <td> <asp:HiddenField Value='<%# Eval("CreditRating") %>' runat="server" ID="HF_CreditRating" /> <asp:Label Text='<%# Eval("CreditRating") %>' runat="server" ID="Lab_CreditRating" /> </td> <td> <asp:Label Text='<%# Eval("DateCreated") %>' runat="server" ID="Lab_DateCreated" /> </td> </tr> </ItemTemplate> <LayoutTemplate> <asp:DataPager runat="server" ID="DataPager_Accounts_Top" PageSize="50" PagedControlID="ListView_CustomerAccounts"> <Fields> <asp:NextPreviousPagerField ButtonCssClass="btn btn-primary fas" ButtonType="Button" FirstPageText="" PreviousPageText="" ShowFirstPageButton="true" ShowLastPageButton="false" ShowNextPageButton="false" ShowPreviousPageButton="true" /> <asp:NumericPagerField ButtonCount="10" ButtonType="Button" CurrentPageLabelCssClass="btn btn-outline-primary" NumericButtonCssClass="btn btn-primary" /> <asp:NextPreviousPagerField ButtonCssClass="btn btn-primary fas" ButtonType="Button" LastPageText="" NextPageText="" ShowFirstPageButton="false" ShowLastPageButton="true" ShowNextPageButton="true" ShowPreviousPageButton="false" /> </Fields> </asp:DataPager> <table runat="server" id="itemPlaceholderContainer" class="table table-light mt-2" border="0"> <tr runat="server" style=""> <th runat="server" scope="col"><span>ACCOUNT:</span></th> <th runat="server" scope="col"><span> #</span></th> <th runat="server" scope="col"><span>NAME</span></th> <th runat="server" scope="col"><span>TYPE</span></th> <th runat="server" scope="col"><span>CATEGORY</span></th> <th runat="server" scope="col"><span class="text-center">STATUS</span></th> <th runat="server" scope="col"><span class="text-center">REWARDS</span></th> <th runat="server" scope="col"><span class="text-center">CREDIT</span></th> <th runat="server" scope="col"><span>CREATED</span></th> </tr> <tr runat="server" id="itemPlaceholder"></tr> </table> <asp:DataPager runat="server" PageSize="50" ID="DataPager_Accounts_Bottom"> <Fields> <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="False" ShowPreviousPageButton="true" ShowNextPageButton="False" ButtonCssClass="btn btn-primary fas" FirstPageText="" PreviousPageText="" /> <asp:NumericPagerField ButtonCount="10" ButtonType="Button" NumericButtonCssClass="btn btn-primary" CurrentPageLabelCssClass="btn btn-outline-primary" /> <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="False" ShowLastPageButton="True" ShowNextPageButton="True" ShowPreviousPageButton="False" ButtonCssClass="btn btn-primary fas" LastPageText="" NextPageText="" /> </Fields> </asp:DataPager> </LayoutTemplate> </asp:ListView>
Wednesday, February 12, 2020 5:53 AM
All replies
-
User-775646050 posted
If you want to learn how to use ListView, you should start out with a simple example. I created this simple example for you. Notice I am simply binding my ListView control in the code behind instead of using a SqlDataSource control and I suggest you do the same. The datasource controls are terribly inflexible and not recommended. Let me know if this doesn't make sense.
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="lvCustomers_OnPagePropertiesChanging"> <LayoutTemplate> <table cellpadding="0" cellspacing="0"> <tr> <th> FirstName </th> <th> LastName </th> <th> AccountNumber </th> </tr> <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder> <tr> <td colspan = "3"> <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="3"> <Fields> <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true" ShowNextPageButton="false" /> <asp:NumericPagerField ButtonType="Link" /> <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton = "false" /> </Fields> </asp:DataPager> </td> </tr> </table> </LayoutTemplate> <GroupTemplate> <tr> <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder> </tr> </GroupTemplate> <ItemTemplate> <td> <%# Eval("FirstName") %> </td> <td> <%# Eval("LastName") %> </td> <td> <%# Eval("AccountNumber") %> </td> </ItemTemplate> </asp:ListView> </asp:Content>
using System; using System.Collections.Generic; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class _Default : Page { private readonly List<Person> myList = new List<Person>() { new Person {FirstName = "Larry", LastName = "Cool", AccountNumber = "12345"}, new Person {FirstName = "Curly", LastName = "Cool", AccountNumber = "54321"}, new Person {FirstName = "Moe", LastName = "Cool", AccountNumber = "1213145"}, new Person {FirstName = "Ronald", LastName = "Ricardo", AccountNumber = "183345"}, new Person {FirstName = "Bobby", LastName = "Jones", AccountNumber = "1202345"}, new Person {FirstName = "Rex", LastName = "Hamilton", AccountNumber = "123349845"}, }; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindListView(); } } private void BindListView() { lvCustomers.DataSource = myList; lvCustomers.DataBind(); } protected void lvCustomers_OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e) { (lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false); BindListView(); } } }
Wednesday, February 12, 2020 8:46 PM -
User283571144 posted
Hi, RobertH3,With the description you gave, I could not reproduce the error message.
Could you please tell me when you triggered the error message (like, press a button or select a dropdown list)?
If you could post more details about the code or information, it would be helpful to reproduce your issues and find out the solution. As you can see, there are some codes we do not have an idea on, e.g. "SDS_Paging", "ListView_PagerTop.DataBind()".
Besides, could you please make sure what you need is to update the select command of SqlDataSource with the ViewState values and rebind the data to the list view?
"PagePropertiesChanging" works here but it is unnecessary since you already bind the SqlDataSource control to the list view and set the "PagedControlID" for DataPager. Therefore, the paging function will be automatically completed by control itself. "PagePropertiesChanging" just takes effect twice.
Best regards,Sean
Thursday, February 13, 2020 9:32 AM