简体   繁体   English

如何在客户端的ASP.NET中获取自定义文本框控件的javaScript'value'属性

[英]How to get javaScript 'value' property for custom text box control in ASP.NET on client side

Is there a way to access specific textbox control inside custom ASP.NET control from another page by using javascript? 有没有一种方法可以使用JavaScript从另一个页面访问自定义ASP.NET控件内的特定文本框控件?

In other words: I have custom control BankInformation: 换句话说:我具有自定义控件BankInformation:

    <%@ Register Src="~/Controls/Attachments/SingleAttachment.ascx" TagName="SingleAttachment" TagPrefix="ABS" %>

    <div id="divIBAN" runat="server" class="control-group">
        <asp:Label ID="lblIBAN" runat="server" CssClass="control-label" AssociatedControlID="txtIBAN" meta:resourcekey="lblIBAN"></asp:Label>
        <div class="controls">
            <asp:TextBox ID="txtIBAN" runat="server"></asp:TextBox>
        </div>
    </div>
    <div id="divRoutingABANumber" runat="server" class="control-group">
        <asp:Label runat="server" ID="lblRoutingABANumber" CssClass="control-label" AssociatedControlID="txtRoutingABANumber" meta:resourcekey="lblRoutingABANumber"></asp:Label>
        <div class="controls">
            <asp:TextBox ID="txtRoutingABANumber" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator runat="server" CssClass="validator" ValidationGroup="vgDirectDebitApplication" ID="rfvRoutingABANumber" ControlToValidate="txtRoutingABANumber"  SetFocusOnError="True" Display="Dynamic" Enabled="false" meta:resourcekey="rfvRoutingABANumber"></asp:RequiredFieldValidator>
        </div>
    </div>

I've made an instance of this custom control inside of another control:

    <%@ Register Src="~/Controls/BankInformation.ascx" TagName="BankInformation" TagPrefix="ABS" %>

    <script type="text/javascript">
        $(document).ready(function () {
              var isAccountNumberEmpty = document.getElementById('<%=txtAccountNumber.ClientID%>').value; //This is not valid of course
              function validateAndCloseDirectDebitDialog(validationGroup, dialogID){
                if  (isAccountNumberEmpty != "")
                    dialogID.modal('hide');
                else {

                }
            }
</script>
<div id="bankInfoDialog" runat="server" class="modal hide fade" tabindex="-1" role="dialog">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" style="display: none">×</button>
        <h1 id="bankInformationDialogHeader"><%=GetLocalResourceObject("BankInfoHeader") %></h1>
    </div>
    <div class="modal-body">
        <ABS:BankInformation runat="server" ID="bankInformation" />
    </div>
    <div class="modal-footer">
        <asp:Button runat="server" ID="btnUpdate" meta:resourcekey="btnUpdate" CausesValidation="False" />
        <button id="btnCancel" runat="server" data-dismiss="modal" aria-hidden="true"><%=GetLocalResourceObject("Cancel")%></button>
    </div>
</div>

What I am trying to do is to access text box with an ID of 'txtAccountNumber' inside BankInformation control, from control where it is instantiated and get it's value so I can do client side validation on btnUpdate click event. 我想做的是在BankInformation控件中访问ID为'txtAccountNumber'的文本框,从控件的实例化位置获取其值,以便可以对btnUpdate click事件进行客户端验证。 I know how to do everything except most important part: Is it possible to get value of txtAccountNumber outside it's control and how to do this in javaScript if it is possible? 我知道除了最重要的部分外,该如何做所有事情:是否可以获取txtAccountNumber的值在它的控制范围之外?如果可能,如何在javaScript中执行此操作?

Yes, it is possible. 对的,这是可能的。 You can use ClientIDMode property (ASP.NET 4.0): 您可以使用ClientIDMode属性(ASP.NET 4.0):

<asp:TextBox ID="txtAccountNumber" runat="server" ClientIDMode="Static"></asp:TextBox>

then access text box: 然后访问文本框:

var accountNumber = document.getElementById('txtAccountNumber').value;

Another approach is to save txtAccountNumber.ClientID to global javascript variable, or better save it as property of global application object. 另一种方法是将txtAccountNumber.ClientID保存到全局javascript变量,或者更好地将其另存为全局应用程序对象的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将javascript添加到ASP.NET文本框控件的OnBlur属性 - Adding javascript to the OnBlur property of an ASP.NET text box control 如何使用JavaScript在客户端禁用asp.net控件 - How to disable asp.net control in client side using javascript ASP.NET:通过客户端JavaScript获取ASP.NET TreeView上选定节点的父节点文本 - ASP.NET: Get parent node text of selected node on an ASP.NET TreeView by Client Side JavaScript 如何从客户端(通过javascript)获取由asp.net ajax修改的文本框的值? - How to get the value of a textbox modified by asp.net ajax from client side(by javascript)? 如何在ASP.NET webform上使用javascript设置文本框的值 - How to set the value of a text box with javascript on an ASP.NET webform 如何使用javascript设置asp.net的label文本并在服务器端获取设置值 - How to set asp.net 's label 's text using javascript and get setted value on server side 如何从 ASP.net 自定义控件获取客户端值? - How to Get ClientSide Value from ASP.net Custom Control? 如何使用ASP.NET添加客户端(JavaScript)确认消息框? - How can I add a client-side (JavaScript) confirmation message box using ASP.NET? 如何从JavaScript控制ASP.NET验证程序控制客户端验证? - How to control ASP.NET Validator Controls Client Side validation from JavaScript? 在客户端读取asp.net控件 - read asp.net control in client side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM