GridView plus CheckBox's full selection code

  • 2020-05-30 19:47:01
  • OfStack

First of all, I want to make an GridView with some materials
After the completion of GridView, we will add a new one in the rear (TemplateField).
Add 1 hang CheckBox to the new HeaderTemplate board and add 1 hang CheckBox to the new ItemTemplate board and then add 1 hang CheckBox to the new ItemTemplate board


<asp:TemplateField>      
                    <HeaderTemplate>      
                        <asp:CheckBox ID="CheckAll" runat="server" onclick= "SelectAllCheckboxes(this);"  Text=" All � / cancel " ToolTip=" According to the 1 Click again 1 Cancel the entire copyright once " />      
                    </HeaderTemplate>      
                    <ItemTemplate>      
                        <asp:CheckBox ID="CheckBox2" runat="server" Text=" All � "/>      
                    </ItemTemplate>      
</asp:TemplateField>

Then add Java Script below to the bottom of the picture


<script type="text/javascript">      
function SelectAllCheckboxes(spanChk)      
{      
    elm=document.forms[0];      

    for(i=0;i<elm.length;i++)      
        {      
            if(elm[i].type=="checkbox" && elm[i].id!=spanChk.id)      
            {      
                if(elm.elements[i].checked!=spanChk.checked)      
                 elm.elements[i].click();                      
            }      
    }      
}      
</script>

If you have a copy of MasterPage, put JS at the top < /asp:Content > To � � � the above had finished all � � � and cancel all the functions of � you can � � and � I � � to modify the part (you � � not make users just � � / cancelled all function according to the fun!), then placed on the surface of the � � 1 click � (Button), according to � � � � and � to cut programs, cut parts after I � � to program using the following code to catch out the columns (ROW) has been ticking


int i;      
for (i = 0; i < this.GridView1.Rows.Count; i++)      
{      
    if (((CheckBox)GridView1.Rows[i].FindControl("CheckBox2")).Checked)      
    {      
    Response.Write(GridView1.DataKeys[i].Value.ToString());      
    //GridView1.DataKeys[i].Value.ToString() You can catch the list DataKeys I'm going to say pk value       
    }      
} 

If you have an GridVie with an DataKeyNames=" image and space ", then the code at the top will not list the pk value

Using the above method, you can grab which columns have been checked by the user and use them for other programs you want


Related articles: