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

Dynamically populating a DropDownList, AutoPostback and JavaScript error

100+
P: 112
hello,

i am trying to create a request form using asp.net where when a user selects a checkbox, a dropdown should appear dynamically. I have seen few java-scripts in web, but I was wondering if I can achieve this without java-script.

I tried to make the control invisible(<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True" Visible="False">) and in the onselectedindexchanged event, tried to make the invisible property to true. But it did not help me.
Also, I was trying to use postback for the page when a checkbox is selected(asp:CheckBoxList ID="chckbx" runat="server" AutoPostBack="true" onselectedindexchanged="chckbx_SelectedIndexChange d") and it throws me an error in IE saying the object does not support this property.


Can somebody please help me with this. Thanks in advance.
Dec 7 '09 #1
Share this Question
Share on Google+
27 Replies


Frinavale
Expert Mod 5K+
P: 9,731
Before you continue please take a look at this article on:
How to use dynamic controls in ASP.NET.

-Frinny
Dec 7 '09 #2

100+
P: 112
thank you for the reply.

I am able to understand the example in the link but could not really put it into working for my scenario.
Dec 7 '09 #3

Frinavale
Expert Mod 5K+
P: 9,731
Ok, in your scenario you need to do this when the user checks a checkbox.
I'm assuming that you need to populate the drop down list with items depending on what the user has checked.

This is not dynamically creating a control.

What you need to do is dynamically populate the drop down list.

What do you have currently that we can use as a starting point?

-Frinny
(PS please post code in code tags)
Dec 7 '09 #4

100+
P: 112
Yes, I have to populate the dropdown based on the selection of checkbox.

Here is my information. I have four sources of information as checkboxes, newspapers,web,employee and others. Now when a user checks newspapers, the dropdown must populate, tribune,WSjournal,PJ star.

And one more thing is if I put the autopostback ppty for checkbox to true, it throws me an error. This should be something very basic, am I missing something?
Thanks for all your help.
Dec 7 '09 #5

Frinavale
Expert Mod 5K+
P: 9,731
Lets fix the error because you need to set the checkBoxList's autopostback=true.
Please post the error details.

-Frinny
Dec 7 '09 #6

100+
P: 112
the error appears only in IE, as a script error. I mean the page loads normally but on the left hand bottom corner, it says there is an error on the page. When i click on it, it tells me the the object does not support this property or method.
When I try to debug the page without the autopostback property, it does not display any error.
Dec 7 '09 #7

Frinavale
Expert Mod 5K+
P: 9,731
Do you have any additional JavaScript on the page?

-Frinny
Dec 7 '09 #8

100+
P: 112
yes, a one line javascript to open a pop-up window.
Dec 7 '09 #9

Frinavale
Expert Mod 5K+
P: 9,731
Post that code please.

-Frinny
Dec 7 '09 #10

100+
P: 112
Expand|Select|Wrap|Line Numbers
  1. function window_open()
  2.       {
  3.        window.open("Codes.aspx","","menubar=no,status=no,scrollbars=yes,resizable=yes,toolbar=no,top=180,left=250,width=750,height=600");
  4.       }
the ap.net control that calls it,
Expand|Select|Wrap|Line Numbers
  1. <asp:Button runat="server" ID="btnlookup" Text="Code" OnClientClick="window_open()" CausesValidation="false" Width="90px" />
Dec 7 '09 #11

Frinavale
Expert Mod 5K+
P: 9,731
I don't see anything wrong with this JavaScript.
I'm going to ask that a JavaScript expert take a look at this.

Does this problem prevent your code from being executed?
If so, could you remove it while we are fixing your current problem with dynamically populating the DropDownList?

-Frinny
Dec 7 '09 #12

Dormilich
Expert Mod 5K+
P: 8,639
I'm going to ask that a JavaScript expert take a look at this.
I canít find anything wrong, works as expected when inserted in plain text HTML.
Dec 7 '09 #13

100+
P: 112
my page executes normally. but I just wanted to get rid of the error and also wanted to postback the page when I check the checkbox.
Dec 7 '09 #14

Frinavale
Expert Mod 5K+
P: 9,731
Well enable autopostback for the checkbox.
Put a break point on the method that handles the event and run the application.
Do you hit the break point?

-Frinny
Dec 7 '09 #15

100+
P: 112
ah..it does not hit the breakpoint. and the script error is displayed only when I try to check the checkbox, not when the page loads.
Dec 7 '09 #16

Frinavale
Expert Mod 5K+
P: 9,731
Okay, it sounds like the JavaScript that preforms the AutoPostback is not working.

I don't know why this would be the case. The only thing that I can think of is that your JavaScript is somehow invalid (maybe you didn't close the <script> tag?) But your JavaScript looks fine.

Try removing your JavaScript from the page.
Do you get the same error?

What web browser are you using? What version is it?

Are you using Ajax?
If so are you using any Ajax enabled controls?
These may be causing a problem...


-Frinny
Dec 7 '09 #17

100+
P: 112
Hi Frinny

Thank you for your quick responses.

I did try to remove the javascript from the page. it still gives me the error. I am currently using IE7 and I do not have any ajax controls.
I did try to create a new checkbox list and even that gives me the same error.

I created a new page and could figure out the how to create a dropdown when a checkbox is selected. It works fine. But my original page still has the error for the autopostback.

Your help is greatly appreciated.
Thank you...
Dec 7 '09 #18

Frinavale
Expert Mod 5K+
P: 9,731
It's too bad that you were having such a hard time with your original page.
At least the error isn't happening on your new aspx page.

So, in the method that handles the AutoPostback for the CheckBoxList (list??) you need to create list items for your DropDownList based on what was checked/selected.

(I'm not sure what language you're using.)

There are two ways to do this. You can create generate ListItem objects and add them to your DropDownList's "Items" property. Or you can bind your DropDownList to a data source.

-Frinny
Dec 7 '09 #19

100+
P: 112
yeah. I did use the items property to make my dropdowns work and it works fine, but still could not figure out what is wrong with original page.

Anyway thanks a lot for your help and support.
Dec 7 '09 #20

Frinavale
Expert Mod 5K+
P: 9,731
Ah, sorry I miss-read your response.
You have figured out how to dynamically populate your DropDownList.
Contrats!

:)

I have no idea what is wrong with your page.
I can't see you page so....I'm kind of in the dark here.

The only reasons that I can think of is that there is some sort of invalid JavaScript or Ajax on the page that is breaking the code that normally preforms the AutoPostback.

Stuff like not closing the script tag or leaving out a curly brace ("}") or something.

If you want to debug your original page, and you don't want to move your code into the new page, then (...I think I'm going to regret asking for this but...) please post the entire ASPX page code. **winces**

-Frinny
Dec 7 '09 #21

100+
P: 112
Thank you.

But I am afraid I wont be able to post the aspx code as it has many fields that are pulled from a database.
Anyway, I will try to move my code once I figure out a small issue that i have with my new page.

My new issue, as mentioned earlier I have a checkbox list. But checking only one box should enable the dropdown.
For example,
  • Web
  • Employees
  • Newspaper
  • Other

As it is a checkbox list, a user can select any number of checkboxes. BUt my dropdwon should be enabled only when newspapers is checked.I see that whenever I select more than two checkboxes, the CheckBoxList1.SelectedValue still has only one value. For example a user selects both web and newspaper, but the dropdown is not enabled as CheckBoxList1.SelectedValue is Web.
In my CheckBoxList1_SelectedIndexChanged, I am using
Expand|Select|Wrap|Line Numbers
  1. if (CheckBoxList1.SelectedValue == "NewsPaper"){//enable dropdown}
Am I missing a logic here or is there any way to collect all the values that are checked
Any help would be greatly appreciated.
Please let me know if I am not clear.
Dec 7 '09 #22

Frinavale
Expert Mod 5K+
P: 9,731
Loop through each item in the CheckBoxList and check if it is "NewsPaper" and if it is selected.

The thing is that the CheckBoxList is going to post back every time a check box is checked within it. Make sure you check to see if the DropDownList is already populated.

-Frinny
Dec 8 '09 #23

100+
P: 112
Hello Frinny

Thanks for all the help. I have accomplished all the required functionality. But still my script error exists, I tried to transfer my original page code to a new page...and again the script error pops up...still struggling to find out the cause.
Dec 8 '09 #24

Frinavale
Expert Mod 5K+
P: 9,731
Wish I could help but I'm in the dark here.
Can't see what you're doing.

-Frinny
Dec 8 '09 #25

100+
P: 112
yeah..I can understand. Wish I could share the .aspx file.
Anyway, I am debugging the code line by line...and trying to find my luck

Thank you..
Dec 8 '09 #26

100+
P: 112
Hurray, my code is working.
I had a sumbit button in my page with id='submit' and I did change the small letter 's' to capital letter 'S' and it working like a charm..
It was a silly thing but dont know why it caused the problem. Have found this solution online after hours of searching.
Thought this could help someone like me.
Dec 8 '09 #27

Frinavale
Expert Mod 5K+
P: 9,731
It's strange that this would cause a problem with JavaScript.
Regardless I'm glad you solved your problem :)

-Frinny
Dec 9 '09 #28

Post your reply

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