Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to set the 'selected' attribute of a dynamic dropdownlist

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
I need to automatically set the 'selected' attribute for the correct value in a dropdownlist. I'm not very familiar with this code b/c I didn't write it.

Anyway, here's the code that populates the dropdownlist...it works just fine:
Code:
public static DataSet GetClosuresBillingAddressDataSet(string CustomerId)
        {
            try
            {
                DataSet ds = new DataSet();
                System.Text.StringBuilder sb = new System.Text.StringBuilder();

                sb.AppendLine("SELECT a.*, b.bill_to_location_id FROM V_CSIDIRECT_BILL_TO a, V_CSIDIRECT_SHIP_TO b ");
                sb.AppendLine("WHERE a.location_id = b.bill_to_location_id ");
                sb.AppendLine("AND a.CUSTOMER_NUMBER = " + CustomerId + " ");
                //TODO - US OR MEXICO
                sb.AppendLine("AND a.OPERATING_UNIT = 2008 ");
                sb.AppendLine("ORDER BY a.ADDR, a.STATE, a.CITY ");

                ds = m_DataWarehouseDatabase.ExecuteDataSet(CommandType.Text, sb.ToString());

                //try to get non NA_GDW value because can be US or non-US address
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    dr[3] = dr[3].ToString().Replace("NA_GDW,", "");
                }
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

And on the page load, it binds this to the ddl:
Code:
DataSet dsBilling = ClosuresSystem.GetClosuresBillingAddressDataSet(Session["CUST_ID"].ToString());
                ddlBillToAddress.Visible = true;
                ddlBillToAddress.DataSource = dsBilling;
                ddlBillToAddress.DataBind();

And to automatically select the correct item from the ddl, I'm think I would use something like this...I just don't know where to put it.
Code:
ddlBillToAddress.SelectedIndex == ddlBillToAddress.Items.IndexOf(ddlBillToAddress.Items.FindByValue("bill_to_location_id"))
Any ideas? Not sure if this is enough info to go on or not.
 
Code:
string id_to_selelct = [something];
DDL.DataSource = GetData();
DDL.DataBind();
DDL.SelectedIndex = DDL.Items.IndexOf(DDL.Items.FindByValue(id_to_selelct));

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Okay, I need to set the id_to_select varialbe to the result of a query, but I'm not sure how to do that...
Here's what I'm trying, but getting an error...
Code:
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendLine("SELECT bill_to_location_id FROM V_CSIDIRECT_SHIP_TO WHERE location_id = " + Session["CLOSURES_SHIP_TO"].ToString() + "");
DataSet dsShipId = new DataSet();
dsShipId = ClosuresSystem.GetDataSetOracleDataWarehouse(sb.ToString());
                
string id_to_select = dsShipId.bill_to_location_id;

What am I doing wrong here?
 
string id_to_select = dsShipId.Rows[0]["bill_to_location_id"];

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I must be going about this all wron b/c no matter what I try I'm getting error after error. I'm going to end this thread and revisit this later.

Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top