BleepingComputer.com: help with editable gridview with paging

Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

help with editable gridview with paging

#1 User is offline   nixx 

  • Forum Regular
  • PipPipPip
  • Find Topics
  • Group: Members
  • Posts: 157
  • Joined: 17-January 06

  Posted 17 June 2009 - 08:11 PM

Hi,

I need help with storing the changes made in a gridview even after going to another gridview page. I have a gridview with a column for checkboxes. There's also a "check all" checkbox which should, well obviously, check all the items in the gridview.

<asp:GridView ID="gvSearch" runat="server" AutoGenerateColumns="false" CssClass="gv" DataKeyNames="ID" PageSize="5" AllowPaging ="true" OnPageIndexChanging="gvSearch_PageIndexChanging">
	<Columns>
		<asp:TemplateField>
			<HeaderTemplate>
				<asp:CheckBox ID="chkSelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelectAll_CheckedChanged" />
			</HeaderTemplate>
			<ItemTemplate>
				<asp:CheckBox ID="chkSelect" runat="server" />
			</ItemTemplate>
			<ItemStyle CssClass="gvCell" HorizontalAlign="Center" VerticalAlign="Middle" />
			<HeaderStyle CssClass="gvHeader" />
		</asp:TemplateField>
		<asp:BoundField DataField="Name" HeaderText="Skill">
			<ItemStyle CssClass="gvCell" HorizontalAlign="Left" VerticalAlign="Middle" />
			<HeaderStyle CssClass="gvHeader" />
		</asp:BoundField>
		<asp:BoundField DataField="Description" HeaderText="Description">
			<ItemStyle CssClass="gvCell" HorizontalAlign="Left" VerticalAlign="Middle" />
			<HeaderStyle CssClass="gvHeader" />
		</asp:BoundField>
	</Columns>
	<EmptyDataTemplate>
		<asp:Label ID="lblNoResults" runat="server" Text="No results matching your search were found." />
	</EmptyDataTemplate>
	<RowStyle CssClass="gvRow" />
	<AlternatingRowStyle CssClass="gvAlternatingRow" />
	<EmptyDataRowStyle CssClass="gvEmpty" />
	<PagerSettings Position="TopAndBottom" Mode="NumericFirstLast" PageButtonCount="3" FirstPageText="First" LastPageText="Last" PreviousPageText="Previous" NextPageText="Next"/>
		<PagerTemplate>
			<p>
			   <asp:ImageButton ID="cmdFirst" runat="server" ImageUrl="~/IMAGES/first.gif" CommandArgument="first" CommandName="Page" OnCommand="Paginate" ToolTip="First" />
			   <asp:ImageButton ID="cmdPrevious" runat="server" ImageUrl="~/IMAGES/prev.gif" CommandArgument="prev" CommandName="Page" OnCommand="Paginate" ToolTip="Previous" />
			   <asp:Label ID="lblPage" runat="server">Page <%= gvEmployees.PageIndex + 1 %> of <%= gvEmployees.PageCount %></asp:Label>
			   <asp:ImageButton ID="cmdNext" runat="server" ImageUrl="~/IMAGES/next.gif" CommandArgument="next" CommandName="Page" OnCommand="Paginate" ToolTip="Next" />
			   <asp:ImageButton ID="cmdLast" runat="server" ImageUrl="~/IMAGES/last.gif" CommandArgument="last" CommandName="Page" OnCommand="Paginate" ToolTip="Last" />
		  </p>
</PagerTemplate>
</asp:GridView>



The code for selecting all
protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
	{
		CheckBox cb;
		foreach (GridViewRow gvr in gvSearch.Rows)
		{
			cb = (CheckBox)gvr.FindControl("chkSelect");
			cb.Checked = ((CheckBox)sender).Checked;
		}
	}


The code for populating the gridview
private void showSearchResults()
{
	//create list of skills
	List<Skill> filteredSkillList = SkillBL.GetFilteredList(qSkillName, qTypeID, qCorporateLevelID,
									qCategoryID, qRatingScale, qProviderID, qSkillSetID, qKeyword, qArchived).ToList<Skill>();
									
	
	//bind list to gvSearch
	gvSearch.DataSource = SkillBL.GetFilteredRelatedSkill(filteredSkillList, qRelatedSkill);
	gvSearch.DataBind();
}


the code for paging
protected void Paginate(object sender, CommandEventArgs e)
{
	// get the current page selected
	int intCurIndex = gvSearch.PageIndex;

	switch (e.CommandArgument.ToString().ToLower())
	{
		case "first":
			gvSearch.PageIndex = 0;
			break;
		case "prev":
			if (intCurIndex == 0)
			{
				gvSearch.PageIndex = intCurIndex;
			}
			else
			{
				gvSearch.PageIndex = intCurIndex - 1;
			}
			break;
		case "next":
			gvSearch.PageIndex = intCurIndex + 1;
			break;
		case "last":
			gvSearch.PageIndex = gvSearch.PageCount;
			break;
	}
}


protected void gvSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
	e.NewPageIndex = gvSearch.PageIndex;
	showSearchResults();
}


How do I store the rows that I selected so that when I do something to it, not only the selected rows on the current page are selected but also the selected rows from the other pages?

#2 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,522
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 17 June 2009 - 09:14 PM

Are you asking how to store results from page to page?
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#3 User is offline   nixx 

  • Forum Regular
  • PipPipPip
  • Find Topics
  • Group: Members
  • Posts: 157
  • Joined: 17-January 06

Posted 17 June 2009 - 10:37 PM

hey groovicus, I've already figured that out. What I'm trying to achieve is select/deselect all the rows when I click on the checkbox header.

The current gridview displays only 5 rows per page. Checking/unchecking the select all checkbox only checks/unchecks the 5 rows. How do I check all the other rows that aren't shown in the page?

This post has been edited by nixx: 17 June 2009 - 10:37 PM


#4 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,522
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 17 June 2009 - 11:59 PM

Quote

hey groovicus, I've already figured that out

I'm curious as to how you did that. If I have a collection representing a collection of checked boxes on one page, stored using javascript, that collection is gone when I load the next page, unless I pass the entire collection to the next page in the URL.

I am not explaining this well. HTML is stateless. That means that it can not remember from one page to the next what data has been entered in forms, what links have been clicked, etc. (browsers so this, to an extent). So if I have a page that I click on checkboxes 1,3, and 5, the next page has no way of knowing that. So if you decide to unselect the visible rows, there is no way that you can unselect rows from a different page, because the current page knows nothing about the previous page.

Quote

How do I store the rows that I selected so that when I do something to it, not only the selected rows on the current page are selected but also the selected rows from the other pages?

You can't, just using javascript and HTML. You need something on the server end to handle this. The browser can not remember from one page to the next what is happening.


Or else I am not understanding what you want to do.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#5 User is offline   nixx 

  • Forum Regular
  • PipPipPip
  • Find Topics
  • Group: Members
  • Posts: 157
  • Joined: 17-January 06

Posted 18 June 2009 - 12:14 AM

Oh I'm using a Session to store the checked items in a list.

#6 User is offline   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,522
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 18 June 2009 - 12:30 PM

One way to do it would be to use an AJAX call to cal your servlet when the "check all" option is checked, then just iterate through the List, setting them to checked (or unchecked, as the case may be).
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users