Solved

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

Posted on 2009-04-06
7
825 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 150 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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 350 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

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