0

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?

1
  • You can add a CustomValidator in your user-control; then perform the validation there. Commented Dec 28, 2015 at 9:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.