Question

Hi that's a simple code that I saw in my asp.net course references...

I don't understand something in the property onClientClick:

onClientClick = "myClick(); return false;"

Why in this case (or eventually other cases) it's important to finish the JavasScript calls with the "return false;" after the myClick() function?

What's the logic behind that?

Thx Guys!

<script type = "text/javascript">

    function myClick() {
        alert("Hello");
    }  
</script>



<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" Height="202px" Width="349px">
            <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" Text="Click me" onClientClick = "myClick(); return false;" />
                </ItemTemplate> 
            </asp:TemplateField> 
                <asp:BoundField DataField="Id" HeaderText="Id"  />
                <asp:BoundField DataField="Family" HeaderText="Family"  />
                <asp:BoundField DataField="Name" HeaderText="Name"  />
            </Columns>
        </asp:GridView>

    </div>
    </form>
</body>
</html>
Was it helpful?

Solution

It prevents the default action of the link from taking place, which would most likely be a quick post back. This can also be accomplished with preventDefault (https://developer.mozilla.org/en-US/docs/Web/API/event.preventDefault)

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