Checkbox in gridview

99封情书 提交于 2019-12-25 01:39:33

问题


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

awaiting response


回答1:


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
            }
        }
    }



回答2:


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.



来源:https://stackoverflow.com/questions/1839558/checkbox-in-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!