0

Inside an aspx file I have asp:panels under an UpdatePanel

ASPX:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
 <ContentTemplate>
  <asp:Panel ID="PnlTraveler" runat="server" Visible="false">
    <input id="chkBoxVisAst" type="checkbox" class="checkbox" enableviewstate="true" />
  </asp:Panel>
 <ContentTemplate>
</asp"UpdatePanel>

If I write my Jquery codes in the same page under the asp:Content head to work with the click event it doesn't recognize the event id in my case chkBoxVisAst => this id.

JQuery

asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">


<script type="text/javascript">

    $(document).on('ready', function () {
        $('#chkBoxVisAst').on('click', function ()
        {
            if ($(this).attr("checked") == "checked")
            {
                alert("checked");
            } else {
                alert("unchecked");
            }
        });
    });
</script>

It works if I put my input(checkbox) outside my panel. How am I going to access my event id from inside the asp:panels?

1 Answer 1

2

The problem is in your code. You cannot add any element directly inside update panel. You have to put them in ContentTemplate.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Panel ID="PnlTraveler" runat="server" Visible="false">
                    <input id="chkBoxVisAst" type="checkbox" class="checkbox" enableviewstate="true" />
                </asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>

After adding the code above you will be able to access the checkbox element with its ID.

EDIT

Add the code below,

var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function () {
        $('#chkBoxVisAst').on('click', function () {
            if ($(this).attr("checked") == "checked") {
                alert("checked");
            } else {
                alert("unchecked");
            }
        });
    });

https://msdn.microsoft.com/en-us/library/bb311028(v=vs.100).aspx

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

3 Comments

Pardon me for I forgot to put it down. In my code contentTemplate was already there. If I make the basic panel visible then it gets the id but after validation of input that panel (PnlTraveler) is supposed to be visible. I just cant get the id after the validation . (After the Panel Updates)
Update my ans above@JabedHossain
Thanks a lot brother ... It helped a lot to understand the operation

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.