CheckBox checkbox in asp.net gridview (Firefox compatible)

Source: Internet
Author: User

In asp.net, you can use the template column, in the GridView implementation of the CheckBox column, the relevant code is not complex, you can refer to here, I extracted some of the code as follows:

The code is as follows Copy Code

<script language= "javascript" type= "Text/javascript" >    function SelectAll (obj)
    {
        var thetable  = Obj.parentElement.parentElement.parentElement;
        var i;
        var j = Obj.parentElement.cellIndex;
        for (i=0;i<thetable.rows.length;i++)
        {
            var objcheckbox = Thetable.rows[i].cells[j].firstchild;
            if (objcheckbox.checked!=null) objcheckbox.checked =
    </script>
<asp:gridview id= "gridview1″runat=" Server " allowpaging= "True"

Autogeneratecolumns= "False"
datakeynames= "id" datasourceid= "Accessdatasource1″

allowsorting= "True" ondatabinding= "gridview1_databinding"
onrowdatabound= "GridView1_RowDataBound" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:checkbox id= "checkbox1″runat=" Server "
Checked= "True" text= ' <% #DataBinder. Eval (Container.DataItem, "id")%> '/>
</ItemTemplate>
<HeaderTemplate>
&nbsp;<input id= "Checkall" type= checkbox "onclick=" SelectAll (this); "/> Select all </HeaderTemplate>
<asp:boundfield datafield= "id" headertext= "id" insertvisible= "False" readonly= "True"

sortexpression= "id"/>
<asp:boundfield datafield= "name" headertext= "name" sortexpression= "name"/>
<asp:boundfield datafield= "Sex" headertext= "sex" sortexpression= "sex"/>
<asp:boundfield datafield= "DeptID" headertext= "DeptID" sortexpression= "DeptID"/>
</Columns>
</asp:GridView>

The SelectAll (obj) method in the JS code can be used normally in IE. Yesterday, a friend to do the test, found in Firefox does not support, let me help find the reason, the estimated is JS code is not compatible with Firefox, check the relevant information, the original in Firefox does not support parentelement, but support parentnode, So the original JS code modified a bit, through the obj.parentNode.parentNode.parentNode to get the table objects (the GridView compiled form), but to find the CheckBox object to Trouble Point, Because there is no way to locate the checkbox by Obj.parentElement.cellIndex, you can only find it by looping through cells in a row. The specific code is as follows:

The code is as follows Copy Code


function Select_all (obj)
{
var thetable = Obj.parentNode.parentNode.parentNode;
var j = Obj.parentNode.cellIndex;

for (var i=0; i<thetable.rows.length; i++)
{
var objcheckbox = getcheckboxobj (Thetable.rows[i].cells[j]);
if (objcheckbox.checked!= null)
objcheckbox.checked = obj.checked;
}
}

function Getcheckboxobj (Objcell)
{
for (var i= 0; i < objCell.childNodes.length; i++)
{
if (objcell.childnodes[i].id)
{
return objcell.childnodes[i];
}
}
}

After this modification, the checkbox will be compatible with IE and Firefox.

It seems that if you want to be compatible with IE and Firefox in web development, the job is pretty daunting. About the JavaScript difference between Firefox and IE you look here, the summary is very detailed. Not only is js,css also a lot of different places. Hey, I kind of miss ie Eminence era, but I think the general enterprise-class application or not specifically to consider Firefox, unless the portal site

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.