What's Problem?
-
Hi! I'm doing a Web page in which Users can post. -In Page_load , I did : If (!Page.IsPostBack) { Load_Data(); } -In button Post Click Event: If !CheckEmptyControl() { ...... //WriteToDB; } Post Successed! But After Post_Click Event if I click Refresh button in Menu bar of IE. The Post_Click event is re-called, and the result is there's two the same data in DB. How can i fix it?
-
Hi! I'm doing a Web page in which Users can post. -In Page_load , I did : If (!Page.IsPostBack) { Load_Data(); } -In button Post Click Event: If !CheckEmptyControl() { ...... //WriteToDB; } Post Successed! But After Post_Click Event if I click Refresh button in Menu bar of IE. The Post_Click event is re-called, and the result is there's two the same data in DB. How can i fix it?
Anonymous wrote: But After Post_Click Event if I click Refresh button in Menu bar of IE. The Post_Click event is re-called, and the result is there's two the same data in DB The reason is that the client browser submits the same information to the server on the refresh as it did to generate the page. When a page request arrives at the server it treats it as if it is the only request it has ever received. ASP.NET uses some clever abstraction to make it look to you and the user that it is one seamless application. However, there are certain instances where the abstraction breaks down. Anonymous wrote: How can i fix it? So, you must make your database interaction check that the record does not already exist. If it does exist then it does nothing, if it doesn't exist then it inserts a new record.
Do you want to know more? WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums
Upcoming talk: SELECT UserName, Password FROM Users -- Getting unauthorised access to a SQL Server, and how to prevent it.
-
Hi! I'm doing a Web page in which Users can post. -In Page_load , I did : If (!Page.IsPostBack) { Load_Data(); } -In button Post Click Event: If !CheckEmptyControl() { ...... //WriteToDB; } Post Successed! But After Post_Click Event if I click Refresh button in Menu bar of IE. The Post_Click event is re-called, and the result is there's two the same data in DB. How can i fix it?
Hi, This is actually one of the most common mistakes i see, but dont worry that also meens that the solution is pretty straight forward. The problem... Page_load() is the FIRST event to be triggered. so you have a situation where your code behaves corectly but just not the way you intended. I guess that you want to have some code execute after your button event was triggered. there are a few methods, but i like to use the PreRender event. the PreRender event is the last event to be fired before the HTML is rendered and send to the client. to use: In the InitializeComponent function add: this.PreRender += new System.EventHandler (this.Page_PreRender); and create a private function: private void Page_PreRender(object sender, System.EventArgs e) { Load_Data(); } thats all there is to it.
-
Hi! I'm doing a Web page in which Users can post. -In Page_load , I did : If (!Page.IsPostBack) { Load_Data(); } -In button Post Click Event: If !CheckEmptyControl() { ...... //WriteToDB; } Post Successed! But After Post_Click Event if I click Refresh button in Menu bar of IE. The Post_Click event is re-called, and the result is there's two the same data in DB. How can i fix it?
When refreshing the page the browser asks whether you want to resend the page again. If we accept, then it sends the data again. It is equivalent to clicking the post button again. What you will do if click the post button with the same data? If you allow it, the refresh is also doing the same thing. Still if you dont want it do a backend check for the uniqueness and simply display a message that the data already present.