?
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,766 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
[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
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

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