[英]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.