Dynamically update DropDownList with new value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • satyabhaskar
    New Member
    • Dec 2008
    • 32

    Dynamically update DropDownList with new value

    i am developing a web page in that i have a drop down list for Authors Name...which allows to select the Author available in the data source.and if the user wants a new author to insert into the authors table i had put a add new button beside the drop down list will allows the user to add the details of the new authors...so soon the user will select the add new button the Author page will be get open to enter Author details.....and now my requirement is soon the user click the insert button in Authors page...the Author details page should close and the newly entered Author name in the Author page should be added into drop down list of Books page with out refreshing.. can any one plz solve my problem plz. im developing my web page using asp.in with c#....
    and im using following JS in .cs file
    this.RegisterCl ientScriptBlock ("newAuthor" , "<script language='JavaS cript'> newAuthors('"+d dlAuthors.DataT extField+"','"+ ddlAuthors.Data ValueField+"',' "+ddlAuthors.Cl ientID+"')</script>");

    hope i will get a quick respone

    thank u...
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You can use JavaScript to add the author to the DropDownList but you may run into problems because the ViewState won't match with what was sent to the browser. For an idea of what's involved to get this to work, check out this thread...I know it deals with ListBoxes but the same concept will apply to your DropDownList.

    It would be easier to just make an asynchronous call to the server to refresh just the DropDownList instead of the whole page.

    Comment

    • satyabhaskar
      New Member
      • Dec 2008
      • 32

      #3
      hi boos,
      Thanks for reply, iam in idea to write the same code for java script in authors page...but the drop down list is in books page ... how can i get the id of the drop down list which is in Books page into the Authors page...
      will u plz make me clear in this ...
      thanks

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I'm assuming that you are using popups. The key to this is to use the JavaScript window.opener to retrieve a reference to the Parent window. You have to set the name of the parent window before opening the popup window so that the window.opener will be set.

        So, make a JavaScript function that opens the popup window but names the parent window before opening it. Also, create a function that updates the list that accepts a parameter with the value to add to it.

        Call the function that opens the child window when you click the Add button...and when you hit "update" in the child window, have the update button refer to the windows.opener to call the JavaScript function that updates the list....passing it the value to add to the list.

        Look up how to use window.opener to achieve what you're trying to accomplish.

        -Frinny

        Comment

        • satyabhaskar
          New Member
          • Dec 2008
          • 32

          #5
          hey thanks boss... ur reply helped me a lot... i solved my problem by following ur reply....nw i got two problems one is linked to the previous problem..
          1. soon im updating the Authors details if i press ok button available in that form i will be going back to books details.and nw the problem is im not able to display the authors name in dropdown list as my first choice visible which i added in the authors form. can i get the information or sample example for this problem...
          i tried even setting selectedIndex=1 and 0 but its not solved..

          2. i have some number of text boxes in my page and even a button... soon i enter text in first text box and press enter it actually fires the button...but i dont to be like that...my need is soon i press enter the text in the text box and press enter i want to focus on the cursor on the next text box so that user can type the info in next text box(similar like tab key)...so actually i want navigation to carriage return..can u plz make me clear even in this doubt..by giving some relevant example.. so that i can take further steps in my project...

          thanks

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by satyabhaskar
            1. soon im updating the Authors details if i press ok button available in that form i will be going back to books details.and nw the problem is im not able to display the authors name in dropdown list as my first choice visible which i added in the authors form. can i get the information or sample example for this problem...
            i tried even setting selectedIndex=1 and 0 but its not solved..
            In JavaScript you cannot set the DropDownList.Se lectedIndex=1.
            The reason you can't do this is because a DropDownList is an ASP.NET Object...not a JavaScript Object. ASP.NET transforms DropDownLists into HTML in order to let the browser know how to display them. The HTML generated for a DropDownList is a <select>.

            Since you aren't posting back to the server to process this information using .NET, and you are certain that you want to use JavaScript to implement your solution, I have moved your question to the JavaScript Forum where JavaScript experts can help you. (It would be very challenging for any .NET expert to help you with this)


            Originally posted by satyabhaskar
            2. i have some number of text boxes in my page and even a button... soon i enter text in first text box and press enter it actually fires the button...but i dont to be like that...my need is soon i press enter the text in the text box and press enter i want to focus on the cursor on the next text box so that user can type the info in next text box(similar like tab key)...so actually i want navigation to carriage return..can u plz make me clear even in this doubt..by giving some relevant example.. so that i can take further steps in my project...
            This is a new question.
            You should post this question in a new thread in the JavaScript Forum. I would recommend that you research and attempt this before asking your question because there is a lot of information and tutorials on the web about how to do this.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by satyabhaskar
              1. soon im updating the Authors details if i press ok button available in that form i will be going back to books details.and nw the problem is im not able to display the authors name in dropdown list as my first choice visible which i added in the authors form. can i get the information or sample example for this problem...
              i tried even setting selectedIndex=1 and 0 but its not solved..
              Get the client-side generated ID and use document.getEle mentById() to refer to the element and then set it's selectedIndex property.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Originally posted by acoder
                Get the client-side generated ID and use document.getEle mentById() to refer to the element and then set it's selectedIndex property.

                Since this ID is not dynamically generated (because you're updating the list using JavaScript) you should know what it is .

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  To avoid confusion and see exactly what the problem is, satyabaskhar, post your code.

                  Comment

                  Working...