asp.net backspace key press repeat previous postback - c#

I am new to .NET and can use some help. I have been working on a VB6 database client side frontend with mySql backend for years. Now I am moving to .NET 4.0 Web server side frontend with C#.
I just started on my first data entry page. I have a listbox for a Sql dataset from db. User click an item in the list to load the record tags into bunch of textboxes, ddls, gridview, and table for editing. Most things worked so far as I wanted, but I just found a weird thing. After loading a record from the list, I double clicked a textbox which is set to read-only and disabled, the content is highlighted, then I hit the backspace key (by accident actually), the previous postback action was performed (which is to load the records into the listbox and clear all controls for record tages).
How do I prevent this from happening? Thanks.

Related

asp.net web form in multiple browser tabs

I have a grid view and an edit button in the grid view. On edit button click I am opening a new aspx page that has text fields for input the data. When a user copies the URL of the gridview and opens it in a new tab of any browser then click on the edit button for two different records. If the user changes anything in the first tab and submits it. It changes the info for the record on the second tab. It is happening because I am passing userid in session to the form aspx page and session got updated when user opens the second record in the new tab.
Are there only two ways to passing data to aspx page?
using session
using a query string
I don't want to use the query string.
Please help thank you.
You are writing a ASP.Net application, so at the end of the day there is only that much you can do. You can request some things off the browser, but if he actually does it is entirely up to it.
You can make it unlikely to happen by accident, using the HTML Links target property. This requests the browser to re-use any alread open tabs for this record. But that will not prevent a dedicated person from still opening 2 copies.
A pretty simple way to avoid race conditions in general, is the SQL rowversion column. You retreive the rowversion with the rest. You keep it along in a hidden formular field (that is what they are there for). When writing the update, check if it still matches before the write. If yes, you update. If not, somebody has modified the record since then and you reject the update. Can be the same user in another tab, can be another user at the end of the world. Could be that this tab was opened a year ago, surviving on sleep mode. It does not mater - any change trips this protection.

Get previous webpage

I am at a loss as to how this is best handled in asp.net
I have a webpage with a lot of panels, and tables that is populated with data from mysql request. Now this data might change depending on an input. All the data are buttons that will open a modal windows displaying data.
My issue is that when I press the button the modal opens, but all the tables are reset to the standard "sql data" and the panels collapses. I basiclly want to be able to post back to the exact spot the user was earlier, including where on the page he had scrolled.
I get that the created html istanse is destroyed as soon as the person moved to the modal but how can i store the current exact state of the webpage so when the user goes back he is met with the exact "copy" he left. I've looked into viewstate and state management, and also to check for postback but still havent been able to solve it.
Is there any good tutorial covering this topic?
I've looked into this: Persisiting the state of a webpage
But thats based on ASP2.0 (im at 4.5) and dated 2006. Would this approach still be my best option or is this fixed in a better way on newer ASP.NET version?

asp.net dropdown displaying incorrectly in chrome

I have asp.net web forms page that has a asp.net dropdownlist control on it with an autopostback. When a user makes a selection in the drop down a get request is made to the same page with a query string parameter.
On postback I read the query string parameter and load / populate additional controls.
When a user presses the browsers back button the previous request is submitted (along with its query string). In the code behind I read the query string and load / populate additional controls, which works fine. I also try to select the appropriate item in the drop down (by value).
The issue I am seeing in the browser is that appropriate option text is not shown in the select. By inspecting the source I can see the the correct option has the selected="selected" attribute, so setting the selected item in the code behind was successful. But the text is incorrectly still the same as before the back button was pressed.
Curiously this only happens in chrome. ie and firefox both behave as expected.
Has anyone had any issues with asp.net drop down lists and chrome?
I'm stumped here.
I have created a simple example that demonstates this problem. src can be found at https://github.com/beembow/AspDotNetDropDownInChrome.git
Reproduce the error by selecting some values in drop down then pressing back button.
In which Page Event are you setting the selected value of the drop-down? It could be that you are setting it once the page is loaded, and that is why you don't see the dropdown set with the selected value.
They only fix I have found is similar to the one suggested ASP.NET - Google Chrome caching DropDownList selections.
I essentially add a global variable that holds the value of the option to select in the drop down. I do this via Page.RegisterStartupScript in the code behind.
I then have some js that looks for this global value, and if it finds it selects the appropriate option in the select via jquery.
This makes me think that the issue is something to do with how chrome treats some asp.net generated javascript / viewstate. It may also be something to do with chrome caching policies. curious that it works in both firefox and ie.
Either way, the solution described above is definitely an ugly hack. Would be interested in any solutions other people find.

Button click event won't fire on first click, but always fires on subsequent clicks

I have a large-scale asp.net project that I'm working on (new to ASP.NET), and I have a problem with some dynamically added buttons not firing their click events on the first click. What's weird is that this doesn't always happen; sometimes they work just fine on the first click, and other times if I run the project and follow exactly the same steps the button won't work on the first click.
Here's some background...
The page that's problematic is used to deploy different versions of some content to clients. The page has an UpdatePanel that contains a bunch of tables, and only one of them is visible at any given time. Each table represents a different "screen" of a wizard-like workflow. So when the page is loaded, the first table is visible and all others are not, and the first page has a radio button list with deployment types and a Continue button. When the Continue button is clicked, some data is loaded for the next "screen" and the first table is made invisible and the next table is made visible.
So now that you have some background, on to the problem...
One of these tables has an empty Accordion on it, and when the previous "screen"'s Continue button is clicked, the Accordion is populated dynamically with data from the DB; there's one pane for each major version and each pane has a table in it with one row for each minor version. Each of these rows has the Version Number, the Deployment Status, and an Action button that lets the user Deploy the version, or if its already deployed they can Redeploy. Anyways, these action buttons are the ones that I have a problem with where they sometimes don't work on the first click. Like I said, the buttons are created in the click event from the Continue button on the previous "screen", and I set the ID of each button, and link the event up to the click event at the time the button is created. I can debug over this and see the event handlers being added with no problem, but then when I click the button the first time (and this is actually not consistent; sometimes they work on the first time!) they don't do anything, but on the second click they work normally. I have a method called something like populateAccordion() that is responsible for actually loading the data from the DB and creating these records with buttons in them. I call this method on page_load if it's a postback, but only if the cached selections from previous "screens" are not null (i.e. I cache a subjectId and centerId on previous "screens" and only load the Accordion data if those are not null).
This is a little too complicated to post code for, but does anyone have any ideas what could be going wrong, or how I could figure this out? I've done quite a bit of debugging, and my populateAccordion() is always called properly on the previous screen's Continue button click, and on every page_load after that, and the Click events are always wired up right when the buttons are created in that method, so I can't figure out why the click events wouldn't fire on the first try.
The problem is similar to the one mentioned in this post...
https://stackoverflow.com/a/2784140/1246574
But I am definitely setting the IDs for all of my buttons, so the solution mentioned in that post isn't the fix for my problem.
I've also read quite a few other posts that say to move your code from Page_Load to Page_PreRender, but that doesn't change anything in this case; it acts exactly the same.
I also tried setting the buttons' UseSubmitBehavior = false, but no luck.
Thanks!
This is probably a latency issue. It sounds like you're able to click the controls before the page has fully loaded all the JS. Is there an onload() function in there? Perhaps you could try hiding the controls until the page has finished loading?

Update listbox in c# sql

Currently I created a wizard with a listbox. The listbox has a next and previous button. The listbox has thousands of items but the next and previous will show them 30 items at a time.
I am not using AJAX so the page reloads each time the button is being clicked. I don't want the page to reload everytime the user clicks next and previous. I thought of using AJAX.
Is there anyway I can prevent the page from reloading either by AJAX or a different way everytime the user presses next. Pressing next triggers a stored procedure which selects the next 30 items.
C#, SQL, ASP.NET, HTML, JavaScript
You can surely use jquery for this purpose. It will allow you to do an ajax request. In fact, ajax request can be done from a .Net page with even plain javascript and it has nothing to do with .Net version.
However, using jquery will make it easy and you will be able to easily populate the listbox using the results from server.

Categories