📄 combobox.cs
字号:
protected internal virtual string ClientFormID {
get {
Control parent = this;
while (parent != null) {
if (parent is System.Web.UI.HtmlControls.HtmlForm)
break;
parent = parent.Parent;
}
return parent.ClientID;
}
}
#endregion
#region Render Methods
/// <summary>
/// This member overrides Control.AddParsedSubObject.
/// </summary>
/// <param name="obj"></param>
protected override void AddParsedSubObject(object obj) {
if (obj is ListItem) {
this.Items.Add((ListItem) obj);
return;
}
throw new HttpException("ComboBox Cannot Have Children Of Type " + obj.GetType().Name);
}
/// <summary>
/// This member overrides Control.Render.
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer) {
this.RenderBeginTag(writer);
this.RenderChildren(writer);
this.RenderEndTag(writer);
}
/// <summary>
/// This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
/// </summary>
/// <param name="writer"></param>
public override void RenderBeginTag(HtmlTextWriter writer) {
writer.Write("<?XML:NAMESPACE PREFIX=\"" + TagNamespace
+ "\" />\n<?IMPORT NAMESPACE=\"" + TagNamespace + "\" IMPLEMENTATION=\""
+ this.GetFilePath() + "combobox.htc" + "\" />");
writer.WriteLine();
writer.WriteBeginTag(TagNamespace + ":" + ComboBoxTagName);
this.RenderAttributes(writer);
writer.Write(">");
}
/// <summary>
/// This member overrides Control.RenderChildren.
/// </summary>
/// <param name="writer"></param>
protected override void RenderChildren(HtmlTextWriter writer) {
ListItem item;
writer.WriteLine();
writer.Indent = writer.Indent + 1;
if (this.Items.Count > 0) {
for (int i=0; i < this.Items.Count; i++) {
item = this.Items[i];
writer.WriteBeginTag("option");
if (item.Selected) {
writer.WriteAttribute("selected", "true");
}
writer.WriteAttribute("value", item.Value, true);
item.Attributes.Render(writer);
writer.Write('>');
HttpUtility.HtmlEncode(item.Text, writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}
writer.Indent = writer.Indent - 1;
}
/// <summary>
/// This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
/// </summary>
/// <param name="writer"></param>
protected virtual void RenderAttributes(HtmlTextWriter writer) {
if (base.ID != null) {
writer.WriteAttribute("id", base.ClientID);
writer.WriteAttribute("name", this.UniqueID);
}
if (!this.Enabled)
writer.WriteAttribute("Enabled","false");
if (this.AutoValidate)
writer.WriteAttribute("AutoValidate","true");
writer.WriteAttribute("ParentFormID", this.ClientFormID);
writer.WriteAttribute("ResourcesDirectory",this.GetFilePath());
if (this.AutoPostBack) {
string sOnChangeCmd = this.Attributes["onchange"];
if (sOnChangeCmd != null) {
this.Attributes["onchange"] = (sOnChangeCmd.EndsWith(";")) ? sOnChangeCmd + Page.GetPostBackEventReference(this, String.Empty)+ ";" : sOnChangeCmd + ";" + Page.GetPostBackEventReference(this, String.Empty);
} else {
this.Attributes.Add("onchange", Page.GetPostBackEventReference(this, String.Empty));
}
}
this.Attributes.Render(writer);
}
/// <summary>
/// This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
/// </summary>
/// <param name="writer"></param>
public override void RenderEndTag(HtmlTextWriter writer) {
writer.WriteEndTag(TagNamespace + ":" + ComboBoxTagName);
}
#endregion
/// <summary>
/// Overridden. Creates an EmptyControlCollection to prevent controls from
/// being added to the ControlCollection.
/// </summary>
/// <returns>An EmptyControlCollection object.</returns>
protected override ControlCollection CreateControlCollection() {
return new EmptyControlCollection(this);
}
/// <summary>
/// This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
/// </summary>
protected virtual void ClearSelection() {
foreach (ListItem item in this.Items) {
item.Selected = false;
}
}
/// <summary>
/// This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
/// </summary>
/// <param name="index"></param>
protected virtual void Select(int index) {
if (index < -1 || index >= this.Items.Count)
throw new ArgumentOutOfRangeException("index");
else {
this.ClearSelection();
this.Items[index].Selected = true;
}
}
#region PostBack Methods
public virtual void RaisePostDataChangedEvent() {
this.OnServerChange(EventArgs.Empty);
}
public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) {
string sValue = this.Value;
string sPostedValue = postCollection.GetValues(postDataKey)[0];
if (sPostedValue != null && !sValue.Equals(sPostedValue)) {
this.Value = sPostedValue;
return true;
}
return false;
}
protected override void LoadViewState(object savedState) {
if (savedState != null) {
object[] State = (object[])savedState;
this.Value = (string) State[0];
ArrayList ItemsList = (ArrayList) State[1];
foreach (object obj in ItemsList) {
Triplet trip = (Triplet) obj;
if (this.Items.FindByValue((string)trip.First)==null) {
ListItem item = new ListItem();
item.Value = (string) trip.First;
item.Text = (string) trip.Second;
item.Selected = (bool) trip.Third;
this.Items.Add(item);
}
}
}
}
protected override object SaveViewState() {
ArrayList ItemsList = new ArrayList();
foreach (ListItem item in this.Items) {
ItemsList.Add(new Triplet(item.Value, item.Text, item.Selected));
}
object[] savedState = new object[2];
savedState[0] = this.Value;
savedState[1] = ItemsList;
return savedState;
}
#endregion
/// <summary>
/// This member overrides Control.OnPreRender
/// </summary>
/// <param name="e"></param>
protected override void OnPreRender(EventArgs e) {
if (base.Page != null) {
base.Page.RegisterRequiresPostBack(this);
}
}
/// <summary>
/// Raises the ServerChange event of the ComboBox control. This allows you to provide a custom handler for the event.
/// </summary>
/// <param name="e">A System.EventArgs that contains the event data.</param>
protected virtual void OnServerChange(EventArgs e) {
EventHandler handler = (EventHandler) this.Events[this.EventServerChange];
if (handler != null)
handler.Invoke(this, e);
}
/// <summary>
/// This member overrides Control.OnDataBinding.
/// </summary>
/// <param name="e">A System.EventArgs that contains the event data.</param>
protected override void OnDataBinding(EventArgs e) {
base.OnDataBinding(e);
IEnumerable eDataSource = GetResolvedDataSource();
if (eDataSource != null) {
this.Items.Clear();
if (eDataSource is ICollection) {
this.Items.Capacity = ((ICollection)eDataSource).Count;
}
try {
for(IEnumerator ie = eDataSource.GetEnumerator() ; ie.MoveNext();) {
ListItem item = new ListItem();
item.Text = (this.DataTextField.Length > 0)?DataBinder.GetPropertyValue(ie.Current, this.DataTextField, null):ie.Current.ToString();
item.Value = (this.DataValueField.Length > 0)?DataBinder.GetPropertyValue(ie.Current, this.DataValueField, null):ie.Current.ToString();
this.Items.Add(item);
}
}
catch {}
}
if (this.cachedSelectedIndex != -1) {
this.SelectedIndex = this.cachedSelectedIndex;
this.cachedSelectedIndex = -1;
}
}
#region Private Methods
private IEnumerable GetResolvedDataSource() {
if (this.DataSource == null) {
return null;
}
if (this.DataSource is IListSource) {
IListSource iListSource = (IListSource)this.DataSource;
IList iList = iListSource.GetList();
if (!iListSource.ContainsListCollection) {
return iList;
}
if (iList != null && iList is ITypedList) {
ITypedList iTypeList = ((ITypedList) iList);
PropertyDescriptorCollection collection = iTypeList.GetItemProperties(new PropertyDescriptor[0]);
if ((collection != null) && (collection.Count != 0)) {
PropertyDescriptor discriptor = null;
if ((this.DataMember == null) || (this.DataMember.Length == 0)) {
discriptor = collection[0];
}
else {
discriptor = collection.Find(this.DataMember, true);
}
if (discriptor != null) {
object key = iList[0];
object val = discriptor.GetValue(key);
if ((val != null) && val is IEnumerable) {
return ((IEnumerable) val);
}
}
throw new HttpException("The datasource does not contain a member named " + this.DataMember);
}
throw new HttpException("Datasource does not contain any members.");
}
}
if (this.DataSource is IEnumerable) {
return ((IEnumerable) this.DataSource);
}
return null;
}
private string GetFilePath() {
// Look at the current configuration for the path
string sFilePath = ConfigurationSettings.AppSettings["PROGWEBCONTORLS_COMMONFILEPATH"];
if (sFilePath != null) {
if (sFilePath.Length > 0) {
if (sFilePath[sFilePath.Length - 1] != '/') {
sFilePath += "/";
}
}
return sFilePath;
}
// Return the default path with version number
System.Reflection.Assembly assembly = typeof(ComboBox).Assembly;
Version version = assembly.GetName().Version;
return DefaultCommonFilesRoot + version.Major.ToString() + "_" + version.Minor.ToString() + "/";
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -