views:

51

answers:

2

As I have check box in gridview if i dont select any one check box and if i click asp button then i have to show message to user to select check box

awaiting ur response

A: 

If you want to do this client side you could use a library like jQuery to iterate through the checkboxes.

If you want to do this server side, you will need to reenumerate the controls on postback, and check the Checked value. Alternatively if this GridView binds to a DataSource, check the posted back values within the DataSource.

Tim
i need java script the page should not post back when i click check box
Domnic
+2  A: 

Should be something like you need...

Boolean Selected = false;
    for (int count = 0; count < grd.Rows.Count; count++)
    {
        if (((CheckBox)grd.Rows[count].FindControl("yourCheckbox")).Checked)
        {
            Selected = true;
        }
    }
if (Selected == false)
    {
        //your message goes here.
    }

if you need javascript code...

 function CheckIfSelect() {
        var frm = document.forms[0];
        var Selected=false;
        for (i = 0; i < frm.elements.length; i++) {
            if (frm.elements[i].type == "checkbox") {
                if(frm.elements[i].checked)
                {
                Selected=true;
                break;
                }
            }
            if(Selected==false)
            {
            //your message goes here
            }
        }
    }
Muhammad Akhtar
i already used onClick event in that button how can i call javascript
Domnic
like... <asp:Button OnClientClick="javascript:yourfunction();"
Muhammad Akhtar