Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-04-06
7
Medium Priority
?
831 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
Industry Leaders: 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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 …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
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

636 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