?
Solved

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

Posted on 2009-04-06
7
Medium Priority
?
829 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
[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
  • 4
  • 2
7 Comments
 
LVL 27

Expert Comment

by:nmarun
ID: 24079219
You need to do:

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

Assisted Solution

by:Allamz
Allamz earned 600 total points
ID: 24079262
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
ID: 24079532
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tcmmaxt
ID: 24079859
  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 1400 total points
ID: 24079887
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
ID: 24090454
no luck: above
0
 
LVL 27

Expert Comment

by:nmarun
ID: 24090480
What message do you get?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 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