2nd DataPager not visible after 2nd DataBind() - asp.net

I have a ListView object in a ASP.Net page with two dataPagers for the same ListView object. One at the top of the page, the other at the bottom. Both have the same properties and both display on the initial loading of the page just fine.
When the user is allowed to search the list for items, the dataPagers usually are intentionally hidden as they don't apply to the short resulting list. However, when I "reset" the filter criteria to show all of the records again, only the dataPager at the bottom of the page is shown. The dataPager at the top of the page remains hidden even though it's properties are set back to visible. It sounds so much like a logic error, but after stepping through the code several times and trying to find an error to no avail, I'm wondering what I might be misunderstanding or not seeing.
Any help would be appreciated.
<asp:DataPager ID="DataPagerHeader" runat="server" PagedControlID="courseListView" PageSize="20" >
<Fields>
<asp:NumericPagerField ButtonType="Button" />
</Fields>
</asp:DataPager>
<asp:ListView ID="courseListView" runat="server" datasourceid="CoursesDataSource" OnSorting="courseListView_Sorting" OnDataBound="courseListView_DataBound" OnItemDataBound="courseListView_ItemDataBound">
<LayoutTemplate>
...
</LayoutTemplate>
<ItemTemplate>
...
</ItemTemplate>
</asp:ListView>
<asp:DataPager ID="DataPagerFooter" runat="server" PagedControlID="courseListView" PageSize="20">
<Fields>
<asp:NumericPagerField ButtonType="Button" />
</Fields>
</asp:DataPager>
Code for Hiding/Showing datapager objects:
protected void courseListView_DataBound(object sender, EventArgs e)
{
DataPagerFooter.Visible = (DataPagerFooter.PageSize < DataPagerFooter.TotalRowCount);
DataPagerHeader.Visible = (DataPagerHeader.PageSize < DataPagerHeader.TotalRowCount);
}
When looking at the source code using Firebug, you see the following where the dataPager should be. Note the lack of tags between the tags.
<span id="ctl00_mainPlaceHolder_DataPagerHeader"></span>

Related

ASP.NET ListView Paging : Invalid viewstate

I have a ListView control as follows (for posting here I've removed the values from my ItemTemplate):
<asp:ListView ID="ListView1" runat="server" DataSourceID="MyDataSource">
<LayoutTemplate>
<div id="requests" runat="server">
<asp:Panel runat="server" id="itemPlaceholder"></asp:Panel>
</div>
<asp:DataPager runat="server" ID="DataPager" PageSize="3">
<Fields>
<asp:NumericPagerField ButtonCount="10" PreviousPageText="<--" NextPageText="-->" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<div ID="itemPlaceholder" class="request" runat="server">
<asp:LinkButton ID="button" runat="server" Text='...' CommandName="..."
CommandArgument='...' OnClick="..."
style="...">
</asp:LinkButton> -
</div>
</ItemTemplate>
</asp:ListView>
This ListView exists as a user control (.ascx), which I've embedded into an ASPX web page.
As I expect, when the web page loads, for a list of 9 items, I get 3 pages of 3 items.
When I click to go to the next page, the page loads the next set of items correctly... but only for a brief second. Then a strange thing happens. The page embeds a copy of itself inside the page 6 times, each one of them underneath one of the fields on the form inside the page.
If I then attempt to go to the next or previous page, an ASP.NET server error appears:
The state information is invalid for this page and might be corrupted.
In the stack trace, it shows the following errors:
FormatException: The input is not a valid Base-64 string as it contains a non-base character, more than two padding characters, or a non-white space character among the padding characters.
ViewStateException: Invalid viewstate.
I was able to replicate this on both my browsers, IE8 and Chrome.
you use listview event OnPagePropertiesChanging
like this
<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
in aspx.cs
protected void OnPagePropertiesChanging(object sender,PagePropertiesChangingEventArgs e)
{
(lvCustomers.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
this.BindListView();
}
BindListView() is method for binding list view
visit this link for example
i hope it's helpfull

Asp.net gridview edit without editbutton

I have regular asp.net gridview,and i want to enable editmode in each row,and also without editbutton (like excel grid).
Edited data i want to save to my database by clicking "Post" button outside the grid(one button for whole grid).
How can i reach it?
To achieve this, you are going to have to use ItemTemplates for each column with textboxes in them as the control..
ASP
<asp:TemplateField HeaderText="Heading Title" SortExpression="Heading Title">
<ItemTemplate>
<asp:TextBox ID="tbTextbox" runat="server" Width="65px" Text='<%# Bind("ColumnNameYouWantToView") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
After this is set up properly, you will want the post button. You can either put it in the grid or outside the grid. I use both, but here is the one inside the grid as a footer.
ASP
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnView" runat="server" Text="View" OnClick="btnView_Click" Width="40px" />
</ItemTemplate>
<FooterTemplate>
<asp:Button ValidationGroup="UPDATE" ID="btnUpdate" OnClick="btnUpdate_Click" runat="server" Text="Update" Width="50px"></asp:Button>
</FooterTemplate>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
</asp:TemplateField>
So far, what I have found that works best is using a foreach statement in your button click. The biggest flaw with this idea is that it will update every single row. It works, but if you only change a single row at a time, it will update all of them. I have my pager set to 10, so 10 rows are always updated (unless you are just searching for a single record and update it, the only that single record is updated).
Code Behind C#
protected void btnUpdate_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["databaseConnection"].ConnectionString);
conn.Open();
//finds the controls within the gridview and updates them
foreach (GridViewRow gvr in gvGridViewName.Rows)
{
string ID = (gvr.FindControl("lblId") as Label).Text.Trim();//finds the control in the gridview
string anotherControl = ((TextBox)gvr.FindControl("tbTextBox")).Text.Trim();//finds the textbox in the gridview
//Your update or insert statements
}
This is how I do it. You can also look into Real World Grids but I haven't had much luck with this as I would always get an error if a textbox was empty. This however, is suppose to be "smart" enough to just update the rows that have been changed, but again, I didn't have much luck of doing it this way. Hope this helps!

Dynamic Data - Selecting a table from a DropDownList to scaffold in a GridView

I have been struggling with this ASP.NET Dynamic Data problem for days now... I have a DropDownList containing the table names of all the tables in my data context (.dbml) file. When I select the DropDownList, it needs to scaffold the selected table in a GridView. My code works 100% in scaffolding the MetaTable in the GridView (it implements all the rules that I applied in my Meta Classes).
However, filtering only seems to work if I explicitly add the DynamicExpression in the declaration of the QueryExtender:
<asp:QueryExtender ID="GridQueryExtender" TargetControlID="GridDataSource" runat="server">
<asp:DynamicFilterExpression ControlID="FilterRepeater" />
</asp:QueryExtender>
This in turn requires me to specify the MetaTable explicitly in the LinqDataSource (linqdsData), either programmatically in the Page_Load or in the ASP.NET syntax.
Since the GridView gets scaffolded in the Page_Load part of the life-cycle, the above approach does not work for me, since it takes place in the Page_Init part of the life-cycle.
So my requirement is that as soon as I select another table to populate the GridView with from the DropDownList, the FilterRepeater needs to reflect the filters of the newly selected MetaTable.
Is there any way for me to programmatically update the FilterRepeater in the Page_Load so that it will contain the filters of the MetaTable that I selected in the DropDownList.
The following is some of my code:
ASP.NET Page Code-Behind:
protected void Page_Load(object sender, EventArgs e)
{
if (ddlTable.SelectedIndex > 0)
{
string tableName = ddlDataType.SelectedValue;
linqdsData.TableName = tableName;
MetaTable mt = ASP.global_asax.DefaultModel.GetTable(tableName);
GridViewData.SetMetaTable(mt, mt.GetColumnValuesFromRoute(Context));
GridViewData.EnableDynamicData(mt.EntityType);
GridViewData.DataSourceID = linqdsData.ID;
}
}
ASP.NET Page:
<asp:Panel runat="server" ID="pnlFilters" CssClass="gridFilterCon" EnableTheming="True">
<div class="filterGridHeading">
Filter the grid by:</div>
<asp:QueryableFilterRepeater runat="server" ID="FilterRepeater">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("DisplayName") %>' OnPreRender="Label_PreRender"
CssClass="gridFilterLbl" />
<asp:DynamicFilter runat="server" ID="DynamicFilter" />
<br />
</ItemTemplate>
</asp:QueryableFilterRepeater>
<asp:Button ID="btnFilter" runat="server" Text="OK"
EnableTheming="False" UseSubmitBehavior="False" OnClick="btnFilter_Click" />
</asp:Panel>
<asp:GridView ID="GridViewData" runat="server" OnSelectedIndexChanged="GridViewData_SelectedIndexChanged"
OnPreRender="GridViewData_PreRender" OnRowDataBound="GridViewData_RowDataBound"
OnPageIndexChanged="GridViewData_PageIndexChanged" AllowPaging="True" PageSize="50" OnInit="GridViewData_Init">
<Columns>
...
</Columns>
<PagerTemplate>
<asp:GridViewPager ID="GridViewPager1" runat="server" />
</PagerTemplate>
<PagerSettings Mode="NumericFirstLast" NextPageText="Next" />
</asp:GridView>
<asp:LinqDataSource ID="linqdsData" runat="server" ContextTypeName="pdcDataContext"
OnSelected="linqdsData_Selected" OnSelecting="linqdsData_Selecting" EnableUpdate="True">
</asp:LinqDataSource>
<asp:QueryExtender ID="GridQueryExtender" TargetControlID="linqdsData" runat="server">
</asp:QueryExtender>
Your help will be greatly appreciated.
It sounds like you are trying to do a lot on one web page. This creates complications of the type you are experiencing: each table requires distinct filters and MetaTables. Trying to keep each item straight requires a bunch of switch and/or if...then statements. I recommend an alternate approach. Instead of doing all of this on one page:
Create a web page for each table
Setup the appropriate filters and MetaTables
Copy the DropDownList containing the table names of all the tables to each web page, and use it to redirect to the appropriate web page.
ASP.net Dynamic Data makes it easy to create web pages for each table. That is what scaffolding is all about. With the approach above, each web page will handle its own set of concerns that are focused on the particular table.
Hope this helps.

Gridview custom paging inside wizard control doesn't work correct in asp.net

I have a page where guests can be selected.
This page contains an update panel, within the update panel there is a wizard control and in the wizard control there is a gridview (Panel-> Update Panel -> wizard control -> gridview)
Now the paging doesn't work well, I searched the internet for answers but I didn't find the right answer. I use custom paging.
When I want to change the pagenumber (to the second or last page) it works fine. But when I want to select a row or go to another page, the gridview doesn't work correct. When I select A row on the second or last page (other page's don't work) the selected guest (item) is always one of the first page. When I want change to another page (from the second or last page) it doesn't work and stays on the selected page.
Is there a way to solve this problem?
part of the code:
<asp:Panel ID="PnlRe" Style="display: none; ... >
<asp:UpdatePanel ID="UpdtPnlDetail" runat="server" UpdateMode="Conditional">
<ContentTemplate>
...
<asp:Wizard ID="wzd" runat="server" ... >
...
<asp:WizardStep ID="WizardStep3" runat="server" Title="Naam">
...
<asp:Gridview ID="Gridview1" runat="server" AutoGenerateColumns="False"
DataKeyNames="GastID,Versie" DataSourceID="odsGasten" EmptyDataText="...."
OnRowCommand="Gridview1_RowCommand" OnPageIndexChanging="Gridview1_PageIndexChanging"
OnPageSizeChanging="Gridview1_PageSizeChanging" OnSearching="Gridview1_Searching"
OnSorting="Gridview1_Sorting" OnRowDataBound="Gridview1_RowDataBound"
CausesValidation="False" meta:resourcekey="..." PagerType="Custom"
ShowFilter="True" ShowInsertRecord="True" ShowPageSizer="True" UseSubmitBehaviour="False">
<Columns>
<asp:TemplateField ShowHeader="False" meta:resourcekey="...">
<ItemTemplate>
<asp:ImageButton runat="server" CommandName="Select" CausesValidation="False" SkinID="uprowbutton"
ToolTip="..." ID="ImgBtnSelect" meta:resourcekey="...">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
... Boundfields ...
</Columns>
</asp:Gridview>
...
</asp:WizardStep>
...
</asp:Wizard>
</ContentTemplate>
</asp:UpdatePanel>
protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.odsGasten.FilterExpression = searchText;
this.Gridview1.DataBind();
this.UpdtPnlDetail.Update();
}
there is a tricky way to achieve this, you have to manipulate your data source.
for example if you are using a datatable you have to select first ten elements of dt and bind it to grid, and when next button is clicked get next ten elements of dt and bind it to gridview. this will make your task easy and will get more performance for your page.
I can see that this.GridView1.DataBind() is called in GridView1_PageIndexChanging, but where is the currentPageIndex stored and updated with the page index you want to navigate to and when in the process is it happening?
Sounds to me like you have a viewstate problem.
You need to make sure that between Init and Load you have the grid's source set, either from viewstate or from a new bind-ing.

ASP.NET GridView CommandField Update/Cancel does not wrap

My question is on the ASP.NET GridView control. I am using a CommandField in the Columns tag as seen below.
<asp:CommandField ShowEditButton="True" HeaderStyle-Width="40px" UpdateText="Save" ButtonType="Link" HeaderStyle-Wrap="true" ItemStyle-Wrap="true" ItemStyle-Width="40px"/>
What renders is the shown in the following image (after I click on the Edit button).
As you can see I am trying to have the Cancel link show up a new line and my question is how do you do what? If I change the ButtonType="Link" to ButtonType="Button", I get it rendering correctly as shown below.
alt text http://i38.tinypic.com/2pqopxi.jpg
I've tried Google already and maybe I'm not searching on the right tags but I couldn't see this one addressed before.
If you use a template field it will give you complete control over the look of your page, but it requires that you use the CommandName and possible CommandArgument properties, and also using the GridView's OnRowCommand.
The aspx page:
<asp:GridView id="gvGrid" runat="server" OnRowCommand="gvGrid_Command">
<Columns>
<asp:TemplateField>
<ItemTemplate>
Some Stuff random content
<br />
<asp:LinkButton id="lbDoIt" runat="server" CommandName="Cancel" CommandArgument="SomeIdentifierIfNecessary" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The code behind:
protected void gvGrid_Command(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Cancel")
{
// Do your cancel stuff here.
}
}
Don't use a command field, use a TemplateField and put your command button in that with a line break (br) before it like you want.

Resources