Search code examples
javascriptcheckboxdetailsviewconfirmbutton

how to get control inside ASP DetailsView through javasscript?


could anybody describe me how to find a control inside a ASP DetailsView using javascript? my requirement is to display a confirm box on a button's client click that checkbox is checked or not.


Here is the code working without DetailsView-

<script type="text/javascript" language="javascript">
function confirmation() {
    var chkbx = document.getElementById("chkbox4PubnOrder");

    if (chkbx.checked == false) {
        var answer = confirm('Are you sure to add a feature which be published');
        if (answer) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return true;
    }
}
</script>

where chkbox4PubnOrder is a asp checkbox. In case of details view, the above code is unable to find the checkbox
And I fire this onclientclick event of asp button-

OnClientClick="if(!confirmation()) return false;"

Please help...


Solution

  • <%= chkbox4PubnOrder.ClientID %> also does not work as the control is inside the ASP DetailView. i tried the same manner as we search a control inside a GridView or datagrid. That was also no luck.

    i tried to get the control as below

    <script type="text/javascript" language="javascript">
    function confirmation() {
    
        // first finding asp detailsview
        var detailsview = document.getElementById('<%= DetailsView1.ClientID %>');
        //then finding control inside the detailsview
        var chekbx = detailsview.getElementByTagName("chkbox4PubnOrder");
    
        if (chkbx.checked == false) {
            return confirm('Are you sure to add a feature which be published');
        }
        else {
            return true;
        }
    }
    </script>
    

    This code does not show chkbx as null i.e. this finds the checkbox inside detailsview but could not find it is checked or not. Is the typecasting is needed? if yes then please describe how?