Question

I'm new at Jquery and im trying to display a div on the LinkButton ClickEvent (If I use a button instead of a linkbutton it will work)

This is my Jquery code

<script type="text/javascript">
            $(document).ready(function () {
                $('#<%=lbLog.ClientID%>').click(function () {

                    $("#login").show(2000);
                    alert("hello");
                });
            });
        </script>

The hello message is displayed by not the div

and this is my html code:

<asp:LinkButton ID="lbLog" runat="server" onclick="lbLog_Click">Login</asp:LinkButton>
        <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
        <div id="login" style="display:none">            
            Username: <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="rfvUsername" runat="server" ControlToValidate="txtUsername" Display="Dynamic" ErrorMessage="*" ValidationGroup="LogGroup">*</asp:RequiredFieldValidator>
            Password: <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox> <asp:RequiredFieldValidator ID="rfvPassword" runat="server" ControlToValidate="txtPassword" Display="Dynamic" ErrorMessage="*" ValidationGroup="LogGroup">*</asp:RequiredFieldValidator>
            <asp:Button ID="btnLog" runat="server" Text="Login" onclick="btnLog_Click" ValidationGroup="LogGroup" />
        </div>
Was it helpful?

Solution 2

The ASP.NET link button will cause a postback which will refresh the page and hide the div thanks to style="display:none" no matter how you change your jQuery function. Try this instead:

<script type="text/javascript">
    $(document).ready(function () {
        $("#fakeLink").click(function () {
            $("#login").show(2000);
        });
    });
</script>
<style type="text/css">
    .fakeLink
    {
        color: blue;
        text-decoration: underline;
        cursor: pointer;
    }
</style>

   <div>
    <span class="fakeLink" id="fakeLink">Login</span>
    <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
    <div id="login" style="display:none;">
        Username:
        <asp:TextBox ID="txtUsername" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvUsername" runat="server" ControlToValidate="txtUsername"
            Display="Dynamic" ErrorMessage="*" ValidationGroup="LogGroup">*</asp:RequiredFieldValidator>
        Password:
        <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvPassword" runat="server" ControlToValidate="txtPassword"
            Display="Dynamic" ErrorMessage="*" ValidationGroup="LogGroup">*</asp:RequiredFieldValidator>
        <asp:Button ID="btnLog" runat="server" Text="Login" OnClick="btnLog_Click" ValidationGroup="LogGroup" />
    </div>
</div>

OTHER TIPS

try this

$("#login").css('display','block');

You don't need onclick="lbLog_Click". The jquery selector for the ID looks incorrect '#<%=lbLog.ClientID%>'. View the source and make sure the id on the linkbutton matches what your passing to the selector in your javascript code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top