简体   繁体   中英

Use a variable derived from Javascript into an ASP tag

I'm having difficulty in using a variable derived from Javascript into an ASP tag .

<script language="javascript"> 
    if (arg) 
    {
        var text_box_to_fill = arg.sendValue_Code;                  
        document.getElementById( document.getElementById("<%= "+text_box_to_fill+".clientID %>").value = selected_libelle_value;
    }
</script>

Any suggestions?

Not sure why there are duplicate "document.getElementById" code references, but you can only go from server to client in this fashion. So it would be:

<script language="javascript"> 
    if (arg) 
    {
        var text_box_to_fill = arg.sendValue_Code;                  
        document.getElementById("<%= text_box_to_fill.ClientID %>").value = selected_libelle_value;
    }
</script>

EDIT: Note that you have to have the script in the same page or control as the origin of "text_box_to_fill". If you have this script in a page, and the textbox is in the user control, that won't work.

text_box_to_fill is defined client side in the browser, whereas the <% %> tags are evaluated on the server. It's not possible to reference javascript variables from the server code.

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