0

All I am trying to accomplish is to call a javascript function when a button is clicked in sharepoint. This is the extent of my 'code' in sharepoint designer 2007...

<%@ Page masterpagefile="~masterurl/default.master" language="C#" title="|" inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bxe2111e8529c" meta:webpartpageexpansion="full" meta:progid="SharePoint.WebPartPage.Document" %>
<asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderMain">
<script type="javascript">

function tellme() {
    alert('yep yep yep');
}

</script>
<p></p>
<asp:Button runat="server" Text="Button" id="Button1" onclientclick="tellme()" />
</asp:Content>

Can anyone tell me why the function is not called? When I save the page, view it and click the button, it just acts as a submit button. I'm perfectly happy with JS/HTML and PHP but I'm dabbling in SharePoint / .net and struggling slightly.

Thanks

Tom

1
  • Did you view browser's HTML source to see if something wrong? Commented Jun 1, 2011 at 17:31

3 Answers 3

1

The Button you've added in your Sharepoint page is a ASP.Net button. it's default behaviour is postback.

If you want to do something client side, use :

<input type="button" id="ClientSideBtn" value="Click ME" onclick="javascript:tellme()" />

If you want to do something server-side, use:

<asp:Button Text="Click ME" id="Button1" runat="server" onclick="Button1_Click"/>

For the server side button , you would need to write some c# or vb.net code:

    protected void Button1_Click(object sender, EventArgs e)
    {
        //Do something here

        //Such as
        this.Response.Redirect("http://www.google.com");
    }

You can read more here: http://support.microsoft.com/kb/306459

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

Comments

1

Change the line:

<asp:Button runat="server" Text="Button" id="Button1" onclientclick="tellme()" />

To instead read:

<asp:Button runat="server" Text="Button" id="Button1" onclientclick="tellme(); return false;" />

By returning false, you will prevent the PostBack.

Comments

0

use following code

    <%@ Page masterpagefile="~masterurl/default.master" language="C#" title="|" inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bxe2111e8529c" meta:webpartpageexpansion="full" meta:progid="SharePoint.WebPartPage.Document" %>
<asp:Content id="Content1" runat="server" contentplaceholderid="PlaceHolderMain">

<p></p>
<asp:Button runat="server" Text="Button" id="Button1" onclientclick="tellme()" />

<script type="text/javascript">

function tellme() {
    alert('yep yep yep');
}

</script>

</asp:Content>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.