By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

Dynamically update DropDownList with new value

P: 32
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.RegisterClientScriptBlock("newAuthor", "<script language='JavaScript'> newAuthors('"+ddlAuthors.DataTextField+"','"+ddlAu thors.DataValueField+"','"+ddlAuthors.ClientID+"') </script>");

hope i will get a quick respone

thank u...
Jan 5 '09 #1
Share this Question
Share on Google+
8 Replies


Frinavale
Expert Mod 5K+
P: 9,731
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.
Jan 5 '09 #2

P: 32
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
Jan 6 '09 #3

Frinavale
Expert Mod 5K+
P: 9,731
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
Jan 6 '09 #4

P: 32
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
Jan 7 '09 #5

Frinavale
Expert Mod 5K+
P: 9,731
@satyabhaskar
In JavaScript you cannot set the DropDownList.SelectedIndex=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)


@satyabhaskar
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.
Jan 7 '09 #6

acoder
Expert Mod 15k+
P: 16,027
@satyabhaskar
Get the client-side generated ID and use document.getElementById() to refer to the element and then set it's selectedIndex property.
Jan 9 '09 #7

Frinavale
Expert Mod 5K+
P: 9,731
@acoder

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

acoder
Expert Mod 15k+
P: 16,027
To avoid confusion and see exactly what the problem is, satyabaskhar, post your code.
Jan 10 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.