We're working hard on the next version of Developer Fusion. Let us know what you think
we should be up to!
Members
Technology Zones
Articles
Hosted By
Info
|
[5258] Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid/GridView - Part 2: Maintaining CheckBox State Acr
Last post 04-10-2008 11:15 AM by nermeen. 15 replies.
-
01-01-1999 12:00 AM
|
|
-
-
DMarko1


- Joined on 09-19-2003

- Points 45
|
Highlight and maintain multi-selected checkbox row
Hello all,
Since I have had this article published here, I have now also been able to implement a way to highlight the selected row or entire set of rows and maintain this state across pages just as in my article, with of course the added benefit of showing users which row(s) they've checked, even when they happen to page back.
To get this going, follow these four steps.
First, add the style properties to the DataGrid:
Code:
<SelectedItemStyle BackColor="#F5EDED" ForeColor="Black">
</SelectedItemStyle>
<ItemStyle BackColor="White" ForeColor="Black">
</ItemStyle>
Second, add the highlightChkBxRow(this); JavaScript function call to both checkbox onClick events, so it now becomes:
Code:
OnClick="javascript: highlightChkBxRow(this); return select_deselectAll (this.checked, this.id);"
Third, in the code behind - add the following JS function within the Registered Script Block section to handle the client side checkboxes:
Code:
.Append ("function highlightChkBxRow(chkbx) {" & nl & nl)
.Append (" if (chkbx.id.indexOf ('DeleteThis') != -1) {" & nl & nl)
.Append (" if (chkbx.checked) {" & nl & nl)
.Append (" chkbx.parentElement.parentElement.style.backgroundColor='#F5EDED';" & nl)
.Append (" chkbx.parentElement.parentElement.style.color='#000000';" & nl)
.Append (" } else {" & nl & nl)
.Append (" chkbx.parentElement.parentElement.style.backgroundColor='#FFFFFF';" & nl)
.Append (" chkbx.parentElement.parentElement.style.color='#000000';" & nl & nl)
.Append (" }" & nl & nl)
.Append (" } else {" & nl & nl)
.Append (" var frm = document.forms[0];" & nl & nl)
.Append (" for (h = 0; h < frm.length; h++) {" & nl & nl)
.Append (" if (frm.elements[h].id.indexOf ('DeleteThis') != -1) {" & nl & nl)
.Append (" if (chkbx.id.indexOf ('CheckAll') != -1 && chkbx.checked) {" & nl & nl)
.Append (" frm.elements[h].parentElement.parentElement.style.backgroundColor='#F5EDED';" & nl)
.Append (" frm.elements[h].parentElement.parentElement.style.color='#000000';" & nl & nl)
.Append (" } else {" & nl & nl)
.Append (" frm.elements[h].parentElement.parentElement.style.backgroundColor='#FFFFFF';" & nl)
.Append (" frm.elements[h].parentElement.parentElement.style.color='#000000';" & nl & nl)
.Append (" }" & nl)
.Append (" }" & nl)
.Append (" } //loop" & nl)
.Append (" }" & nl)
.Append ("}" & nl)
And, finally to maintain our selected rows across pages, replace the RePopulateCheckBoxes () method with the one listed below:
Code:
Sub RePopulateCheckBoxes ()
CheckedItems = New ArrayList
CheckedItems = Session ("CheckedItems")
If Not IsNothing(CheckedItems) Then
'Loop through DataGrid Items
For Each dgItem in MyDataGrid.Items
ChkBxIndex = MyDataGrid.DataKeys(dgItem.ItemIndex)
'Repopulate DataGrid with items found in Session
If CheckedItems.Contains(ChkBxIndex) Then
CheckBox = CType(dgItem.FindControl("DeleteThis"), CheckBox)
CheckBox.Checked = True
dgItem.ForeColor = MyDataGrid.SelectedItemStyle.ForeColor
dgItem.BackColor = MyDataGrid.SelectedItemStyle.BackColor
Else
dgItem.ForeColor = MyDataGrid.ItemStyle.ForeColor
dgItem.BackColor = MyDataGrid.ItemStyle.BackColor
End If
Next
End If
'Copy ArrayList to a new array
Results = CheckedItems.ToArray(GetType(String))
'Concatenate ArrayList with comma to properly send for deletion
deletedIds = String.Join(",", Results)
End Sub
And that's it. You're now able to offer the user the added functionality of highlighting the row they've checked or all the rows and persist their selections across pages!
-Jimmy Markatos
|
|
-
-
aimaring


- Joined on 02-01-2006

- Points 20
|
A couple of points to note
You see, Dimitros,
while thank you very much for this article and other I was lucky to read before, I must admit there are times when you suggest a way to solve a problem in a quite suboptimal way.
It's rarely good to loop through all form elements to find checkboxes appropriate. Well must admit under normal circumstances it can hardly pose a performance hit, but still when number of records is high or the html file is large (which is not good, I know, but I saw times when there seemed to be no other way), it takes a certain amount of time to iterate through all elements. I think it's better to have a client-side array of ids of all checkboxes in question. And on the server side you just emit client side code for each checkbox to register itself with that array. In this case anytime you need to loop through all checkboxes you simply loop through array elements which is much better from performanse standpoint.
Then, I saw how you suggested to create client-side code via a long series of StringBuilder's Append method while it could have been done through static string concatenation. You can seed a bad practice, especially among newbies
No time to write more, gotta be going  Next time I'll write more.
Thank you for your articles. Usually, I like them
Yevgeniy
|
|
-
-
DMarko1


- Joined on 09-19-2003

- Points 45
|
Yevgeniy,
Thanks for your input, but I would have to disagree on looping through all the elements on the given page being a performance issue, when it's looping through 10 checkboxes per page, not a million. Additionally, utilizing state management via session or viewstate further enhances performance and scalability. So there's no such thing as suboptimal when you perform the task at hand in the manner best suited.
Now regarding your bias using the Stringbuilder class, again I would disagree. First of all, it's good practice in using Stringbuilder as there is performance advantages over static string concatenation - How StringBuilder string concatenation affects performance , string builder vs string concatenation "+" and Use StringBuilder to Build Strings Efficiently. So in my example it’s fine and justified.
I will however give you this though, that I definitely agree with you on not using StringBuilder for 5 lines of code. Incidentally, in Part 1 of my article I did in fact use string concatenation if that makes a difference for you.
But all in all Yevgeniy, I wouldn't just go out and post comments on articles unless you truly have facts to support your claims, which is why the statements you’ve made can "seed a bad practice, especially among newbies."
|
|
-
-
-
DMarko1


- Joined on 09-19-2003

- Points 45
|
No, I appreciate the point you're trying to get across Yevgeniy. At any rate, as I indicated earlier, I agree with you when the concatenation is for minimal strings, but when you are concatenating numerous strings or functions the overhead in instantiation is overall justified I feel, and better practice.
But either way, you're not really going to notice much truly in the way of performance, especially when you apply other performance enhancing techniques, ie, disabling debugging and tracing, closing connections, or even better bind a Datagrid from a Datareader instead of a DataSet for even greater advantages in speed, etc. In .NET, it's the collective means, rather than one tweak, that'll determine a scalable and efficient application.
This applies to looping page size, that in my experience simply displaying a few hundred per page is still ill-advised, and with anything one does in greater magnitude, comes with it degrading performance regardless of much code manipulation.
|
|
-
-
-
-
-
-
Timeware


- Joined on 07-18-2006

- Points 35
|
Re: [5258] Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid/GridView - Part 2: Maintaining CheckBox State Acr
Having tried your solution I was impressed by the ease with which it worked and it started me thinking of extensions to the principle.
Can I request that you extend the sample code with some additional features?
Firstly add an option to take all the selected records and then insert them into another file. One possible use of this would be to create a shopping 'wish list'.
Secondly to add an update feature. One use of this would be to amend prices of a products in price list, and produce a listing/report before committing the amendments.
Thirdly, add a count of records selected (displayed in the footer?).
And to be really cheeky, can you do it in ASP.NET 2.0 (in case you finish your version before I finish mine) ;-)
|
|
-
-
DMarko1


- Joined on 09-19-2003

- Points 45
|
Re: [5258] Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid/GridView - Part 2: Maintaining CheckBox State Acr
Hey Thanks! Good suggestions. Now the question remains in getting to those :-) As for .NET 2.0, this article has it already included.
Thanks again!
|
|
-
-
-
-
olegdelev


- Joined on 06-01-2007
- United States

- Points 5
|
Re: [5258] Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid/GridView - Part 2: Maintaining CheckBox State Acr
Hi Dimitrios,
Thanks for such a great tutorial. It works for me. Keep it up.
Oleg
|
|
|
Search
Code Samples
New Members
|