简体   繁体   中英

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

ASP.Net generates its own IDs for server-side controls.

You can write <%= chkbox4PubnOrder.ClientID %> to get this generated ID.

Or you can always view the source of the page after it's in the browser and see what ID it gave to the control. It's typically built off the page name and any additional "layers" you may have put in such as user controls, etc.

<%= 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 ie 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM