0

I have 2 checkboxes in a GridView. I want to validate them 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>

And my JavaScript condition is:

function check_one() {
            var obj = document.form1;
            if (obj.chkExists.checked == true || obj.chkExistsInUpdate.checked == true) {
                alert("Plese check only one checkbox...");
                return false;
            }
            else
                return true;
        }

But I don't know that how to access the checkboxes from GridView on the client side? Please suggest an example.

2

1 Answer 1

1
MyGridView = document.getElementById('<%= this.MyGridView.ClientID %>');
var Inputs = MyGridView.getElementsByTagName("input");
var chkBox = "chkExists";
for(var n = 0; n < Inputs.length; ++n)
     if(Inputs[n].type == 'checkbox' && 
        Inputs[n].id.indexOf(chkBox,0) >= 0 && 
        Inputs[n].checked)

      return true;   

Similarly you can check for the other checkbox too...

Sign up to request clarification or add additional context in comments.

2 Comments

My checkbox are in front of each other.. so ur code not working
Front of each other.. lol.. dude.. just post clear questions, else don't expect the answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.