473,473 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

creating dynamic drop down list(ASP .net)

12 New Member
I am tring to create n number of dropdown list.each have the same content. In this way i am able to create the dropdownlist but when the post back happens on the page the dropdownlist vanishes.
so what should be done in this case. i don't now VB so if you could please explain in c# it will be very kind of u.
Expand|Select|Wrap|Line Numbers
  1. DropDownList[] t = new DropDownList[12];
  2. or (int i = 1; i <= n; i++)
  3.             {
  4.  
  5.                 t[i].ID = "C" + Convert.ToString (i);
  6.  
  7.                 t[i].DataTextField = "cropName";
  8.                 t[i].DataValueField = "cropId";
  9.                 t[1].Visible = true;
  10.  
  11.                 t[i].DataSource = ds;
  12.                 t[i].DataBind();
  13.                 t[i].Items.Insert(0, "--Select--");
  14.                 Literal l1 = new Literal();
  15.                 l1.Text = "<br/>";
  16.                 PlaceHolder1.Controls.Add(t[i]);
  17.                 PlaceHolder1.Controls.Add(l1);
  18.  
  19.  
  20.             }
Sep 18 '08 #1
5 3410
shweta123
692 Recognized Expert Contributor
Hi,

Hi ,
You have to check the isPostBack property of the page . You can create dropdownlistboxes and fill them if isPostBack property is false so that the dropdownlistboxes will not be created each time your page is posted back.
e.g.

Expand|Select|Wrap|Line Numbers
  1. if(this.isPostBack == false)
  2.   {
  3.     //Now, your code code will be executed only once
  4. DropDownList[] t = new DropDownList[12];
  5. or (int i = 1; i <= n; i++)
  6. {
  7.  
  8. t[i].ID = "C" + Convert.ToString (i);
  9.  
  10. t[i].DataTextField = "cropName";
  11. t[i].DataValueField = "cropId";
  12. t[1].Visible = true;
  13.  
  14. t[i].DataSource = ds;
  15. t[i].DataBind();
  16. t[i].Items.Insert(0, "--Select--");
  17. Literal l1 = new Literal();
  18. l1.Text = "<br/>";
  19. PlaceHolder1.Controls.Add(t[i]);
  20. PlaceHolder1.Controls.Add(l1);
  21.  
  22. }
  23.  
Sep 18 '08 #2
smithak82
12 New Member
Hi,

Hi ,
You have to check the isPostBack property of the page . You can create dropdownlistboxes and fill them if isPostBack property is false so that the dropdownlistboxes will not be created each time your page is posted back.
e.g.

Expand|Select|Wrap|Line Numbers
  1. if(this.isPostBack == false)
  2.   {
  3.     //Now, your code code will be executed only once
  4. DropDownList[] t = new DropDownList[12];
  5. or (int i = 1; i <= n; i++)
  6. {
  7.  
  8. t[i].ID = "C" + Convert.ToString (i);
  9.  
  10. t[i].DataTextField = "cropName";
  11. t[i].DataValueField = "cropId";
  12. t[1].Visible = true;
  13.  
  14. t[i].DataSource = ds;
  15. t[i].DataBind();
  16. t[i].Items.Insert(0, "--Select--");
  17. Literal l1 = new Literal();
  18. l1.Text = "<br/>";
  19. PlaceHolder1.Controls.Add(t[i]);
  20. PlaceHolder1.Controls.Add(l1);
  21.  
  22. }
---------------
hi
actually my code isattached to button it is not in page load.
so if i make the post back false the dynamic dropdownlist will not be created.
actually i have a text box and a button. in text box i have to enter the number of drop downlist required. once i enter the number in the text box and press the button. the dynamic dropdownlist get created. in the same page there are some other dependent dropdownlist. to the page get postback. when the page is postbacked the drop down list created dynamically vanishes.
please help
Sep 19 '08 #3
PRR
750 Recognized Expert Contributor
When you post back your page the code in page load gets executed... As you have written code on button click that wont be visible .... You could do one thing.. use some static variable(bool) and call function depending on whether the button was clicked or not ... and if clicked call your function that puts dropdowns on your page...You will ahve to use 2 static variables.(int n bool)
[quote=smithak82]
Hi,

Hi ,
You have to check the isPostBack property of the page . You can create dropdownlistboxes and fill them if isPostBack property is false so that the dropdownlistboxes will not be created each time your page is posted back.
e.g.

Expand|Select|Wrap|Line Numbers
  1. if(this.isPostBack == false)
  2.   {
  3.     //Now, your code code will be executed only once
  4. DropDownList[] t = new DropDownList[12];
  5. or (int i = 1; i <= n; i++)
  6. {
  7.  
  8. t[i].ID = "C" + Convert.ToString (i);
  9.  
  10. t[i].DataTextField = "cropName";
  11. t[i].DataValueField = "cropId";
  12. t[1].Visible = true;
  13.  
  14. t[i].DataSource = ds;
  15. t[i].DataBind();
  16. t[i].Items.Insert(0, "--Select--");
  17. Literal l1 = new Literal();
  18. l1.Text = "<br/>";
  19. PlaceHolder1.Controls.Add(t[i]);
  20. PlaceHolder1.Controls.Add(l1);
  21.  
  22. }
---------------
hi
actually my code isattached to button it is not in page load.
so if i make the post back false the dynamic dropdownlist will not be created.
actually i have a text box and a button. in text box i have to enter the number of drop downlist required. once i enter the number in the text box and press the button. the dynamic dropdownlist get created. in the same page there are some other dependent dropdownlist. to the page get postback. when the page is postbacked the drop down list created dynamically vanishes.
please help
Sep 19 '08 #4
Frinavale
9,735 Recognized Expert Moderator Expert
You have to keep in mind the Scope of your objects.

If you are dynamically creating DropDownLists in a button click event, the scope of those DropDownLists is limited to that function.

You need to declare your list of DropDownLists outside of that function so that their scope can exist for the whole page.

If you do not do this, your DropDownLists will not existed upon postback (so they cannot be rendered again) and also any events raised by the DropDownLists (eg: autopostback) will not be handled.

You should keep track of how many DropDownLists your page requires (in a HiddenField, Session, Cookie, ViewState...any way you wish) and you will need to initialize that number when the page first posts back.

(Recall the ASP.NET Page Life Cycle -> once the page is sent to the browser every control on the server is destroyed)

You may run into a problem because it is recommended that you initialize your dynamic controls in the Page Init event. The Page Init event occurs before the control ViewStates are loaded. The reason why it is recommended that you do this is because right after the Page Init event your control ViewStates are loaded....if you have not instantiated your controls before this, the ViewState cannot be loaded for the control and you will lose all of your Events and other control ViewState data.

Please check out the article on how to use dynamic controls in asp.net. I realize that this article has examples written in VB but the concepts that you have to understand when using dynamic controls are important to grasp.

-Frinny
Sep 19 '08 #5
smithak82
12 New Member
thank you,
It worked.
I tried implementing it step by step and it worked.
thanks a lot.
it really solved my problem.
Sep 20 '08 #6

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

Similar topics

3
by: Danny | last post by:
How do you guys make listings such as product listings and their detail pages more crawler friendly, if they are genrated using dynamic asp pages. If your ASP uses a format like this:...
14
by: anon | last post by:
Does anyone know how many man-hours were used in creating .NET 1.0? I thought it would be an interesting statistic to know. Thanks.
0
by: Edward Diener | last post by:
Is there any good documentation on creating .NET components and .NET controls with MC++ ? On MSDN there is a general information in the .NET Framework Developer's Guide, but in MSDN under Managed...
3
by: J | last post by:
Hi, I have setup a test server with the following installed: 1) Windoes Server 2003 - Latest SPs applied. 2) Ms_SQL Server 2000 - Latest SPs applied 3) Visual Studio 2003 I have Admin...
2
by: Jörn von Holten | last post by:
Hi, my 3-tier system has a separate bizz-logic and storage service (MVC paradigm as usual) and the bizz-logic layer shall drive the logic (workflow) of the ASP site. My "idea" is (alike Xaml...
1
by: Peter Strøiman | last post by:
Hi. I have a web project, that is intended to be installed multiple times as different product with different names. All strings in the web are placed in local resource files. In some of the...
5
by: Ruben Gatt | last post by:
Hi, I want to do a drag and drop from one listbox to another? is it possible?? if yes any tips ?? Regards RG
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.