0

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>
Eliza
  • 135
  • 1
  • 4
  • 15

3 Answers3

1

try this

$("#login").css('display','block');
bipen
  • 36,319
  • 9
  • 49
  • 62
0

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.

Kyle Nunery
  • 2,048
  • 19
  • 23
  • this is wrong....if such was the case then.. how will he get a hello message.. i wonder..:) – bipen Jan 05 '13 at 18:30
0

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>
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
  • It worked Thank you, but I need to change the Log in to Log out, that's why I was try to use the link button because the link button change be changed through c# code – Eliza Jan 05 '13 at 19:38