Lock an ASP.NET Page if any fields change on it

667 views Asked by At

I want to lock an ASP.NET Page when a user tries to edit value of any control on that Page. If he is just viewing it should be just fine. The moment he edits any of the fields the page should be locked and other users should be still be able to view but not edit it. Is there any inbuilt and easier way to do this in ASP.NET 4.0? Is it something we should do using client side JQuery/Javascript?

Edit:- Here is the needed scenario, if a user opens the Page and make changes to any of the fields in that Page, it should be locked, which marks a flag(User ID) on the DB. When other user tries to edit or make changes to a page, its displays a message that this page is locked and disables the Submit button. but if other user is only viewing and not making any other changes, This should be Okay.

Kindly let me know. I found that Onchange event can be used to do this but I have a question as I am new to ASP.NET? Is onchange event available for controls? and what exactly is the difference between html controls and their ASP.NET Counter parts.

Thanks,

2

There are 2 answers

0
lukejkw On

If I understand you correctly, you will probably need a flag in the database to indicate whether the form is being edited and if so, by whom.

I think that it would be best to re-look what you are needing to do though and how you could achieve it another way. Dealing with concurrency is tricky but there must be better solutions.

Lastly, "what exactly is the difference between html controls and their ASP.NET Counter parts?"

ASP.NET server controls are basically written in ASP.NET markup and often bring in a whole lot of additional functionality over vanilla html elements. The most notable being the ability for them to be accessed server-side and retain state through something called ViewState. At run time, ASP.NET controls are rendered to vanilla html.

0
Hasan Nazeer On

As pointed out by Claies, web pages are by default stateless. If you really have to do something like this, you have to custom develop it. My immediate thoughts are IF you REALLY have to do this, and this is not a perfect solution:

Perhaps start by adding a few columns to table to keep track of the state of the page, Locked(true/false), User(UserID if you have it), LockDate(Current date/time)

Keep the controls enabled=false on the page by default. Put an Edit button on the page. In On-click enable the controls and in table, Set Locked = true, User id and save date and time so you know if someone is editing. When user Saves the page, unlock the page (Locked=false or null)

Keep in mind if user never finished and exits without saving, the page will be in a locked state. So you may have to consider putting a timer on the page and displaying a message to complete the edits, say 5 minutes. If user does not finished the edits, then change the Locked state of the page so others can edit.

Basically you have to develop your state management.