Every time I refresh Internet Browser the ASP.net button's click event fires. SharePoint WebPart
-
Friday, April 13, 2012 10:16 AM
Hi All,
I have a simple SharePoint WebPart that creates a Site Collection when a button is pressed. The Simple WebPart works OK when the button is pressed for the first time, but when the page is refreshed the button resubmits the button event. Because the Site collection has already been created it will always generate an error message:
**********Error********
Message: Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown. Detail: Another site already exists at http://dev/sites/WSTest. Delete this site before attempting to create a new site with the same URL, choose a new URL, or create a new inclusion at the...
**********************
The simple code I am using is shown below:************ProjectWebBuilderUserControl.ascx.cs****************
using System;using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
namespace WebService_CreateSiteCollection.WebServiceCreateSiteCollectionVWP
{
public partial class WebServiceCreateSiteCollectionVWPUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Administration.Admin admService = new Administration.Admin();
admService.Credentials = System.Net.CredentialCache.DefaultCredentials;
try
{
SPSecurity.RunWithElevatedPrivileges
(delegate
{
admService.CreateSite("http://dev/sites/WSTest",
"Title", "Description", 1033, "STS#0",
"Contoso\\Adamb", "Adam Bird",
"Adamb@Contoso.com", "", "");
}
);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
Label1.Text = "Message:\n" + ex.Message + "\nDetail:\n" +
ex.Detail.InnerText +
"\nStackTrace:\n" + ex.StackTrace;
}
}
}
}
************ProjectWebBuilderUserControl.ascx****************
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %><%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebServiceCreateSiteCollectionVWPUserControl.ascx.cs" Inherits="WebService_CreateSiteCollection.WebServiceCreateSiteCollectionVWP.WebServiceCreateSiteCollectionVWPUserControl" %>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" Width="167px" />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
*********************
I have tried surrounding the button event code with ”if (!Page.IsPostBack)” or “if (!IsPostBack)” and it still doesn’t work.
I am fairly new to ASP.Net and SharePoint coding so I am sorry if I am do something obviously wrong.
I hope you can help
CEStar
All Replies
-
Friday, April 13, 2012 10:26 AM
What happens there is that the refresh triggers the post back not just the page load -- so the event is triggered. A simple solution would be to redirect the user to a fresh new page after handling the event.
This way if the page is reloaded it's a new page, not the post back. Thus under the add event, do the redirection on same page.
Regards, Dharnendra Shah "strong belief is the only way to success"
-
Friday, April 13, 2012 10:44 AM
In this simple example I don't want to redirect to another page I want to stay on the same page. I will disable the button after it has been pressed, and also add data validation once I have got over this postback and refresh web page hurdle.
Apart from your simple solution, is there a code solution like ”if (!Page.IsPostBack)” or “if (!IsPostBack)” that I can use?
- Edited by CEStar Friday, April 13, 2012 10:46 AM
-
Friday, April 13, 2012 10:53 AM
Use the same page, do not redirect it to another page. In a page life cycle, every refresh of the page, it will call the last executed action and thus it's calling button click event. No any post-back code would do any resolution in such case.In this simple example I don't want to redirect to another page I want to stay on the same page
Regards, Dharnendra Shah "strong belief is the only way to success"
- Marked As Answer by Qiao WeiMicrosoft Contingent Staff, Moderator Thursday, April 19, 2012 9:48 AM
-
Friday, April 13, 2012 10:54 AM
CEStar,
If you have a button on a page and it performs an action, then returns the user to the same page, you really can't stop the user from hitting F5 and reposting the page. Page.IsPostBack will always be true on refresh AND on first press.
You really should be checking if the site colelciton exists: http://nikspatel.wordpress.com/2011/11/30/code-snippet-how-to-check-if-sharepoint-site-collection-or-sub-site-exists/
Maybe try to follow how SharePoint handles click events and page redirections.
Independant SharePoint Consultant. Feel free to contact me. Blog: http://www.sharepoint.bg/radi Twitter: @RadiAtanassov
- Marked As Answer by Qiao WeiMicrosoft Contingent Staff, Moderator Thursday, April 19, 2012 9:48 AM

