|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace SubSonic
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:DropDown runat=server></{0}:DropDown>")]
public class DropDown : DropDownList
{
private string promptText = "--Please Select--";
public string PromptText
{
get { return promptText; }
set { promptText = value; }
}
private bool showPrompt;
public bool ShowPrompt
{
get { return showPrompt; }
set { showPrompt = value; }
}
private string promptValue = string.Empty;
public string PromptValue
{
get { return promptValue; }
set { promptValue = value; }
}
private string providerName = string.Empty;
public string ProviderName
{
get { return providerName; }
set { providerName = value; }
}
private string tableName = string.Empty;
public string TableName
{
get { return tableName; }
set { tableName = value; }
}
private string textField = string.Empty;
public string TextField
{
get { return textField; }
set { textField = value; }
}
private string valueField = string.Empty;
public string ValueField
{
get { return valueField; }
set { valueField = value; }
}
private string orderField = string.Empty;
public string OrderField
{
get { return orderField; }
set { orderField = value; }
}
private string whereField = string.Empty;
public string WhereField
{
get { return whereField; }
set { whereField = value; }
}
private string whereValue = string.Empty;
public string WhereValue
{
get { return whereValue; }
set { whereValue = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (!DesignMode)
{
//load em up
//cheap way to check for load state
if (Items.Count == 0)
{
if (!String.IsNullOrEmpty(tableName))
{
Query q = new Query(tableName, providerName);
if (String.IsNullOrEmpty(valueField) || String.IsNullOrEmpty(textField))
{
//look it up using the table schema
TableSchema.Table tbl = DataService.GetSchema(tableName, providerName, TableType.Table);
if (tbl != null)
{
//set the fields accordingly
if (String.IsNullOrEmpty(valueField))
{
valueField = tbl.PrimaryKey.ColumnName;
}
if (String.IsNullOrEmpty(textField))
{
if (tbl.Columns.Count > 1)
{
textField = tbl.Columns[1].ColumnName;
}
else
{
textField = tbl.Columns[0].ColumnName;
}
}
}
else
{
throw new Exception("Table name '" + tableName + "' using Provider '" + providerName + "' doesn't work");
}
}
q.SelectList = valueField + "," + textField;
if (!String.IsNullOrEmpty(OrderField))
q.OrderBy = OrderBy.Asc(OrderField);
else
q.OrderBy = OrderBy.Asc(textField);
if (!String.IsNullOrEmpty(WhereField))
q.AddWhere(WhereField, WhereValue);
//fire up a reader
IDataReader rdr = null;
try
{
rdr = q.ExecuteReader();
while (rdr.Read())
{
ListItem item = new ListItem(rdr[1].ToString(), rdr[0].ToString());
Items.Add(item);
}
}
catch (DataException x)
{
throw new Exception("Error loading up ListItems for " + ClientID + ": " + x.Message);
}
finally
{
if (rdr != null)
{
rdr.Close();
}
}
ListItem prompt = new ListItem(promptText, PromptValue);
if (showPrompt)
Items.Insert(0, prompt);
if (!String.IsNullOrEmpty(SelectedValue))
{
foreach (ListItem item in Items)
{
if (Utilities.Utility.IsMatch(item.Value, SelectedValue))
{
item.Selected = true;
break;
}
}
}
}
else
{
//do nothing
}
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.