Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3498
  • Last Modified:

Page refresh triggering DropDownList's SelectedIndexChanged event every time

I have a dropdownlist and a method to handle its selectedIndexChanged event.  When I change the dropdown's selected index and click on a submit button, the selectedIndexChanged event fires as expected.  If I click the button a second time, the event doesn't fire.  Again, as expected.  However, if after clicking the submit button for the first time, I then refresh the page using the browser's refresh button, the event is triggered again... despite the fact that I haven't changed the selectedIndex since clicking the submit button.  I can continue to click refresh and the event will continue firing even though I'm not touching the dropdown at all.  I set up an extremely basic test using just one dropdownlist and one button, so I'm pretty sure it's not a bug being caused by something else.

private void Page_Load(object sender, System.EventArgs e)
{
      if ( !IsPostBack ) {
           ddl.Items.Add( "l" );
           ddl.Items.Add( "2" );
      }
}

protected void indexChanged(object sender, EventArgs e)
{
      Response.Write( "event triggered" );
}

and my aspx page has:
<asp:DropDownList id="ddl" Runat="server" OnSelectedIndexChanged="indexChanged"></asp:DropDownList>

Any insight?
0
smooga
Asked:
smooga
  • 5
  • 3
  • 2
  • +1
1 Solution
 
dfaithenCommented:
I believe this is due to that fact that when you refresh a page it is posting back all the same state that it did the first button click. example
when the page first loads it selects item 1
you select item 2 and click submit
the app looks at the state of the dropdown when you post back
it sees old selected item is item 1 new selected value is item 2
when you refresh it loads the page exactly the same
it sees old selected item is item 1 new selected value is item 2
refresh
it sees old selected item is item 1 new selected value is item 2
refresh
it sees old selected item is item 1 new selected value is item 2
even if you change the selection and then hit refresh it will set it back and see
it sees old selected item is item 1 new selected value is item 2
 I could be wrong, but I believe this should explain your phenomenon.
Duane


0
 
RejojohnyCommented:
the behavour is normal and as expected .. when u refresh a page and if the previous action had submitted the page, u will be promopted to whether u want to submit again with a retry and cancel button .. if u press retry, the page gets submtted again .. as if u had clicked the same button again .. the state of the page remains the same and so the selectedindex gets fired
0
 
RejojohnyCommented:
another option would be .. in the button click event .. instead of the page showing itself again .. u could redirect to the same page .. so the browser thinks it as a new page and so if u press refresh, it will not submit the page again .. the downside been that ur viewstate will no longer be valid and so the textbox will not get filled with the last state .. this solution is applicable .. if u have a form and the button u have clicked is a save button .. so save will save the changes to database and then redirect will load the forma again .. so the page load will take care of fetching the required data from the database which would be the latest information ...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
smoogaAuthor Commented:
I need the viewstate, so I can't redirect... unfortunately.  Is there any way to programatically set the "old selectedindex" to the "new selectedindex".  Or any way to trick it?  I'm inserting data into a database based on the changed dropdowns.  Seems silly that a user can add multiple entries just by hitting refresh.  Is there a way to detect if the page is being reloaded from a browser refresh button vs. a submit button?  I know I can hook up a ButtonClick event, but that ALSO gets refired on the page refresh!  So anytime a user clicks refresh, it's as if they've changed all the same dropdowns and clicked submit again.  There must be a prefered way around this, no?
0
 
dfaithenCommented:
If I could suggest to step out of the problem and look at it. How offen will this actually be an issue. What are the realistic chances that a user would hit refresh on this page and if they did, would it actually have a destructive effect on the app or your data integrity. being that it would essentially just rerun the same processes with the same state and the same values across the board.
 ie. if I am bob and I select red from my favorite color dropdown. The screen label is assembled to say
if I click refresh it will just reprocess with the same info. the end result...
"Bobs favorite color is red"
You said "Seems silly that a user can add multiple entries just by hitting refresh", can you give a few details as to the context of your page?

Duane
0
 
smoogaAuthor Commented:
I'm executing Inserts on the database.  If it was just an Update it wouldn't be a problem - the same data would just be written - but I'm actually inserting new data into the database.
0
 
dfaithenCommented:
You should probably be doing a check to see if the record already exists anyways. (usually at least)
Duane
0
 
smoogaAuthor Commented:
I'd just prefer not to make calls out to the database when I already know I don't want the data inserted.
0
 
avikatz16Commented:
question: on page load (or when you refresh) do you have it programmed so the same item in the ddl stays? or does refreshing the page (or firing pageload again) change your item in the ddl?

if it changes, try this code (VB.NET):
 
 '// This puts back the ddl item that the
        '// user chose back into the ddl
        If Me.IsPostBack = True Then
            Dim liType As ListItem
            Dim intNum As Integer
            liType = Me.ddlCategory.Items.FindByText(Request.Form("ddlCategory"))
            intNum = Me.ddlCategory.Items.IndexOf(liType)
            Me.ddlCategory.SelectedIndex = intNum
        End If

0
 
avikatz16Commented:
the above close will set the SelectedIndex property of the DDL back to what the user chose before submitting.
It will display the same item after submitting
0
 
smoogaAuthor Commented:
No, the selected index stays the same.
0
 
smoogaAuthor Commented:
Found this...

http://www.codeproject.com/useritems/SyncControl.asp

Will try it out when I get to work tomorrow.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now