rated by 0 users
This post has 2 Replies | 2 Followers

Top 25 Contributor
Male
Posts 15
Ahsan Murshed Posted: 04-25-2010 6:23 PM

Problem:
Today I have faced a new problem(new for me), when I press save button it works fine and then refresh(F5 or Refresh button on browser) the page but this time it is execute the button click event again. For this duplicate data posting on the server.

I try to find out the problem, nothing wrong in my code. Now turn to google and figure out the problem with solution. I have got several solutions. Now I would like to share this solution which solved my problem ......

Solution:

Assume that in Test.aspx page you have a button and a TextBox:

<asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="110px" OnClick="btnSubmit_Click" />
<asp:TextBox ID="TxtName" runat="server"></asp:TextBox>


In Test.aspx.cs..:

public partial class Test : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
btnSubmit.Click += new EventHandler(btnSubmit_Click);base.OnInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{

}
private void Save_Click(object sender, EventArgs e)
{
if (CheckIfDuplicateRequest())

{
return;

}
//Your code to insert the value of the SomeText textbox into the database.
//Set the value of one of the key fields in your web form in session
Session["Refresh"] = TxtName.Text;

}

private Boolean CheckIfDuplicateRequest()
{
if (Session["Refresh"] == null)
{
//Probably submitting the page for the first time
return false;
}
String previousValue = Session["Refresh"].ToString();
if (previousValue != TxtName.Text)
{
//Submitting the page with a different set of values
return false;
}
//Duplicate request.
return true;
}

}


you can get more help from these post:

http://aspboss.blogspot.com/

http://aspalliance.com/687
http://forums.asp.net/p/1296011/3702515.aspx
http://forums.asp.net/p/1190997/2045619.aspx

Top 50 Contributor
Posts 4

Thank you for your nice post. I had been suffering for the same trouble and I solved by checking whether a value (Primary key or Unique key) is available in the table. It is very tedious. Thank tou for your easier solution.

Top 10 Contributor
Male
Posts 124

One simple solution was restart the page:

Example:

================================================

private void Save_Click(object sender, EventArgs e)

{   

    //Do your save work

    RestartPage();
}

 private void RestartPage()
        {
            Response.Redirect("Page URL");
        }

 

================================================

Sure you will loss your viewstate work. It would work fine when you are just saving data or something like that.

"Excuse if any gaffe."

Md Nazmul Ahsan
Programmer (.NET Framework)
CIS, IADCS & DIT

Page 1 of 1 (3 items) | RSS