Solved

asp .net C# how do you set dropdownlist in page load if (!Page.IsPostBack)

Posted on 2009-04-06
7
820 Views
Last Modified: 2012-08-13
Expert: I have a Formview with a dropdownlist that is loaded by the database. However when the page is first loaded I want to set the dropdownlist to say "expert". I tried the following with no luck:  --> could you please show me the correct way to do this?
if (!Page.IsPostBack)
        {
            FormView form = FindControl("expertform) as FormView;
            DropDownList Ddl =
                    form .FindControl("mydropdown_Ddl") as DropDownList;
            mydropdown_Ddl.SelectedValue = "expert";
    }
0
Comment
Question by:tcmmaxt
  • 4
  • 2
7 Comments
 
LVL 27

Expert Comment

by:nmarun
Comment Utility
You need to do:

mydropdown_Ddl.Items.FindByValue          ( "expert" ).Selected = true;
0
 
LVL 6

Assisted Solution

by:Allamz
Allamz earned 150 total points
Comment Utility
Hello tcmmaxt,

Please use the following code instead of  mydropdown_Ddl.SelectedValue = "expert";

Best Regards,

Mohamed Allam
Senior Solution Developer
mydropdown_Ddl.SelectedText= "expert";

Open in new window

0
 
LVL 27

Expert Comment

by:nmarun
Comment Utility
Allamz, asp.net dropdownlists do not have a property called '.SelectedText'. I think you meant '.SelectedItem.Text' and that won't work either as it'll just add 'expert' to the list if it doesn't exist.

       tcmmaxt, I think the best way out is:

            if(dgTest1.Items.FindByValue("1") != null)
            {
                dgTest1.Items.FindByValue("1").Selected = true;
            }
            else
            {
                dgTest1.SelectedIndex = 0;
            }
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:tcmmaxt
Comment Utility
  if (!Page.IsPostBack)
        {
            FormView form = FindControl("expertform) as FormView;
            DropDownList Ddl =
                    form .FindControl("mydropdown_Ddl") as DropDownList;
      #1      Ddl.SelectedText = "expert";  NO  LUCK
       #2       Ddl.Items.FindByValue ( "expert" ).Selected = true;  NO LUCK

Object reference not set to an instance of an object???  Maybe cannot find "expert" since it's loaded in dropdownlist by database table same time page is loaded?

           
    }


            CountryDdl.Items.FindByText("United States").Selected = true;  


           //CountryDdl.SelectedText= "United States";
0
 
LVL 27

Accepted Solution

by:
nmarun earned 350 total points
Comment Utility
Tom, I'm sorry about the mistake, but did you try the code:

           if(mydropdown_Ddl.Items.FindByValue("expert") != null)
            {
                mydropdown_Ddl.Items.FindByValue("expert").Selected = true;
            }
            else
            {
                mydropdown_Ddl.SelectedIndex = 0;
            }
             
0
 

Author Comment

by:tcmmaxt
Comment Utility
no luck: above
0
 
LVL 27

Expert Comment

by:nmarun
Comment Utility
What message do you get?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now