473,382 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Can not correctly select item in a DropDownList after postback

2
Using ASP.NET and IE7. (Sorry if I am posting in the wrong forum but my problem seemed to be more related to the JavaScript side than the ASP.NET side.)

I have two DropDownList controls the second of which resides within an UpdatePanel control which responds to a change in the first DropDownList. I also have a hidden button nested inside the UpdatePanel to force a postback via JS.

The user uses these two DropDownList controls to create objects which consist of a type and a code. When the user selects a type in the first DropDownList the second is properly populated with the relevant codes via a postback to the server. Once the user has selected a type and code they can add the object to a table via an 'add' button. The DropDownList controls are then reset to allow the user to add more objects to the table.

So far, so good.

The user can also click on a row in the table (which displays the type and code for a single object) to edit the type and/or code for the object. When the click occurs the DropDownLists should get set to the type and code for the selected object.

This is where the problem arises. The first DropDownList does get set to the correct type for the object. The second DropDownList does get properly populated with the correct codes for the type but the correct code for the object never remains selected. The first time the row is selected the second DropDownList just gets populated. On each subsequent click you can see the correct code get selected for a fraction of a second but the DropDownList almost immediately gets reset to the first item in the list.

I noticed if I raise an alert before the line that selects the code the correct code is selected and remains selected. I assumed that maybe the call to select the code was being called before the control was completely populated. I tried adding a wait (a simple for loop) before the call to select the code but I either ended up with the same results as above or a dialog would appear asking the user to terminate the script.

I tried querying the readyState property of the DropDownList and would only set the code when the readyState == 'complete' and that had no effect.

I tried omitting the hidden button and using __doPostBack to force the postback via JavaScript but that was causing the whole page to refresh which was unacceptable.

Could someone please let me know what is going on? Thanks!



In my .aspx file:


Expand|Select|Wrap|Line Numbers
  1.  
  2. <asp:DropDownList ID=ddlInsertionTypes AutoPostBack=true OnSelectedIndexChanged=ddlInsertionTypes_SelectedIndexChanged runat=server />
  3.  
  4. <asp:UpdatePanel ID=UpdatePanel1 ChildrenAsTriggers=false UpdateMode=Conditional runat=server>
  5.     <ContentTemplate>
  6.         <asp:DropDownList ID=ddlInsertionCodes AutoPostBack=True OnSelectedIndexChanged=ddlInsertionCodes_SelectedIndexChanged runat=server /> 
  7.         <asp:Button ID=cmdForcePostBack1 style="display: none;" runat=server />
  8.     </ContentTemplate>
  9.     <Triggers>
  10.         <asp:AsyncPostBackTrigger ControlID=ddlInsertionTypes EventName=SelectedIndexChanged />
  11.         <asp:AsyncPostBackTrigger ControlID=cmdForcePostBack1 EventName=Click />
  12.     </Triggers>
  13. </asp:UpdatePanel>
  14.  
  15.  

In my .js files:


Expand|Select|Wrap|Line Numbers
  1.  
  2. function tblInsertionItems_onRowSelect(row, selected)
  3. {
  4.     if (selected) {
  5.  
  6.         var item = getInsertionItemById(row.getAttribute('insertionItemId'));
  7.  
  8.         if (item != null) {
  9.  
  10.             setInsertionTypeFromId(item.itemTypeId);
  11.  
  12.             // force the UpdatePanel to postback
  13.             document.getElementById(_cmdForcePostBack1_ClientId).click();
  14.  
  15.             // if we leave this alert in the insertion code is properly selected in the DropDownList.   
  16.             alert('here');
  17.  
  18.             setInsertionCodeFromId(item.itemId);
  19.         }
  20. }
  21.  
  22.  
  23. function setInsertionCodeFromId(id)
  24. {
  25.     var ddl = document.getElementById(_ddlInsertionCodes_ClientId);
  26.  
  27.     return setSelectedValue(ddl, id);
  28. }
  29.  
  30. function setInsertionTypeFromId(id)
  31. {
  32.     var ddl = document.getElementById(_ddlInsertionTypes_ClientId);
  33.  
  34.     return setSelectedValue(ddl, id);
  35. }
  36.  
  37. function setSelectedValue(ddl, selectedValue)
  38. {
  39.     if (ddl != null) {
  40.  
  41.         var options = ddl.options;
  42.  
  43.         for (var i=0; i < options.length; i++) {
  44.             if (parseInt(options[i].value) == selectedValue) {
  45.                 ddl.selectedIndex = i;
  46.                 return true;
  47.             }
  48.         }
  49.     }
  50.  
  51.     return false;
  52. }
  53.  
  54.  
Aug 7 '07 #1
3 10547
pbmods
5,821 Expert 4TB
Heya, Lohboy. Welcome to TSDN!

Thanks for using CODE tags! Did you know that you can specify a language for your CODE tags to make your source code easier to read?

You will still need to use [/code] to close your code blocks, regardless of the language, but you can the use one of these tags to open your code block:

[code=html]
[code=javascript]
[code=php]

and so on.

Thanks!

MODERATOR
Aug 7 '07 #2
Lohboy
2
Figured it out. I was attempting to set the selected item before the asynch postback was complete. I just needed to handle the PageRequestManager pageLoaded event.

Expand|Select|Wrap|Line Numbers
  1.  
  2. function tblInsertionItems_onRowSelect(row, selected)
  3. {
  4.  
  5.     if (selected) {
  6.         var item = getInsertionItemById(row.getAttribute('insertionItemId'));
  7.  
  8.         if (item != null) {
  9.  
  10.             _editInsertionItemId = item.id;
  11.  
  12.             setInsertionTypeFromId(item.itemTypeId);
  13.  
  14.             // set up our handler for when the asynch postback completes
  15.             Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(insertionTypesLoaded);
  16.  
  17.             // force the postback
  18.             document.getElementById(_cbxInsertionTypes_ClientId).fireEvent('onchange');
  19.  
  20. }
  21.  
  22. function insertionTypesLoaded(sender, e)
  23. {
  24.  
  25.     var item = getInsertionItemById(_editInsertionItemId);
  26.  
  27.     if (item != null)
  28.         setInsertionCodeFromId(item.itemId);
  29.  
  30.     Sys.WebForms.PageRequestManager.getInstance().remove_pageLoaded(insertionTypesLoaded); 
  31.  
  32. }
  33.  
  34.  
Aug 9 '07 #3
pbmods
5,821 Expert 4TB
Heya, Lohboy.

Thanks for posting your solution.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 9 '07 #4

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

Similar topics

5
by: Allan M. | last post by:
I have a series of select boxes that must be populated client side, because they interact with each other. The design specification calls for these boxes to be updated without having to make a...
7
by: localhost | last post by:
A DataGrid with shows a label in one of the columns when in view mode. When in edit mode, I want to show a dropdown, and have the default selection set to what the textbox used to be. Right now...
6
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this...
3
by: Siamak Zahedi | last post by:
Hi, weird problem I'm having cant seem to figure out what is going on. I have a dropdownlist that gets populated from db and a button that causes a post back when I assign the value of the...
1
by: Atreju | last post by:
Ok I got form within a c sharp page. Situation: On the form I have a drop downliwst and a textbox, the dropdownlist is populated with products and the textbox has a default vale of the product...
6
by: Steve Hershoff | last post by:
Hi everyone, I've got a strange one here. There are two datagrids on my page, one nested within the other. I'll refer to them as the topmost and secondary datagrids. In the topmost...
2
by: JamesW | last post by:
Hi there. Vstudio 2003, IE 6.0 I have an asp connected to an Access database. A DropDownList on the page shows names from the Db. When the user selects a name from the DropDownList I want...
4
by: Jimmy M | last post by:
I've got a page that generates dropdowns and text boxes based on database data. I have them all set to auto-postback because I'll be using this with Atlas to make a more seamless user experience....
4
by: =?Utf-8?B?c2lMdmVy?= | last post by:
Hhi, I'm working on an asp .net project that have small side panel which 'disallow' long SELECT/dropdown list.. So i could not force them to a certain that shows my longest option, which now...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.