zoukankan      html  css  js  c++  java
  • asp.net从masterPage继承的页面,如何在客户端用javascript取control(比如textbox)的值

      最初是想在用户点“确定"按钮之前给一个confirm,就像这样

    <asp:Button 
        ID="btnDelete" 
        runat="server" 
        Text="Delete" 
        UseSubmitBehavior="false" 
        Click="btnDelete_Click" 
        OnClientClick="return confirmation();" />
    function confirmation() {
        if(confirm("Are you sure you want to delete?")) 
            return true;
        else return false;
    }

    但是,发现这样的congfirm 没有任何和页面相关的信息,不好 , 提示信息应该是类似 : Are you sure you want to delete TextBox1.text ?

    这样,问题就转换为 : javascript 如何在客户端取textbox控件的值?

    经多次google,发现正确的方法是 :

    1: 把这段js放到子页面的mainContent下面

        <script type="text/javascript">
            function confirmation() {
                var txtName = '<%= TextBox1.ClientID %>';
                var upEmail = document.getElementById(txtName).value==""  ?  "root node" : document.getElementById(txtName).value;
                if (confirm("Are you sure you want to register under " + upEmail + "?"))
                    return true;
                else return false;
            }
        
        </script>

    注意不能放到 master页面 , 因为如果master页面没有TextBox1 ,会报错。

    2 : 控件的写法和原来一样

    <asp:Button 
        ID="btnDelete" 
        runat="server" 
        Text="Delete" 
        UseSubmitBehavior="false" 
        Click="btnDelete_Click" 
        OnClientClick="return confirmation();" />

    Over.

  • 相关阅读:
    【LeetCode】3Sum Closest 解题报告
    LOJ#6277. 数列分块入门 1
    洛谷P1919 【模板】A*B Problem升级版(FFT快速傅里叶)
    LOJ #108. 多项式乘法
    快速傅里叶变换(FFT)详解
    HDU 5536 Chip Factory
    洛谷P4093 [HEOI2016/TJOI2016]序列
    洛谷P2633 Count on a tree
    HDU 4825 Xor Sum
    洛谷T21778 过年
  • 原文地址:https://www.cnblogs.com/lthxk-yl/p/3690412.html
Copyright © 2011-2022 走看看