[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-04-06
7
Medium Priority
?
832 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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

868 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