• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 894
  • Last Modified:

check only one checkbox in gridview

i have list of items which have checkboxes in front,
so i try to only allow only one checkbox can be checked in the gridview.
<asp:GridView ID="gvLS" runat="server" 
        AutoGenerateColumns="False" AllowPaging="True" CssClass="datatable"
                    CellPadding="5" BorderWidth="1px" BorderColor="#6E6E6E" 
                 Font-Names="Arial" Font-Size="Small" 
                PageSize="5" GridLines="None" AlternatingRowStyle-BackColor="#efefef" 
            Width="300px" 
                 HeaderStyle-HorizontalAlign="Left" 
            AllowSorting="True"   >
                    <HeaderStyle BackColor="#BDBDBD" />
                    <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:CheckBox id="cbCheckLevel" runat="server" />
                            <asp:Label ID="lblIDField" Visible="false" Text='<%# Eval("id") %>' runat="server" />
                    
                         </ItemTemplate>
                    </asp:TemplateField>

                    <asp:BoundField HeaderText="Level Code" DataField="code" 
                            SortExpression="code" HeaderStyle-ForeColor="Black"
                            HeaderStyle-CssClass="first" ItemStyle-CssClass="first" ReadOnly="True"  
                            HeaderStyle-HorizontalAlign="Left"  ItemStyle-HorizontalAlign="Left">
<HeaderStyle CssClass="first" ForeColor="Black"></HeaderStyle>

<ItemStyle CssClass="first"></ItemStyle>
                        </asp:BoundField>
                        
                            <asp:BoundField HeaderText="Level Name" DataField="name" 
                            SortExpression="name" HeaderStyle-ForeColor="Black"
                            HeaderStyle-CssClass="first" ItemStyle-CssClass="first" ReadOnly="True"  
                            HeaderStyle-HorizontalAlign="Left"  ItemStyle-HorizontalAlign="Left">
<HeaderStyle CssClass="first" ForeColor="Black"></HeaderStyle>

<ItemStyle CssClass="first"></ItemStyle>
                        </asp:BoundField>
                       
                     
                    </Columns>
        
                         </asp:GridView>

Open in new window

0
doramail05
Asked:
doramail05
  • 2
1 Solution
 
Sarika30Commented:
try using radio buttons in the same group.
0
 
doramail05Author Commented:
found article on repeater,

http://www.codeguru.com/csharp/csharp/cs_controls/custom/article.php/c12371/

changed to gridview
aspx
----

<asp:TemplateField>
                        <ItemTemplate>
                            <asp:RadioButton ID="rdoSelected" GroupName="gn01" runat="server" />
                            <asp:Label ID="lblLIDField" Visible="false" Text='<%# Eval("id") %>' runat="server" />
                    
                         </ItemTemplate>
                    </asp:TemplateField>
codebehind
----------

protected void gvLS_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            RadioButton rdo = (RadioButton)e.Row.FindControl("rdoSelected");
            string script =
               "SetUniqueRadioButton('gvLevel.*gn01',this)";
            rdo.Attributes.Add("onclick", script);
        }
    }

javascript
----------
<script type="text/javascript">
function SetUniqueRadioButton(nameregex, current)
{
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i]
      if (elm.type == 'radio')
      {
         if (re.test(elm.name))
         {
            elm.checked = false;
         }
      }
   }
   current.checked = true;
}
</script>

Open in new window

0
 
Sarika30Commented:
It is using radio buttons only...thts what i suggested :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now