用户控件中的访问文本框 [英] Acessing textbox in usercontrol

查看:112
本文介绍了用户控件中的访问文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的ascx代码:

This is my ascx code:

<script language="javascript" type="text/javascript">
           function fnMsg() {
               var value = document.getElementById('txt').value

           }
       </script>





<input id="txt" style="WIDTH: 12%"  type="text" maxlength="10" name="txt"  runat="server"/>
  <input type="button"  runat="server" value="click"  önclick="fnMsg();" style="width: 33px" />



我无法在javascript中获得文本框的值.

错误是document.getElementById(''txt'')未定义.

无论如何,是否可以访问值



I couldnt get the value of text box in javascript.

The error is document.getElementById(''txt'') is undefined.

Is there anyway to access the value

推荐答案

如果您使用的是asp.net 4,则可以使用
If you are using asp.net 4 you can use
<asp:Textbox ClientIdMode="Static">


而不是服务器端的html标签.
或在aspx/ascx中获取此标签的客户端ID


Instead of your server side html tag.
Or obtain client id of this tag in aspx/ascx

var id="<%=txt.ClientID %>";


使用runat="Server"属性修改的HTML标记的ID属性会更改.该ID现在仅在服务器端可用,并且要获取该标签的ID,因为该ID在发送到浏览器的HTML中呈现时,您必须使用<%= txt.ClientID%>

看到这里:

The ID attribute of a HTML tag that is modified with the runat="Server" attribute changes. This ID is only available on the server side now and to get the ID of that tag as it is rendered in the HTML sent to the browser you''ll have to user <%= txt.ClientID %>

See here:

<script language="javascript" type="text/javascript">
    function fnMsg() {
        var value = document.getElementById('<%= txt.ClientID %>').value

    }
</script>



问候,

—曼弗雷德(Manfred)



Regards,

— Manfred


这篇关于用户控件中的访问文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆