Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to disable paging in gridview using a check box

Status
Not open for further replies.

josie2007

Technical User
Apr 14, 2007
90
US
Is it possible to add a checkbox where users have a choice to disable or enable the gridview's paging function.
 
here is what I added so far when I click the check box it disable the paging but when I click it back my gridview just disapear...
Protected Sub ckPaging_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ckPaging.CheckedChanged
gvItem.AllowPaging = False
BindData()
End Sub
 
Code:
Protected Sub ckPaging_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ckPaging.CheckedChanged
   gvItem.AllowPaging = ((CheckBox)sender).Checked
   BindData()
End Sub

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you Jason for the help. I think i need to cast it like this in order to work. so far it works when checkbox is checked and I would like to have the paging back when the checkbox is unchecded.


Protected Sub ckPaging_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ckPaging.CheckedChanged
gvItem.AllowPaging = (CType(sender, CheckBox)).Checked
BindData()
End Sub
 
I assumed this would account of postbacks.

just to confirm:
is paging enabled when the page is first visited?
is the paging checkbox checked when the page is first visited?
does autopostback="true" for the checkbox control?
does the ckPaging_CheckedChanged event fire when the checkbox state is changed?
Does the gvItem.AllowPaging reset to "true" after you call databind?
Does gvItem have AllowPaging="true/false" set in the aspx file? (if so remove it)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you Jason for the reply and I am still do not have a luck to get this to work...

<asp:CheckBox ID="ckPaging" runat="server" Text="Enable / Disable Paging" AutoPostBack="True" Checked="true"/ >

yes, the autopostback= true
yes, the event fire whtn the checkbox state is changed
yes, paging is enabled when the page is first visited
I did remvoe the allowpaging = true from aspx page



Protected Sub ckPaging_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ckPaging.CheckedChanged
If gvItem.AllowPaging = (CType(sender, CheckBox)).Checked Then
BindData()
Else
gvItem.AllowPaging = False
BindData()
End If

End Sub
 
long shot. do you define the Paging or Paged events for the gridview?
Code:
//aspx
<asp:GridView ...
     OnPagingIndexChanging="GridView_PageIndexChanging"
     OnPagingIndexChanged="GridView_PageIndexChanged"

//code behind
protected sub GridView_PageIndexChanging(...)
end sub
protected sub GridView_PagIndexChanged(...)
end sub

if so you may need to add/remove the handlers when the checkbox changes.
I don't remember what it looks like in vb, but c# would look like this
Code:
protected void CheckBox_CheckedChanged(object sender, eventArgs e)
{
   CheckBox cb = (CheckBox)sender;
   gvItem.AllowPaging = cb.Checked;

   if(cb.Checked)
   {
      gvItem.OnPageIndexChanging += new EventHandler(GridView_PageIndexChanging);
      gvItem.OnPageIndexChanged += new EventHandler(GridView_PageIndexChanged);
   }
   else
   {
      gvItem.OnPageIndexChanging -= new EventHandler(GridView_PageIndexChanging);
      gvItem.OnPageIndexChanged -= new EventHandler(GridView_PageIndexChanged);
   }

   BindData();
}
In vb it uses the keyword AddressOf

Jason Meckley
Programmer
Specialty Bakers, Inc.
 

Jason, thank you for the help.So far I have gotten this far with your help but I have no luck to get it to work.I guess I should find some other way to make this happen where I can add a button for uers to clik On and off...I do not know if that is the best approach but this one is over my head. thank you again

the error msg. is :It is not accessible in the context because it is protected.

Sub ckPaging_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim cb As CheckBox = CType(sender, CheckBox)
gvItem.AllowPaging = cb.Checked

If (cb.Checked) = True Then
gvItem.OnPageIndexChanging += New EventHandler(AddressOf gvItem_PageIndexChanging)
Else

gvItem.OnPageIndexChanging -= New EventHandler(AddressOf gvItem_PageIndexChanging)
End If
BindData()
End Sub
 
Hi there, i'm quite new here and thought i should throw some ideas here and i apologize if i confuse you. Why can't you define two gridViews and set visible=false to the one without paging. On selecting the check box, make the gridView with paging visible=false and the gridView without paging visible = true.
 
sammyDashoe said:
Why can't you define two gridViews and set visible=false to the one without paging.
that would work, but it's alot of duplicate code and would quickly become a maintenance nightmare.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Eventhough, I am not quite sure if this is the best approach I added a button for users to click on and off the check box for paging to be on an off. Let me know if there is another better way to hanlde this. thank you all

Protected Sub btnPaging_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPaging.Click
If ckPaging.Checked Then
gvItem.AllowPaging = True
BindData()
btnPaging.Text = "Is Enabled...."
btnPaging.ForeColor = Drawing.Color.Green
btnPaging.Font.Italic = True
Else

gvItem.AllowPaging = False
BindData()
btnPaging.Text = "Is Disabled...."
btnPaging.ForeColor = Drawing.Color.Red
btnPaging.Font.Italic = True
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top