Hey,
I hope someone can quickly tell me what I am obviously missing for this weird problem.
To give a general picture, I have an ASP.net webpage that allows users to select values from several dropdown menus and click an add button which formats and concatenates the items together into a listbox. After the listbox has been populated the users have the option to save the items via a save button.
The save button parses each item in the listbox to basically de-code the concantenated values and subsequently inserts them into a table residing on a backend MSSQL 2005 database.
PROBLEM:
In the process of testing the application, I noted this strange behavior. If I use the webpage to insert the values, go to the table where the values are stored and delete the rows; Upon a refresh of the web page the same actions seem to be getting replayed and the items are again inserted into the table.
Naturally, what I'd really like would be for the page to refresh and show that the items aren't any longer there and not the other way around.
If the code that performed the insert was residing in a component that was set for postback I'd expect this type of behavior but its in the Save buttons on_click event. I have tried practically everything in effort of targeting the problem but not having much luck with it.
Is this behavior practical and expected in ASP.net or has anyone ever heard of anything similar? I have never encountered this type of problem before and was hoping someone could provide some clues for resolving it. If more information is required I'd be happy to supply it. Hopefully, there's a simple explanation that I am simply unaware since I haven't experienced anything like this before.
Anybody got any ideas?
Thanks.
Do you know about the page method IsPostback?
that is,
in page_load (or whatever)
if (!Page.IsPostBack)
{
}
|||Hi Peter,
Yes, I am familiar with the IsPostBack method and I realize that any code placed inside of the if (!Page.IsPostBack) {} will typically only be executed upon the initial load of the page if that's where the condition is placed. I explored the possibility of overlooking that somewhere in my code and also experimented by placing that condition a various points in the code starting with the obvious, "the save_onClick event" but results are either the data doesn't get inserted into the database or it will insert the data and persist manipulation of the data in the database.
As I presently have it setup, on the btnSave_OnClick event; the data is inserted into the database table by looping through all the values in one listbox, then that listbox is cleared of all items and another listbox is populated with the inserted data variables through an active connection to the database and a select all statement. The intent was to allow the users to see the data they just inserted into the table. I suppose I could remove this feature and resolve the problem but I'd much rather identify why its happening and come up with a more viable solution.
I am open to any alternative solutions as well if the outcome will produce the desired results.
Any other thoughts?
Thanks for trying Peter.
I have managed to target the specific problem now. Debugging the app shows me the issue is that (refreshing or reloading) the page, is causing the on_click event of the save button to be fired. So it appears I was right on target with the description of the problem as the inserts are actually being replayed to the database upon a refresh of the page.
So what I need to determine is what can be done to prevent previous events of the button from being retriggered on a refresh but allowed on a normal postback (if there is any such animal).
Obviously implementing the !Page.IsPostback method in the block of code for the button_click event won't work because the records would never get inserted and I can't place the button_click event inside of the page_prerender event either.
There has got to be a simple method of preventing this from happening. Maybe some way to programmatically distinguish a page postback from a page reload would be an option?
Anyway, I'll keep posting in other forums to see if I can get some additional feedback. I simply can't believe that this type of thing is only new to me.
Thanks again.
Sure.
I am using a Standard Button, a Listbox, and C# with ASP.net. So far I have examined other methods that may give me what I need in terms of behavior including enableViewState and a few others but I can't seem to control the _Click event from replaying the previous action upon reload of the page.
1//Save button iterates through the listbox and acquires the data added by the user2protected void btnSave_Click(object sender, EventArgs e)3 {4 String adUserInsert ="";56try7 {8for (x = 0; x < lstUser.Items.Count; x++)9 {10 adUserInsert = lstUser.Items[x].Text.ToString();11 insertRecords(adUserInsert);12 lstInsertedUser.Items.Add(adUserInsert);13 }14 }15catch16 {17//Error Trap18 }19 }2021//This function inserts the record into the database22private void insertRecords(String adUserInsert)23 {2425try26 {27 String sqlConnString ="Provider=SQLNCLI;Server=TESTSERV;Database=Users_db;Uid=sa;Pwd=p@.sswrd25;";28 String sqlQuery ="INSERT INTO Users_db VALUES('" + adUserInsert +"')";29 OleDbConnection sqlConn =new OleDbConnection(sqlConnString);30 OleDbCommand sqlCommand =new OleDbCommand(sqlQuery, sqlConn);31 sqlConn.Open();32 sqlCommand.ExecuteNonQuery();33//Close the DB connection after use34 sqlConn.Close();35 }36catch37 {38//Error Trap39 }40 }
No comments:
Post a Comment