Click here to Skip to main content
16,019,140 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am having a check box in grid view.
But i want to allow only one check box to be checked at time
i.e if one check box is checked then other should be unchecked automatically.
I don wan to use any java script.can any one Gide me with resolving this.

Regards

Shiv
Posted
Updated 10-Apr-16 20:33pm
Comments
Sergey Alexandrovich Kryukov 26-May-11 20:57pm    
Please remove this all-caps. This is considered shouting in the Web, which is not very polite.
--SA
shivtk 1-Jun-11 9:07am    
Thanks for suggestion
Sergey Alexandrovich Kryukov 1-Jun-11 19:52pm    
Thank you for following it.
--SA

You can do this using Javascript function

JavaScript
function CheckOne(obj) {
    var grid = obj.parentNode.parentNode.parentNode;
    var inputs = grid.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "checkbox") {
            if (obj.checked && inputs[i] != obj && inputs[i].checked) {
                inputs[i].checked = false;
            }
        }
    }
}  



Now Call through Checkbox inside the grid

ASP.NET
<ItemTemplate>
  <asp:CheckBox ID="ChkSelect" runat="server"  onclick="CheckOne(this)" />
</ItemTemplate>
 
Share this answer
 
Does that not perfectly describe a radio button, instead of using a checkbox?
 
Share this answer
 
Comments
shivtk 26-May-11 8:44am    
Yes, but i wan to make the check box behave like radio button
Sergey Alexandrovich Kryukov 26-May-11 20:55pm    
You should not want it! Think about what your user want: to be compliant with standards and support commonly expected behavior of standard controls.
My 1 for your question, therefore.
--SA
shivtk 1-Jun-11 9:04am    
That doesn't matter whether u use radio button or check box as you can't group the radio button in the grid, as the data flooded to the grid is dynamic. so if u can suggest for radio button i can do it even for check box.

Shiv
Sergey Alexandrovich Kryukov 1-Jun-11 19:54pm    
If you use check box or radio button in the situation where is does not matter which one, it simply means you should not use either of them. Isn't that obvious?
--SA
[no name] 26-May-11 9:04am    
And why would that be worth the trouble?
At last i have found a solution. If any one feels the code is to be fine tuned please help me out.
It works fine for my requirement.

C#
protected void chkSelect_CheckedChanged(object sender, EventArgs e)
    {
        int i;
        CheckBox chk;
        for (i = 0; i &lt;= grdRmaDetails.Rows.Count - 1; i++)
        {
            chk = (CheckBox)grdRmaDetails.Rows[i].FindControl(&quot;chkSelect&quot;);
            if (chk.Checked == true)
            {
                if (jVal != i)
                {
                    iVal = i;
                    jVal = i;
                    if (jVal &gt;= i)
                    {
                        break;
                    }
                }
            }
        }
        for (i = 0; i &lt;= grdRmaDetails.Rows.Count - 1; i++)
        {
            chk = (CheckBox)grdRmaDetails.Rows[i].FindControl(&quot;chkSelect&quot;);
            if (i == iVal)
            {
                chk.Checked = true;
            }
            else
            {
                chk.Checked = false;
            }
        }
    }
}



Regards

Shiv
 
Share this answer
 
At last i have found a solution. If any one feels the code is to be fine tuned please help me out.
It works fine for my requirement.

<br />
<br />
Declare these variables:<br />
public static int iVal = -1;<br />
public static int jVal = -2;<br />
<br />
<pre lang="cs">protected void chkSelect_CheckedChanged(object sender, EventArgs e)
    {
        int i;
        CheckBox chk;
        for (i = 0; i <= grdRmaDetails.Rows.Count - 1; i++)
        {
            chk = (CheckBox)grdRmaDetails.Rows[i].FindControl(&quot;chkSelect&quot;);
            if (chk.Checked == true)
            {
                if (jVal != i)
                {
                    iVal = i;
                    jVal = i;
                    if (jVal &gt;= i)
                    {
                        break;
                    }
                }
            }
        }
        for (i = 0; i <= grdRmaDetails.Rows.Count - 1; i++)
        {
            chk = (CheckBox)grdRmaDetails.Rows[i].FindControl(&quot;chkSelect&quot;);
            if (i == iVal)
            {
                chk.Checked = true;
            }
            else
            {
                chk.Checked = false;
            }
        }
    }
}</pre><br />
<br />
<br />
Regards<br />
<br />
Shiv
 
Share this answer
 
v2

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