How to persist checkbox selections across recordset pages

P

Parag Gaikwad

Hi All,
I have a large recordset to be displayed on a ASP 3.0 page. I am using
recordset paging for this. For the next and previous link i am passing href
as
<a href=<Page URl>?page=<%= iPageCurrent - 1 %> for Previous.
Now when i display the contents i also add a checkbox (for deletion) to each
of the records.
Now user should be able to select one or more checkboxes across pages and
then should be allow to delete all selections together.
Can anyone please point me right directions or give some hints on how to
acheive this.
Thanks and Regards,
Parag
 
D

daddywhite

Parag,

If I were you, I would use Javascript to populate a hidden field. So
as they select/deselect values, the hidden field changes. This gets
passed from page to page and then finally when you click delete the
full list of IDs that need deleted are passed to your delete function.
for example: 23,56,78,32

So the check boxes would end up looking like:

<input name="checkbox1863" type="checkbox" id="checkbox1863"
value="1863" onClick="javascript:mytester(this.value,this.name)">

(1863 is the unique value of that record)



and the function the imaginately title "mytester" is something like:

NB: the form is called "compareform" and the hidden variable is called
"compareitems"

<script type="text/javascript">
function mytester(myvalue,myname)
{
tester='document.getElementById("'+myname+'").checked'

if (eval(tester))
{

if (document.compareform.compareitems.value=='')
{
document.compareform.compareitems.value=','+myvalue+','
}
else
{

document.compareform.compareitems.value=document.compareform.compareitems.value
+myvalue+','
}
}

else
{
document.compareform.compareitems.value =
document.compareform.compareitems.value.replace(','+myvalue+',', ',');
}
}
</script>

Hope this is usefull, please let me know how u get on.
 
P

Parag Gaikwad

Thanks for your response.
I have tried the code provided by you however, the hidden field value is not
getting passed on.
I am using recordset paging (Absolute Page). Now for the user to be able to
navigate i am providing two buttons PREV and NEXT.
The PREV button code is
<a href=<Page URl>?page=<%= iPageCurrent - 1 %>
I am not sure what I have to do to be able to pass the hidden field value
from one next click to another.
I submit the form only on click of the delete button.
Any help and pointers on this would be much appreciated.
Thanks and Regards,
Parag
 
D

daddywhite

I would use Javascript to grab the value of the hidden variable as you
click the link

so something like <a href="#" onClick="location.href='yourpage?page=<
%=iPageCurrent -
1%>&hiddenvalue='+document.form.myform.hiddenvariable.value"

I THINK this will work

Play around with this Idea.

If still struggling, send me the code and i will look over it.

regards
dave
 
D

daddywhite

Try:

<a href="#" onClick="location.href='mypage.asp?page=<%=iPageCurrent -
1%>&hiddenvalue='+document.forms.myform.hiddenvariable.value"

This should work, or something along these lines anyway.

cheers.
dave.
 
P

Parag Gaikwad

Thanks for the response dave.
Are you suggesting I should pass the selections as QueryStrings?
Is there any other way to be able to do this other than either submitting
the page on click of Next or Prev button or pssing the values in the
querystrings.
Regards,
Parag
 
D

daddywhite

Not that I know of - unless you change my function mytester to set a
cookie in javascript, that would work as well I think. Then you would
not be passing data around - simply changing a cookie value.
Ther are plent of article on Javascript and Cookies, just punch
"javascript cookies" inbto google.

Regards
Dave
 
P

Parag Gaikwad

Thanks once again Dave.
Based on the discussions till now I can see three ways to acheive this
1. Querystrings as explianed by your examples. But this means user can the
see values and Querystring has length limitation (though quite long). However
this will work in all browsers
2. Submit the page on click of Next and Prev buttons. store the selections
in hidden field. This i can pass the data without being visible. Not sure how
much performance impact this may have.
3. Cookies - Again here i think the limit is 4K so not sure how many
selections can be stored and also browser needs to support this.
Can you please let me know which would be best option considering following
criterias
1. Performance
2. Data Security
Thanks and Regards,
Parag
 
D

daddywhite

Thanks once again Dave.
Based on the discussions till now I can see three ways to acheive this
1. Querystrings as explianed by your examples. But this means user can the
see values and Querystring has length limitation (though quite long). However
this will work in all browsers
2. Submit the page on click of Next and Prev buttons. store the selections
in hidden field. This i can pass the data without being visible. Not sure how
much performance impact this may have.
3. Cookies - Again here i think the limit is 4K so not sure how many
selections can be stored and also browser needs to support this.
Can you please let me know which would be best option considering following
criterias
1. Performance
2. Data Security
Thanks and Regards,
Parag

Submission obviously means that there is no limit to what is stored

Any of these methods would work - yes there are problems with 1 and 3
but it all depends on how many records you expect a user to want to
delete in any one action - if hundreds then dont use querystrings or
cookies. If on a few (less than 40/50) then use any of these methods.

I wouldnt worry about the browser problem with cookies - you can
always check whether they have cookies enabled and if not warn them
and tell them to switch them on - but i dont see how they would get
most websites to work if they have cookies disabled.

Regards
Dave.
 
P

Parag Gaikwad

Thanks for the response Dave.
I think I would implement one of the following options. However, I am not
100% sure which one to use. Would really appreciate your advice
1. Hiden Field & Submit the Form on click of next and previous button
Pros
- Data is not visible to user
- No storage Limitation
Cons
- Form needs to be submitted resulting some overhead not sure what (main
cause of not being 100% sure)

2. Use cookies to store selection
Pros
- Data is not easily visisble
- Available at client side not sure if boost performance as read write
operation to the cookie file
Cons
- Size limitation. I think t is 4096 bytes or 255 characters.
- The clicking on next button will do saving to cookie and trip to server
though..

Some users might delete more than 50 records...so really bit confused. If I
have to go cookie way then have to restrict users from checking more that say
50 records.
Please let me know your thoughts
Thanks and Regards,
Parag
 
D

daddywhite

To be honest its down to personal prefernce.

Use whichever you like.

If you want to restrict the user to 50 clicks (or however many) then I
have the code to do that if you wish. Alternatively I suppose you
could javascript to count the characters and if over 200 then disallow
any more selections? I dunno.

If it is truly 255 character limit it depends on the size of the ids
of record you want to delete. For example if an ID is "25678655" then
that uses up more characters than "23" obviosuly!
 
J

Jim Rodgers

Parag Gaikwad said:
Hi All,
I have a large recordset to be displayed on a ASP 3.0 page. I am using
recordset paging for this. For the next and previous link i am passing href
as
<a href=<Page URl>?page=<%= iPageCurrent - 1 %> for Previous.
Now when i display the contents i also add a checkbox (for deletion) to each
of the records.
Now user should be able to select one or more checkboxes across pages and
then should be allow to delete all selections together.
Can anyone please point me right directions or give some hints on how to
acheive this.
Thanks and Regards,
Parag

Parag,

It seems to me you would like to use HTML forms. If you
can put a checkbox on every record, then you are already
building a form. I assume you have some way of assigning
names to each checkbox so it exactly identifies which record
needs to be deleted. I would use the primary key of the
table; that would simplify the SQL for deleting the record.

The nice thing about forms and inputs is you don't have to
display the primary key. Rather, just use it as the name of
the checkbox.

Next, add HTML at the top of the page before the <table>
element that you use to display the records. This HTML sets
up the <form> for you. THE IMPORTANT THING IS TO USE
method="post" in the <form> element like this...

<form method="post" action="myPage.asp">

Where ever you need to put the Next and Previous links, you
need to use form inputs instead of anchor links like this...

<input type="submit" name="Choice" value="Previous" id="idPrevAction">
<input type="submit" name="Choice" value="Next" id="idNextAction">

The value of the submit button is both the default label for the
button, and it is the value passed by the form. Since you only
can click on one button at a time, it's OK they have the same
name. If you need to identify them uniquely for any reason,
use the id= parameter. The id's must not be the same.

....and on each record the checkboxes need to be <inputs>
also like this...

<input type="checkbox" name="PrimaryKey_iii" value="delete">

where the name is actually some value, perhaps the primary
key of each record, that will help you delete stuff.

At the top of this page, even before the <form> starts, there
is VBScript (or whatever) code that checks the incoming data
from the foregoing page. So if the user checks a bunch of
checkboxes, you will first delete these records before doing
anything else.

It might at first seem to be a problem that you don't know the
names of the checkboxes, but this is OK. Consider how this
code works...

For iForm = 1 To Request.Form.Count
If Instr(UCase(Request.Form(iForm)), "DELETE") >0 Then
adodbConnection.Execute _
"DELETE * FROM myTable " & _
"WHERE myPriKey='" & Request.Form.Key(iForm) & "';" _
, nRecordsDeleted
End If
Next ' iForm

Bang! There gone. Also, it may at first seem inefficient to
delete these records one at a time rather than to build one big
delete SQL string, but not really. ASP and ADODB runs as fast
as a scalded dog! Anyway, you certainly can write one single
SQL rather easily if you wish, but I would get this much working
first.

After doing the record deleting, you need to decide if this was
a request for the next or for the previous set of records. You
can tell this by looking at the Request.Form object again. I'd
maybe do it something like this...

Select Case Request.Form("Choice")
Case "Previous"
' do what you need to do...
Case "Next"
' do what you need to do...
Case Else
Err.Raise myOwnErrorNumber, "myPage.asp", "The sky is falling!"
End Select

....and now render the page as usual. Believe it or not, I
never have used the recordset paging thing. I tend to write
that kind of code from scratch.

Being a post rather than a get, the URL is nice and clean,
and there is no practical limit on number of records deleted.
I believe this technique is browser independent and very
reliable. Plus, you get to do the whole thing in VBScript and
run it on the server! None of the javascript jibberish (okay, so
I am totally prejudiced against javascript). One of the
problems with javascript is how careful you must be to write
it in a browser neutral way. For example, you need to make
extensive use of GetElementById() as in the follwoing...

function SetFocus(someThing) {
var oMyElement = document.GetElementById(someThing);
oMyElement.focus()
}

If you refer to object by their name directly (Microsoft style),
then you will have problems with Firefox and Opera, etc.

Did I understand what you needed? Does this help at all?

Cheers,

Jim Rodgers
 
D

daddywhite

Parag,

Jim is right this will work also , it depends if you mind that the
records are deleted straight away or not.

I assumed you would the user to leisurely scroll through the pages
selecting (and maybe even deselecting) which records to delete before
finally clicking "Delete All checked records" link or something
similar.

It depends on the nature of the application/website.

Regards
Dave
 
P

Parag Gaikwad

Thanks Jim,
Actually myself and dave having been discussing about the option you
suggested.
The only question in my mind is :- On clicking of the next and prev the form
is submitted. How much impact this has on performance instead of just calling
the same page url (in href) with different PAGE QUERYSTRING value.

Looking forward to your response.
thanks and regards,
paarg
 
P

Parag Gaikwad

Dave,
Your assumption is correct. I do need the functionality as you mentioned
i.e. Delete all selected records link.
As mentioned in my reply to Jim
The only question in my mind is :- On clicking of the next and prev the form
is submitted. How much impact this has on performance instead of just calling
the same page url (in href) with different PAGE QUERYSTRING value.
Your views on this would be really helpful.
Thanks and Regards,
Parag
 
D

daddywhite

I dont personally think there will be a noticable impact on
performance. After all its only one extra fiels your submitting. I
think your worrying about nothing there.

Get on with it and let us know how you get on.

Regards
Dave
 
J

Jim Rodgers

Parag Gaikwad said:
The only question in my mind is :- On clicking of the next and prev the form
is submitted. How much impact this has on performance instead of just calling
the same page url (in href) with different PAGE QUERYSTRING value.

Parag,

I prepared a tutorial for you. This took a long time. I didn't
have the time also to go back and check everything Dave told
you, so I don't if is redundant, or in conflict or what. Please
read this carefully, and I apologise if I'm telling stuff you
already know. I want to be helpful. It is my way of paying
back this newsgroup for helping me with hard problems (and
with easy ones, too.)

Before I start let me go ahead and say this: there are no
performance differences to worry about. Actually, you may
have a lot of work to do. Performance is the least of your
problems.

======================================
Links and the Two Types of Forms
======================================

Clicking on a Submit button when the form is like this...

<form method="get" action="myPage.asp">
<input type="checkbox" name="fileName1" value="delete">
<input type="checkbox" name="fileName2" value="delete">
</form>

-IS- the -EXACT- same thing as clicking on a link where...

<a href="myPage.asp?fileName2=delete">
Next Page
</a>

....assuming that only checkbox #2 is checked.

So, these two are the same. But, this is also the same as
when method="post" except for two things:

(1) The "post" method hides the data so the user will not
see it in the URL. With the href or with method="get",
the data appears on the URL when the user gets to the
next page.

(2) Usually putting the data into the URL is not important,
but it can be if the length limit of URL (the QUERYSTRING
limit) also limits how many rows you can delete. Which
it does. NOTE: method="post" does not put the data
into the URL QUERYSTRING. It puts it into the IP packets
somewhere that more data is allowed than on the URL.

Another difference is you use different ASP functions when
you read the data on the "next page." Here are the functions:

— for forms with method="get" and for links (href="...")

strControlValue = Request.QueryString("strControlName")

— and for forms with method="post"

strControlValue = Request.Form("strControlName")

....where "strControlName" is the value you had in name=""
on the checkbox (or any other input of the form). And the
value="" parameter will go into strControlValue on the "next
page."

======================================
How to Structure an ASP file
======================================

I say "next page" because it is the same page, of course. Go
back and read my examples again. Try to understand every
line. The first thing you do on this page is to determine what
happenned that brought you to this page. Did someone enter
the page name in the address bar of their browser? Did the
user click on "Previous"? On "Next"? You need to figure this
out first thing at the beginning of your asp page.

Now, from a programming point of view, there are many ways
to go through the data once you are back again after clicking
on the "Next" or "Previous" button. Let's talk about that for a
minute.

This is HTML: When you have many checkboxes on a page,
and only some of them are checked, it is these checked ones
that appear in the data. The unchecked boxes are ignored.
The checked boxes appear on the next page as either values
in the Query string or as values in the Form collection.

In the Query string, of course, they look like this:
myPage.asp?name1=value1&name2=value2&name2=value2

In the Form(i) collection, you have to "look" at each Form(i).

Because you don't know the names of the checkboxes that
were checked beforehand, part of what you have to do is to
determine which ones if any were checked. So, you go
through the Query string or through the Form(i)'s until you
get to the end.


======================================
How to Delete a Bunch of Records
======================================

In my previous example, I described how you can
read the Form data from Request.Form(i) and delete the
records. That technique was BACKWARDS from what a lot
of people know. Here, I will show you the "forwards"
way to do this. Both work well. It's personal.
Most programmers would probably prefer the following
technique for your particular problem.

Suppose you named every checkbox with the same name,
and that is name="delete". And each value was something
like value="record1" and value="record2" and like that.
Then what you find when you get to the next page is this...

delete=record1,record2,record3 ...and so forth

(assuming these are the checkboxes you checked)

You can use the VBScript function Split() to make an array
of records_to_delete like this:

Dim strTemp, strRecords
strTemp = Request.Form("delete")
strRecords = Split(strTemp,",")

or if you use href="myPage.asp" (or method="get") then

Dim strTemp, strRecords
strTemp = Request.QueryString("delete")
strRecords = Split(strTemp,",")

....no real differences between them except the limitations in
length you know about already. Split() makes strRecords into
an array containing all the things in strTemp that were
separated by commas.

When you want to delete a file you can do this...

Dim myConnection, iRec, nDeleted
myConnection = Server.CreateObject("ADODB.Connection")
For iRec = 1 to UBound(records)
myConnection.Execute "DELETE * FROM myTable " & _
"WHERE priKey='" & records(iRec) & "';" _
, nDeleted
Next ' delete each record one at a a time

It depends on the database you use, but I think you might be
able to do the following if you want to delete them all at once:

Dim strSQL, iRec, nDeleted
If UBound(strRecords)>0 Then
strSQL = "DELETE * FROM myTable WHERE priKey IN ("
strSQL = strSQL & " '" & strRecords(iRec) & "'"
For iRec = 2 to UBound(strRecords)
strSQL = ", '" & strRecords(iRec) & "'"
Next
strSQL = strSQL & " );"
myConnection.Execute strSQL, nDeleted
Response.Write "<br>" & CStr(nRecords) & _
" were deleted.<br>" & vbCrLf
End If

I tested this for you and it WORKS with an MSAccess database
file (.mdb) assuming that "priKey" is the name of your primary
key and it is a string. Leave out the single quotes if it
is not a string.

If the priKey value were not strings, you could even
do this:

Dim strTemp, strRecords, strSQL, iRec, nDeleted
strTemp = Request.Form("delete")
strRecords = Split(strTemp,",")
If UBound(strRecords)>0 Then
strSQL = "DELETE * FROM myTable " & _
"WHERE priKey IN (" & strTemp & " );"
myConnection.Execute strSQL, nDeleted
Response.Write "<br>" & CStr(nRecords) & _
" were deleted.<br>" & vbCrLf
End If

In fact, if strKey values WERE strings, this should work:

Dim strTemp, strRecords, strSQL, iRec, nDeleted
strTemp = Request.Form("delete")
strRecords = Split(strTemp,",")
strTemp = "'" & Replace(strTemp, ",", "','") & "'"
If UBound(strRecords)>0 Then
strSQL = "DELETE * FROM myTable " & _
"WHERE priKey IN (" & strTemp & " );"
myConnection.Execute strSQL, nDeleted
Response.Write "<br>" & CStr(nRecords) & _
" were deleted.<br>" & vbCrLf
End If


======================================
A Few More Ideas For the Webpage
======================================

You could have on each page THREE buttons. All of
them have name="Choice" loike I mention in my previous
message. One button is to DELETE the records which
are checked, and the other two for the Next and Previous
functions.

Putting it all together, this the new part of your
new asp file:


'
Dim strTemp, strRecords, strSQL, iRec, nDeleted
Dim myConnection
Set myConnection = Server.CreateObject("ADODB.Connection")
myConnection.Open "...Connection String goes here..."
'
'
' First, take care of the records.
'
'
Select Case UCase(Request.Form("Choice"))
Case "DELETE"
'
strTemp = Request.Form("delete")
strRecords = Split(strTemp,",")
strTemp = "'" & Replace(strTemp, ",", "','") & "'"
If UBound(strRecords)>0 Then
strSQL = "DELETE * FROM myTable " & _
"WHERE priKey IN (" & strTemp & " );"
myConnection.Execute strSQL, nDeleted
Response.Write "<br>" & CStr(nRecords) & _
" were deleted.<br>" & vbCrLf
Else
Response.Write "<br>NO records were deleted.<br>" & vbCrLf
End If
End Select
'
'
'
' Next, render the correct page of records.
'
'
Select Case UCase(Request.Form("Choice"))
Case "PREVIOUS"
'
' render the "PREVIOUS" page of records
'
Case "NEXT"
'
' render the "NEXT" page of records
'
Case "DELETE"
'
' render the "SAME" page of records again
'
Case Else ' you got here from ANOTHER page!
'
' render the "FIRST" page of records
'
End Select
'
'
myConnection.Close
Set myConnection = Nothing


Good Luck Parag,

Jim Rodgers




======= An Aside on References =======

By the way, good VBScript and ASP Classic references are
the following:

http://msdn2.microsoft.com/en-us/library/d1wf56tt.aspx
http://www.w3schools.com/vbscript/default.asp
http://msdn2.microsoft.com/en-us/library/ms524664.aspx
http://www.w3schools.com/asp/default.asp

The MSDN references are deteriorating rapidly. MS seems to
be circling the drain under new management. It is fantastically
ironic that the w3schools sites are even on this short list!
Others in this newsgroup will give you more great references.

======================================
 
P

Parag Gaikwad

Thanks a lot Dave.
I have not got it working both ways the Submit and cookie way.
Once again many thanks for helping me out.
Best Regards,
Parag
 
P

Parag Gaikwad

Thanks a lot Jim.
Your inputs were very useful.
I have got it working both ways i.e. submit and cookie.
Once again thanks for the help.
Best Regards,
Parag
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top