Click here to Skip to main content
15,920,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi Friends,


I have a issue on grid checkbox if i had only 10 rows means its immediately checking but if the rows went up to 200 or 300 rows means raising popup error os stop running script?A script on this page is causing ie to run slow.
my code for loop is:
C#
int intRecIndex = 1;

            foreach (GridViewRow row in CGridView1.Rows)
            {

                chk = (CheckBox)row.FindControl("chkSelect");
                sel = (CheckBox)row.FindControl("chkSelect");

                if (intRecIndex <= CGridView1.Rows.Count)
                {
                    chk.Checked = true;
                    intRecIndex++;
                    checkall();

                }
                else
                {
                    chk.Checked = false;

                }
            }

 public void checkall()
    {

        sel.Checked = true;
        //CheckBox SelectedCheckBox = (CheckBox)sender;
        GridViewRow SelectedGridRow = (GridViewRow)sel.NamingContainer;
        CheckBox rb = (CheckBox)SelectedGridRow.FindControl("chkSelect");
        TextBox ddlgrpcode1 = (TextBox)SelectedGridRow.FindControl("CA_Reason_Desc3");
        ddlgrpcode1.Enabled = true;

    }


please help me how to resolve this!
Posted
Updated 22-Mar-12 6:18am
v7
Comments
phil.o 22-Mar-12 7:10am    
int.Parse(Convert.ToString(CGridView1.Rows.Count))

Is it a joke ?
You convert an int to a string, and then use int.Parse() to get the original int again ?
visnumca123 22-Mar-12 9:46am    
please take a look now!
ZurdoDev 22-Mar-12 8:11am    
The message is about JavaScript. Where is your JavaScript code?
visnumca123 22-Mar-12 8:17am    
on the click event i'm using this event and functions only not javascript!
ZurdoDev 22-Mar-12 12:54pm    
But the message is that a script is causing it to run slow so there is some script somewhere. How are you invoking the C#?

1 solution

Hi ,
this example will Guide you .

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript">
        function SelectAll(id) {
            var frm = document.forms[0];

            for (i = 0; i < frm.elements.length; i++) {
                if (frm.elements[i].type == "checkbox") {
                    if (frm.elements[i].disabled == true) {
                        frm.elements[i].checked = false;
                    }
                    else {
                        frm.elements[i].checked = document.getElementById(id).checked;
                    }
                }
            }
        }
        function unCheck(id) {
            var frm = document.forms[0];
            var paren = document.getElementById(id);
            var flag = true;
            for (i = 0; i < frm.elements.length; i++) {
                if (frm.elements[i].type == "checkbox") {
                    if (!frm.elements[i].checked) {
                        paren.checked = false;
                        if (frm.elements[i] != paren)
                            flag = false;
                    }
                }
            }
            if (flag == true) paren.checked = true;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="Gridview1" runat="server"
                        AutoGenerateColumns="False"
                    OnRowDataBound="Gridview1_RowDataBound" AllowPaging="True"

                        >
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:CheckBox ID="Chk" runat="server" />
                                </ItemTemplate>
                                <HeaderTemplate>
                                    <asp:CheckBox ID="Chk1" runat="server" />
                                </HeaderTemplate>
                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="test">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Val" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                      <asp:HiddenField ID="HF2" runat="server" />
    </div>
    </form>
</body>
</html>

C#
public partial class _Default : System.Web.UI.Page
{
    testDataContext db = new testDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        //binding to Grid
        var result = from x in db.tests
                     select new { x.name };
        Gridview1.DataSource = result;
        Gridview1.DataBind();

    }
    protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowType == DataControlRowType.Header))
        {
            HF2.Value = ((CheckBox)e.Row.FindControl("Chk1")).ClientID;
            //adding an attribut for onclick event on the check box in the hearder and passing the ClientID of the Select All checkbox
            ((CheckBox)e.Row.FindControl("Chk1")).Attributes.Add("onclick",
                "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("Chk1")).ClientID + "')");
        }
        else if ((e.Row.RowType == DataControlRowType.DataRow))
        {
            ((CheckBox)e.Row.FindControl("Chk")).Attributes.Add("onclick",
                "javascript:unCheck('" + HF2.Value + "','" + ((CheckBox)e.Row.FindControl("Chk")).ClientID + "')");

        }

    }
 
Share this answer
 
Comments
Mohamed Mitwalli 23-Mar-12 16:22pm    
was it useful ..??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900