Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

I need to set the value of DropDownList.SelectedIndex and I get either error read only or nothing changes.

Posted on 2005-03-11
4
Medium Priority
?
4,772 Views
Last Modified: 2007-12-19
I'm programming in ASP.net and C#.

I have the DropDownList on a WebForm.
There are 2 DropDownLists and TextBoxes on this WebForm.

The user clicks on Edit and the WebForm with the DropDownList loads.
The textboxes get filled with the data for the record the user selected on the the previous page.
The two DropDownLists get populated on the Page_Load with multiple values from the database.

The first item in this list says -Select- and the others below it say -Nissan-, -Ford- and -BMW-.
So I think the -Select- has the index of 0 Nissan 1 Ford 2 BMW 3.

What should happen is that when the page loads, one of the texts like Nissan Ford or BMW should be selected in the list instead of -Select-, according to the record selected from the previous page

The furthest I have gotten is with this code, which is within a function, which is called on the Page_Load event:

Models oCatalog = new Model();
SqlDataReader dr = oCatalog.GetCatalog(idCompany);                   
while(dr.Read())
{      

ddlIDCompany.SelectedIndex=ddlIDCompany.Items.IndexOf(ddlIDCompany.Items.FindByText(dr["ModelName"].ToString()));
}
which does nothing

however when I place ddlIDCompany.Items.IndexOf(ddlIDCompany.Items.FindByText(dr["ModelName"].ToString()));

in a Response.Write() it returns 1,2 or 3 depending on the record the user had selected on the previous page

This one´s a doozy for me.
0
Comment
Question by:jumprooster
  • 2
4 Comments
 
LVL 4

Expert Comment

by:jaynus
ID: 13519163
Is this being executed on a postback or every load, and before or render?
0
 

Author Comment

by:jumprooster
ID: 13522126
It's being executed in the initial Page_Load like this:

private void Page_Load(object sender, System.EventArgs e)
{
            
 if(!Page.IsPostBack)
ExecuteAction();

}

public void ExecuteAction()
{
ChrgFormModel();
}

public void ChrgFormModel()
{
Models oCatalog = new Model();
SqlDataReader dr = oCatalog.GetCatalog(idCompany);                
while(dr.Read())
{    

ddlIDCompany.SelectedIndex=ddlIDCompany.Items.IndexOf(ddlIDCompany.Items.FindByText(dr["ModelName"].ToString()));
}


}
0
 
LVL 13

Accepted Solution

by:
dungla earned 1500 total points
ID: 13541532
You can do that:

ListItem li = ddlIDCompany.Items.FindByText(dr["ModelName"].ToString());
// or
// ListItem li = ddlIDCompany.Items.FindByValue(dr["ModelName"].ToString());
if (li != null)
{
      li.Selected = true;
}
0
 

Author Comment

by:jumprooster
ID: 13612806
Yep, you can use teh ListItem class to insert items into a dropdownlist programatically including the selected item.

:)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

572 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