nemo_87
nemo_87

Reputation: 4791

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?

In other words: I have custom control 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. 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?

Upvotes: 0

Views: 346

Answers (1)

Michal C
Michal C

Reputation: 181

Yes, it is possible. You can use ClientIDMode property (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.

Upvotes: 1

Related Questions