[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

asp.net dynamic drop down

Posted on 2011-09-22
8
Medium Priority
?
255 Views
Last Modified: 2012-05-12
Experts,

I have a dropdown <select><option value=1>1</option><option value=25>25</option></select>
When Onchange event is on, I would like to show like
City/State: <div><input name=city> <input name=state></div>

If 1 is selected, it show one city/state, if 25 is selected it show 25 city/state
and each <input name> has to be different.

And once the button is clicked, it will captures each city/state and save into the database.

Thanks
0
Comment
Question by:Webboy2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 36581036
I would do this through javascript, create an onChange javascript event for your drop down and do something like this:

function DDLChange()
{
     if(document.getElementById('<%= MyDDL.ClientID %>').val == 1)
          document.getElementById('<%= MyCity.ClientID %>').style.display = "none";
     else
          document.getElementById('<%= MyCity.ClientID %>').style.display = "";        
}
0
 

Author Comment

by:Webboy2008
ID: 36582016
strickdd: I think yours won't work to me. It just display <div> or not.
I need a list of city/state based on the number he/she select
Plus I need to save the value into Db as well.

Thanks
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36582142
Then you can have you OnChange server event dynamically add the controls you need to a place holder. In that you can check the value and add the controls needed. The catch is, you will have to add these controls EVERY postback otherwise they will be lost. You can even have a method like this instead of you OnChange method:

protected void Page_Load(...)
{
      if(Page.IsPostBack)
            CreateFields();
}

private void CreateFields()
{

     if(myDDL.SelectedValue == "1")
     {
           TextBox stateTextBox = new TextBox();
           stateTextBox.ID = "MyTextBoxID" //This must be the same each postback
           //Viewstate will automatically re-populate the textboxes when created like this
          MyPlaceHolder.Controls.Add(stateTextBox);
      }
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 28

Expert Comment

by:strickdd
ID: 36582148
If you go that route, you will need to set the AutoPostBack to true on the DDL so it does a server post.
0
 

Author Comment

by:Webboy2008
ID: 36583653
Strickdd: If you don't mind, I really need completed working codes. Thanks. About your codes,
it won't work because it is static textbox name. If I have 25 city/states, what the textbox names will be...?
0
 
LVL 28

Accepted Solution

by:
strickdd earned 2000 total points
ID: 36586413
Assuming you have a fixed naming pattern for you drop downs, you can do something like the following. It is an example of what you will need to do, but the basic concept is to loop through your drop downs, check the values, and add the controls needed.

for (int i=0; i< numDDLs; i++)
{
        if(((DropDownList)Page.FindControl("MyDDL_ID_" + i.ToString()).SelectedValue == "1") //Instead of Page.FindControl, use the closes parrent for better efficiency
        {
                 TextBox stateTextBox = new TextBox();
                 stateTextBox.ID = "MyTextBoxID" + i.ToString() //This must be the same each postback, the static string and "i" value will make it the same
               ((PlaceHolder)Page.FindControl("MyCityStatePlaceHolder_ID_" + i.ToString().Controls.Add(stateTextBox);
         }
}

Open in new window

0
 

Author Comment

by:Webboy2008
ID: 36588374
A completed and working codes will be really helpful....:-D Thanks.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36589305
Given your loosely defined requirements and no knowledge of what your setup is exactly, that is an impossible task. Given the level of knowledge you appear to have, it is well within your ability to take the code provided and make it suit your exact needs.

Experts Exchange is not a place to have people do the work for you, but rather a place to receive assistance on technical problems you are having.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question