 |
|

|
Thank you so much for the article, using the corrections i found in the chain of comments the article helped me a lot and saved tones of time!
|
|
|
|

|
javascript code to highlight row is not working
|
|
|
|
|

|
code is very old and not working on vs2008
|
|
|
|

|
Check out this link: http://demo.c-cubed.net/C3WebControlsDemo/C3DataGrid_MultiSelect.aspx[^]
It's a DataGrid with client-side multiselect. All events happen on the client-side, with events firing upon postback to indicate which items(s) changed selection status. The multiselect indicator can appear in any column (or none), and can contain any specified HTML (not just an icon). No more clunky checkbox datagrid multiselects!
Thanks - TL
|
|
|
|

|
Ver interesting job there Todd..
I went to the site and i loved the stuff.
Do you have the source code available?
Thanks
What you know today isn't enough for tommorow.
|
|
|
|

|
I ran into a problem when I tried to use multiple gridviews on the same page. When I checked the box on the top of one GridView, it checked all the boxes in all of them! I fiddled around with it for way too long, until I came up with the following solution. //check all the checkboxes in the gridview to select all rows //06/15/2006 - Added indexOf stuff because multiple gridviews made all checkboxes checked function SelectAllCheckBoxes(theBox, gvSelected) { var xState=theBox.checked; var elm=theBox.form.elements; var strElmID; for(i=0;i<elm.length;i++) { strElmID = elm[i].id; if(elm[i].type=="checkbox" && strElmID!=theBox.id && strElmID.indexOf(gvSelected)!=-1 ) { if(elm[i].checked!=xState) { elm[i].click(); } } } } Notice I am now searching for a string gvSelected in the id of the checkbox. Since the .NET naming convention will name all of the checkboxes with concatenation and numbering, all we have to do is specify a different id for each checkbox column, and search for that as we cursor through all the elements on the page. The following is the code for the first gridview and the beginning of the second: <asp:GridView ID="gvDefHrs" runat="server" AutoGenerateColumns="false" DataKeyNames="emp_def_earn_uuid" DataSourceID="SqlDataSource1" OnRowCommand="gvDefHrs_RowCommand" OnRowDataBound="gvDefHrs_RowDataBound"> <Columns> <asp:TemplateField> <HeaderTemplate> <input id="chkAllDefHrs" runat="server" onclick="SelectAllCheckBoxes(this,'chkSelectDefHrs');" type="checkbox" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chkSelectDefHrs" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:ButtonField ButtonType="Button" CommandName="EditDefHrs" Text="edit" /> <asp:BoundField DataField="earn_hrs" HeaderText="Hours" SortExpression="earn_hrs" /> <asp:BoundField DataField="emp_def_earn_uuid" HeaderText="UUID" SortExpression="emp_def_earn_uuid" Visible="false" /> </Columns> </asp:GridView>
<asp:GridView ID="gvDefDollars" runat="server" AutoGenerateColumns="false" DataKeyNames="emp_def_earn_uuid" DataSourceID="SqlDataSource2" OnRowCommand="gvDefDollars_RowCommand" OnRowDataBound="gvDefDollars_RowDataBound"> <Columns> <asp:TemplateField> <HeaderTemplate> <input id="chkAllDefDollars" runat="server" onclick="SelectAllCheckBoxes(this,'chkSelectDefDollars');" type="checkbox" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chkSelectDefDollars" runat="server" /> McDude lives
|
|
|
|

|
McDude, you are a legend, may you live longer. Thanks Mate.
The way I google it.
|
|
|
|

|
Hi,
is there a way to for example unselect checkbox#2 when #3 is check and the other way around?
also, how to get the ID/Name of the checkbox in question?
Thankx
|
|
|
|

|
hi
i want to add command buttons in data grid and each button should have seprate
click event.
i just want whenever user will click on particular button a particular .aspx file get open.
thanks
|
|
|
|

|
i have a problem with javascript function that the chkAll can't find it's children . what can i do ?
shadirahmani
-- modified at 6:55 Monday 5th September, 2005
|
|
|
|

|
hi there,
u can view HTML source code of the web page to identify the children controls's id so that u can have solution accordingly
<< >>
|
|
|
|

|
Testing this code showed that I had to remove the refrence for any children as their were no <span> tags to itrate through simply update the javascript to the following: //------------------------------------------------------------- // Select all the checkboxes (Hotmail style) //------------------------------------------------------------- function SelectAllCheckboxes(spanChk){ // Added as ASPX uses SPAN for checkbox var oItem = spanChk; var theBox=oItem xState=theBox.checked; elm=theBox.form.elements; for(i=0;i<elm.length;i++) if(elm[i].type=="checkbox" && elm[i].id!=theBox.id) { //elm[i].click(); if(elm[i].checked!=xState) elm[i].click(); //elm[i].checked=xState; } } //------------------------------------------------------------- //----Select highlish rows when the checkboxes are selected // // Note: The colors are hardcoded, however you can use // RegisterClientScript blocks methods to use Grid's // ItemTemplates and SelectTemplates colors. // for ex: grdEmployees.ItemStyle.BackColor OR // grdEmployees.SelectedItemStyle.BackColor //------------------------------------------------------------- function HighlightRow(chkB) { var oItem = chkB; var xState; xState=oItem.checked; if(xState) {chkB.parentElement.style.backgroundColor='lightcoral'; chkB.parentElement.style.color='white'; }else {chkB.parentElement.style.backgroundColor='white'; chkB.parentElement.style.color='black'; } } // --> Sasi Atia
|
|
|
|

|
great job, i was having the same issue with the children not recognized - this worked for me! thanks!
|
|
|
|

|
First of all, thanks for the code, it is very helpful
and for the those who are really lazy typing it , here is the C# version of the server code:
public void grdEmployees_CheckedChanged(object sender, System.EventArgs e)
{
CheckBox myCheckBox = (CheckBox)sender;
DataGridItem dgi;
dgi = (DataGridItem)myCheckBox.Parent.Parent;
if(myCheckBox.Checked)
{
dgi.BackColor = grdEmployees.SelectedItemStyle.BackColor;
dgi.ForeColor = grdEmployees.SelectedItemStyle.ForeColor;
}
else
{
dgi.BackColor = grdEmployees.ItemStyle.BackColor;
dgi.ForeColor = grdEmployees.ItemStyle.ForeColor;
}
}
|
|
|
|

|
this server method(grdEmployees_CheckedChanged) will not run when I check/uncheck the boxes. The client script works perfectly. I have followed all instructions. Is there anything I left out? Anyone has any idea?
|
|
|
|

|
When I tried this sample code and change AutoGenerateColumns to false, and add custom column template to the datagrid, I cannot retrieve the cell text.
in smample code:
mySelection.AppendFormat("{0} ", DemoGridItem.Cells(1).Text)
when AutoGenerateColumns="false", the cell text is empty.
Please help. thanks.
IT makes life crazy!
|
|
|
|

|
Try this one: <script language="javascript"> function SelectAllCheckboxes(chkBox) { var chk=chkBox.checked; var boxes=document.getElementById('dgMain').getElementsByTagName("input") for (i=0;i<boxes.length;i++){ if (boxes.item(i).type=='checkbox' & !(boxes.item(i)==chkBox)) { boxes.item(i).checked=!chk; boxes.item(i).click(); } } } function HighlightRow(chkB) { if (chkB.checked) { chkB.parentNode.parentNode.style.backgroundColor='black'; chkB.parentNode.parentNode.parentNode.style.backgroundColor='white'; } else { chkB.parentNode.parentNode.style.backgroundColor='white'; chkB.parentNode.parentNode.parentNode.style.backgroundColor='black'; } } </script>
|
|
|
|

|
This one works for me, Thank you.
|
|
|
|

|
if I use a button instead of a checkbox,
the onclick event in javascript give me and error
Jureshka
|
|
|
|

|
Great!
But can you tell me how to run this demo by Firefox browers? The java script code is not working!
Thanks!
Hoac Tai Thien
|
|
|
|

|
In Mozilla FireFox when I test it, the parentNode.parentNode only comes up to the cell level instead of row like in IE given if you use TemplateColumn. Just put a browser sniffer in and giving FireFox with parentNode.parentNode.parentNode and it will work just fine.
Have fun
|
|
|
|

|
When I've changed JavaScript It worked. See below.
function SelectAllCheckboxes( chkAll )
{
xState=chkAll.checked;
elm=chkAll.form.elements;
for(i=0;i |
|
|
|
|

|
a mjor problem with dotnetjunkies version, is that selection does not persits.. meaning even u select something and you implement a paging in datagrid or sorting .. the selection does not persist..
can somebody have a good link or idea about this
|
|
|
|

|
i'm writing an article on how to do that, but the data is stored in a database...soon i'll post it here
the article covers:
- binding data into a datagrid from a database
- checking/unckecking the row(s) when the page loads and in every postback
|
|
|
|

|
Nice try, but be original and don't rehash.
|
|
|
|

|
Hi,
I am surprised but thanks for your comments.
I know you are not going to believe. In fact, when I needed such a solution couldn't find it on internet and then I thought of trying on my own.
Anyways, if you could please mention the link would be helpful to verify and improve my solution.
|
|
|
|

|
Have some class ..."Dude" and don't be so judgmental.
|
|
|
|

|
"Dude" Appreciate what Prashant has done, rather that you yourself being judgemental and "Dude" attitude. Take Chill! Pill!
- web
|
|
|
|

|
Really nice contribution. What about the link?
|
|
|
|
|

|
I have used the "check all" function for the datagrid.
Locally when checking the header template check box all item template check boxes are checking
But after uploading them to site it's not working?
Pls help me.
|
|
|
|

|
I have used the "check all" function for the datagrid.
Locally when checking the header template check box all item template check boxes are checking
But after uploading them to site it's not working?
Pls help me.
|
|
|
|

|
it's really a great article dan i had used it in my application. but there is one thing that doesn't work.
after checked the CheckAll and i checked the chkSelect, the CheckAll still checked.
howto fix it
regards
|
|
|
|

|
I was looking for this feature and you have saved me time.
My datagrid is linked to a SQL database and holds user settings.
Does anyone know if its possible to call a client side jscript function from the server side vb code or vis versa.
THX in advance.
Keep up the good work.
|
|
|
|

|
First: You should use parentNode instead of parentElement, because this is understood by Mozilla and IE. I changed the hardcoded colors to className settings, so color configuration can be done in an external CSS file, that's nicer.
And all the objects given to te JS Functions are the CheckBoxes itself (you used "this" as parameter), so you don't have to find the checkBox.
Here comes my modified code:
//-------------------------------------------------------------
// Select all the checkboxes (Hotmail style)
//-------------------------------------------------------------
function SelectAllCheckboxes(theBox){
xState=theBox.checked;
elm=theBox.form.elements;
for(i=0;i |
|
|
|

|
Grischa,
This was great!!!
I've been spending literally, 4 hours looking for the solution to this bug..(I happend just to be 4months old in VB.NET)
can u pls tell me what was the original code's intention and how you found out that the "this" object was the box itself?
|
|
|
|

|
Hi Prashant
Nice job!!
I am trying to implement your code into my own datagrid coding.
It works OK when I clicked to select a single row, but not when I clicked to uncheck the same row.
I got an error when I tried to select the whole rows in a single page.
The error is: form.element is null or not an object!!
What's wrong? I have named my form as yours which is Form1 and I'm using Framework 1.0
Thanks
ILoveMicrosoft!!
|
|
|
|

|
how i can change the position of the Data grid columns on asp.net page
Have Look at this
|
|
|
|

|
In VS Design Mode u can go into DataGrid properties-->property builder then click on columns tab then u can move cols up and down by clicking on each column then click arrow,
OR
In HTML mode simply cut n paste where u want each column to appear
|
|
|
|

|
Folks,
This article,"Multi-select ASP.NET datagrid" by Prashanth is excellent. It tried and found really works. The only variation we have to follow is that, replace the line of code:
var theBox=oItem.item(0);
with,
var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
Thatz all. It works now.
Prashanth has given the code as per the .NET Framework 1.0. So if you are using .NET Framework 1.0, then it works fine. But if you are using the .NET Framework 1.1 or higher version, the follow the above variation. I have had to beat my head with some R&D with Prashanth's code and got the solution. Finally i found the code working with the above said change.
3cheers to Prashanth for providing this valuable source.
Thanks to all.
Thanks & Regards,
K.Nagalinga Reddy
AZTEC Software Inc.
Koramangala,Bangalore.
|
|
|
|

|
Thanks for the help its working now
|
|
|
|

|
Is amazing to see javascript all it can do and your article is just a great solution for my actual developing job. I would like use a checkbox more like select button so I just need one checkbox selected. But I new in javascript and I don´t know how I can unselect the selected checkbox before selecting another checkbox. I try combaning your allchecked function but Something I mess and It doesn´t work!.
So please help me and Thanks in advance!
Manolo Herrera
|
|
|
|

|
Hi, I have a datagrid with checkboxes in itemtemplate of datagrid as follows: <asp:TemplateColumn> <HeaderTemplate> <asp:Label id="lblchkAll" runat="server" ToolTip="Select each row">SelectTo</asp:Label> </HeaderTemplate> <ItemTemplate> <asp:CheckBox id="chkSelect" runat="server" onclick="javascript:check(this);" Text="Remove" AutoPostBack="false" ToolTip="Select/Deselect each row" OnCheckedChanged ="CheckedChanged"></asp:CheckBox> </ItemTemplate> </asp:TemplateColumn> and I have also wired checkbox event in itemcreated event of page as follows: public void ItemCreated(Object sender, DataGridItemEventArgs e) { // Get the newly created item ListItemType itemType = e.Item.ItemType; if((itemType == ListItemType.Item)||(itemType ==ListItemType.AlternatingItem) ||(itemType ==ListItemType.EditItem)) { TableCell QueryTableCell2 = new TableCell(); QueryTableCell = e.Item.Cells[0]; CheckBox cBox = (CheckBox)QueryTableCell.Controls[1]; cBox.CheckedChanged += new System.EventHandler(CheckedChanged); } and have impleneted this event as follows: public void CheckedChanged(Object sender,System.EventArgs e) { CheckBox chkTemp = (CheckBox)(sender); DataGridItem dgi; dgi = (DataGridItem)(chkTemp.Parent.Parent); if(chkTemp.Checked) { chkTemp.Checked = true; } else { chkTemp.Checked = false; } } also javascript in HTML part of page loos simillar as follows: <script>function check(chkB) { var xState=chkB.checked; if(xState) { chkB.Checked = true; } else { chkB.Checked = false; } } </script> Can you please suggest me where I may be missing something? Or why event is not firing? Thank you so much for help. Bharat.
|
|
|
|

|
I changed autopostback of the checkbox to true.
Whatever heh
|
|
|
|

|
Hi There,
I need to have a number (not all) of the check boxes check when the page loads. The user can then check new ones and un-check existing ones. I have everything worked out, except how can I make the check box for the row = true?
Any ideas
Thanks
Lee
|
|
|
|

|
Lee,
Just set checkedproperty of the checkbox to TRUEin page_load()event.
Ex:
checkbox1.checked=TRUE
MCSD (.NET Lover)
|
|
|
|
|
 |