/// <summary> /// This method is supposed to be called everytime the suggested list changes, /// usually when the SelectedValueChanged event is fired /// </summary> public override void DataBind() { HtmlTextWriter writer = new HtmlTextWriter(new StringWriter()); int i = 0; if (DataSource != null) { //Header template if (HeaderTemplate != null) { AutoSuggestItem item = new AutoSuggestItem(-1, "", AutoSuggestItemType.Header); HeaderTemplate.InstantiateIn(item); //Fires the ItemDataBound event if(ItemDataBound != null) ItemDataBound(this, new AutoSuggestItemEventArgs(item)); if (item.Controls.Count > 0) { foreach (Control control in item.Controls) { control.RenderControl(writer); } } } //ItemTemplate IEnumerator dataEnum = DataSource.GetEnumerator(); while (dataEnum.MoveNext()) { // Find out if its an alternating item AutoSuggestItemType itemType = (i + 1) % 2 == 0 ? AutoSuggestItemType.AlternatingItem : AutoSuggestItemType.Item; //Creates the item AutoSuggestItem item = new AutoSuggestItem(i, dataEnum.Current, itemType); if(this.ItemTemplate != null) this.ItemTemplate.InstantiateIn(item); HtmlTextWriter w = new HtmlTextWriter(new StringWriter()); AutoSuggestMenuItem asbItem = new AutoSuggestMenuItem(); asbItem.CSSClass = this.MenuItemCSSClass; item.DataBind(); //Fires the ItemDataBound event before rendering the item if (ItemDataBound != null) ItemDataBound(this, new AutoSuggestItemEventArgs(item)); item.RenderControl(w); asbItem.Text = w.InnerWriter.ToString(); if (DataKeyField != "") asbItem.Value = GetField(item, this.DataKeyField); asbItem.TextBoxText = GetField(item, this.TextBoxDisplayField); writer.WriteLine(asbItem.GenHtml(i + 1, this.TextBoxID)); this.Controls.Add(item); i++; } } string html = ""; if (i > 0) { html = string.Format("<table style=\"width:{0}\"><tr><td>{1}</td></tr></table>", this.Width.ToString(), writer.InnerWriter.ToString()); } else { html = string.Format("<table style=\"width:{0}\"><tr><td align=\"center\" style=\"color:red;\"><b>{1}</b></td></tr></table>", this.Width.ToString(), this.ItemNotFoundMessage); } StringBuilder sb = new StringBuilder(); Manager.WriteValue(sb, html); string script = string.Format("asbGetObj('{0}').ShowMenuDiv({1});", this.TextBoxID, sb.ToString()); Manager.AddScriptForClientSideEval(script); }
private string GetField(AutoSuggestItem item, string field) { if (item.DataItem is DataRowView) return ((DataRowView)item.DataItem)[field].ToString(); else if (item.DataItem is DataRow) return ((DataRow)item.DataItem)[field].ToString(); else if (item.DataItem is ICollection) { Type type = item.DataItem.GetType(); MethodInfo m = type.GetMethod("get_Item"); return m.Invoke(item.DataItem, new object[] { field }).ToString(); } else { //Here we use reflection to get the value of the any field of the defined //in the DataItem property of AutoSuggestItem Type type = item.DataItem.GetType(); FieldInfo fieldInfo = type.GetField(field); if (fieldInfo != null) return fieldInfo.GetValue(item.DataItem).ToString(); else { PropertyInfo prop = type.GetProperty(field); if (prop != null) return prop.GetValue(item.DataItem, null).ToString(); } } throw new Exception("The datasource type is not supported"); }
public AutoSuggestItemEventArgs(AutoSuggestItem item) { this._item = item; }