dcsimg
Results 1 to 7 of 7

Thread: Problem with Dropdown list on selection change

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Problem with Dropdown list on selection change

    Good morning,

    I am having an issue where I am creating a form and on page load (when it isn't posting back), I fill two drop down lists with data from a table in my database, one for departments and the other for organizations.

    I fill out the form and select a value from the first drop down list. When I do that, I get the postback flash, and it resets the value for the drop down list back to the default value that was originally set on the first page load. The next time I select it, it works without issue.

    Is there a setting I could be missing somewhere that is causing this value to not be detected/set correctly the first time around?

    If you need some portions of code to help determine, I can provide some of that but it seems like I might be missing a property/setting that interferes with the form posting back correctly?

    Thanks!

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Re: Problem with Dropdown list on selection change

    A bit more info with some things I was testing...

    In the page load event, I set the value of the dropdown list to index 3. On the first page load, this worked correctly. The dropdown list correctly selects the value that I want at index 3 and sets the selectedindex to 3.

    However, if i select any other value (the first time I am selecting only), it flips back to index 3. It is like it isn't detecting that value change or maintaining state on that first selection change.

    Any time after that, it works.

  3. #3
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    773

    Re: Problem with Dropdown list on selection change

    Probably an issue where you aren't properly handling IsPostBack state, but impossible to say without seeing your code.

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Re: Problem with Dropdown list on selection change

    Sure. I should be able to post some code, depending on the code. Something specific?

    I guess I would ask...should I be putting this in the page load event or should I put that somewhere else as far as the filling of the drop down lists?

  5. #5
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    773

    Re: Problem with Dropdown list on selection change

    Post the Page Load code.

  6. #6
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,542

    Re: Problem with Dropdown list on selection change

    I fill out the form and select a value from the first drop down list. When I do that, I get the postback flash, and it resets the value for the drop down list back to the default value that was originally set on the first page load. The next time I select it, it works without issue.
    As your using old style web forms, whenever your page does a postback it completely reloads the page, this is what is resetting your initial dropdown list.

    There are a number of ways around this, but what is probably easiest is just to store your initial dropdown choice in a session variable, check in your pageload event if your doing a postback

    Code:
    If IsPostBack then 
        // set your initial dropdown to your saved selected index here
    end if
    and then set you dropdown selectedIndex to your saved value

    Personally i think you should avoid web forms altogether and look at more modern design patterns like MVC which eliminates postbacks but if your just learning it will do no harm to understand web forms first.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Re: Problem with Dropdown list on selection change

    Hi, no, I am not just starting out but I am coming back to it, I guess you would say.

    With my job, coding is a small portion and it has probably been 2-3 years since I have done coding.

    I was able to get this to work correctly by coding the dropdownlist as opposed to dragging and dropping it on the form but I still don't know why it is working when I manually code it as opposed to dragging and dropping. It might be a property that is being set or something else.

    I am certainly open to using MVC but I have never looked at it and unfortunately I don't have a lot of time to dedicate at this point. If there is a resource that will quickly get me up and running, I would appreciate if you could link to it. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width