Firing javascript on button click sharepoint
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" contentplaceho开发者_运维技巧lderid="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
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
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.
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>
精彩评论