Wednesday, March 28, 2012

Why just refresh on an autopostback button that was clicked always execute the event handl

I am still quite new in ASP.net and I am creating just a simple update to a database together with a display. I have a submit button (autopostback) and a description text box on one side and a display on of all the description on the DB on the other. They are all on the same page. This is how it works. Whenever you click the submit button, the description will be save to the database and then it will display it to the display side. My problem is after I clicked the button and then I press F5 to refresh the screen, the refresh will run the submit event handler although I only refreshed the screen. Somehow, it still remembers the last operation that I have done and it is doing that again. I don't want this to happen. Whenever I press F5, I want just to refresh everything. Although when I press F5, it prompts me saying that all information will be send again to the server, retry or cancel. When I press retry, that's the time the event handler is run again.

Thanks in advance for the response.Where is your event handler code (i.e. Page_Load, subroutine, etc.)? Could you post a small sample?
Hi,

Here is a sample of my code. Whenever, I clicked on the add entry button, then I pressed refresh to refresh the screen, my program always run the addentry_click event handler so my database is getting filled up although I don't want to do that during the refresh.

Thanks,

override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.AddEntry.Click += new System.EventHandler(this.AddEntry_Click);
}

//*******************************************************
//
// AddEntry_Click server event handler on this page is used to
// saves a time entry and adds it to the system.
//
//*******************************************************

protected void AddEntry_Click(object sender, System.EventArgs e)
{
// check validation.
if (Page.IsPostBack)
{
ExpiryDateRFV.Validate();
DepartmentListRFV.Validate();

// proceeds if data is valid then create an obejct and save it.
if ( (ExpiryDateRFV.IsValid) && (DepartmentListRFV.IsValid) )
{
ChangeManagement.Announcement announcement = new ChangeManagement.Announcement();
int iResult = announcement.AddEntry();

if (iResult == Constants.SUCCESS)
{
ClearEntryFields();
}
}
}
}
This may be wrong due to the fact I'm not familiar with C#. I use VB.NET myself.

But here it goes...In your AddEntry_Click subroutine you have specified the following:

IF (Page.IsPostBack)

I believe this means that the code gets executed everytime the page is posted back. Try using IF NOT(Page.IsPostBack). I'm not exectly sure of the syntax for C# but you get the idea. This should prevent the code from executing on every post back.

Let me know how you make out.
Hi Todd,

It works. Thanks.

0 comments:

Post a Comment