How to prevent data insertion in a sharepoint web part on page refresh

Posted: January 26, 2009 in Development, SharePoint
Tags: , ,

Today I was trying to develop a web part to use as “Contact Us” . I think it is a good sample in CodeProject site . but there was something wrong after I deployed  it. Every time I hit F5 or Refresh button on the browser,  the button on the web part fired again and so another email was sent to me.

I searched a lot and found my solution to overcome this issue.

the point was that first I used a Page object as my current page.

Page currentPage;

then in the CreateChildControls() method :

currentPage = this.Page;
if ( !currentPage.IsPostBack )
{
currentPage.Session[“Firstload“] = 1;
}

then in my SendMessage button event Hadler I write the code like this :

protected void btn_SendMessage_Click(object sender, EventArgs e)
{
if (int.Parse(currentPage.Session[“load”].ToString()) == 1)

{

//send message codes
currentPage.Session[“Firstload”] = 2; // the last command of the event handler
}

}

hope this help.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s