0

Possible Duplicate:
Validating checkboxes present inside gridview with Javascript

This is my aspx code...
 <asp:TemplateField HeaderText="IsExist">
            <ItemTemplate>
                <asp:CheckBox ID="chkExists" runat="server" Text="Exists" AutoPostBack="false" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Not Exists In Update">
            <ItemTemplate>
                <asp:CheckBox ID="chkExistsInUpdate" runat="server" Text="NotExists" AutoPostBack="false"/>

            </ItemTemplate>
        </asp:TemplateField>

Here I have 2 checkboxes but in different columns i.e. chkExist for Exist column and chkExistInUpdate for NotExist column in gridview. I want If they both checked then alert() mesg should display like "You can check Only one checkbox".. So here is my javascript code....

 function check_one() {
            var obj = document.form1;
            var isValid = false;
            var gridView = document.getElementById('<%= Gridview1.ClientID %>');
            for (var i = 1; i < gridView.rows.length; i++) {
                var inputs = gridView.rows[i].getElementsByTagName('input');
                if (inputs == null) {
                    if (inputs[0].type == "chkExists" && inputs[0].type == "chkExistsInUpdate") {
                        if (inputs[0].checked && inputs[0].checked) {
                            isValid = true;

                        }

                    }
                    var ch1 = inputs[0].type["chkExists"];


                }
                //alert(inputs[0].type == "chkExists");
                alert(ch1);

            }
            alert("Plese select only 1 check");
            return false;

        }

So plese suggest me that what changes has to do in javascript...

0

1 Answer 1

0

You should use radio buttons, and set a group name dependent by row, because you have a group per row:

<asp:TemplateField HeaderText="IsExist">
            <ItemTemplate>
                <asp:RadioButton ID="chkExists" runat="server" Text="Exists" AutoPostBack="false" GroupName='<%# Eval("Id") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Not Exists In Update">
            <ItemTemplate>
                <asp:RadioButton ID="chkExistsInUpdate" runat="server" Text="NotExists" AutoPostBack="false" GroupName='<%# Eval("Id") %>'/>

            </ItemTemplate>
        </asp:TemplateField>

But, if you need checkboxes anyway (it doesn't make sense for me), you can use jQuery:

function check_one() {
        $('#<%= Gridview1.ClientID %> tr').each(function() {
            if(jQuery(":checkbox", this)
              .filter(function(i) { return this.checked; }).length != 1) { 
               alert("Plese select only 1 check");
            }
        });
}
Sign up to request clarification or add additional context in comments.

2 Comments

I want to use checkbox... Requirment is checkboxes.. so plz suggest me...
Can u possible then plz suggest me in normal javascript which I tried...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.