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.

posted on 2014-04-25 23:50  齐文宣  阅读(298)  评论(0编辑  收藏  举报

导航