/// <summary> /// 绑定列表框 /// </summary> /// <param name="list">列表框</param> /// <param name="textField">显示内容</param> /// <param name="valueField"></param> /// <param name="strWhere"></param> /// <param name="strErrMsg"></param> public void GetListBox(System.Web.UI.WebControls.ListBox list, string textField, string valueField, string strWhere, out string strErrMsg) { list.DataSource = aedal.GetDropDownList(textField, valueField, strWhere, out strErrMsg); list.DataTextField = textField; list.DataValueField = valueField; list.DataBind(); }
public static void FillComboWithTiposContrato(System.Web.UI.WebControls.ListBox cbo) { cbo.Items.Clear(); cbo.Items.Add(new System.Web.UI.WebControls.ListItem("Normal", "1")); cbo.Items.Add(new System.Web.UI.WebControls.ListItem("Carência", "4")); cbo.Items.Add(new System.Web.UI.WebControls.ListItem("Migração", "3")); cbo.Items.Add(new System.Web.UI.WebControls.ListItem("Administrativa", "2")); cbo.Items.Add(new System.Web.UI.WebControls.ListItem("Especial", "5")); }
//SelectDataBind public static void SelectDataBind(System.Web.UI.WebControls.ListBox _listBox, string sqlCommandString, SqlParameter[] parameters, CommandType commandType, string connectionString) { DataTable dt = SelectTable(sqlCommandString, parameters, commandType, connectionString, null); _listBox.DataSource = dt; _listBox.DataTextField = dt.Columns[1].ColumnName; _listBox.DataValueField = dt.Columns[0].ColumnName; _listBox.Items.Clear(); _listBox.DataBind(); }
protected override void CreateChildControls() { base.CreateChildControls(); _innerTbx = new System.Web.UI.WebControls.TextBox(); this.Controls.Add(_innerTbx); _innerList = new System.Web.UI.WebControls.ListBox(); FillTimes(); Controls.Add(_innerList); }
/// <summary> /// 绑定无限级下拉框 /// </summary> /// <param name="ddlst">下拉控件</param> /// <param name="dt">数据表</param> /// <param name="id">值ID</param> /// <param name="parentid">父节点ID</param> /// <param name="name">文本</param> public static void CreateLevelDropDown(System.Web.UI.WebControls.ListBox lib, DataTable dt, string id, string parentid, string name, string pVal) { System.Collections.ArrayList allItems = new System.Collections.ArrayList(); DataRow[] rows = dt.Select(parentid + "=" + pVal); foreach (DataRow row in rows) { CreateLevelDropDownAssistant(dt, ref allItems, row, string.Empty, id, parentid, name); } System.Web.UI.WebControls.ListItem[] items = new System.Web.UI.WebControls.ListItem[allItems.Count]; allItems.CopyTo(items); lib.Items.AddRange(items); }
public static bool SafeSelect(ref System.Web.UI.WebControls.ListBox objList, string strValue) { foreach (System.Web.UI.WebControls.ListItem objItem in objList.Items) { if (objItem.Value == strValue) { objItem.Selected = true; objList.SelectedValue = strValue; return(true); } } return(false); }
public static void BindListbox(ref System.Web.UI.WebControls.ListBox ListBoxControl, DataTable dt, string DataTextField, string DataValueField, params string[] SelectedValues) { ListBoxControl.DataTextField = DataTextField; ListBoxControl.DataValueField = DataValueField; ListBoxControl.DataSource = dt; ListBoxControl.DataBind(); for (int i = 0; i < SelectedValues.Length; i++) { for (int j = 0; j < ListBoxControl.Items.Count; j++) { ListBoxControl.Items[j].Selected = (ListBoxControl.Items[j].Value == SelectedValues[i].ToString()); } } }
/// <summary> /// LÝSTBOXTAKÝLERÝ DATATABLE A AKTARIR /// </summary> /// <param name="lb"></param> /// <returns></returns> public static DataTable ListboxToDataTable(System.Web.UI.WebControls.ListBox lb) { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn(lb.DataValueField.ToString())); dt.Columns.Add(new DataColumn(lb.DataTextField.ToString())); DataRow dr; foreach (System.Web.UI.WebControls.ListItem li in lb.Items) { if (li.Selected) { dr = dt.NewRow(); dr[lb.DataValueField.ToString()] = li.Value; dr[lb.DataTextField.ToString()] = li.Text; dt.Rows.Add(dr); } } return(dt); }
public static void SelectDataBind(System.Web.UI.WebControls.ListBox _listBox, string sqlCommandString) { SelectDataBind(_listBox, sqlCommandString, null, CommandType.Text, MainDatabase.ConnectString); }
public static void SelectDataBind(System.Web.UI.WebControls.ListBox _listBox, string sqlCommandString, SqlParameter[] parameters) { SelectDataBind(_listBox, sqlCommandString, parameters, CommandType.Text, MainDatabase.ConnectString); }
public static void AssociateListBoxes(System.Web.UI.WebControls.ListBox sourceListBox, System.Web.UI.WebControls.ListBox targetListBox, System.Web.UI.HtmlControls.HtmlInputButton addButton, System.Web.UI.HtmlControls.HtmlInputButton removeButton ) { // Test for validity. if (sourceListBox.Page == null || targetListBox.Page == null) { throw(new ArgumentException("The control must be added to a page before " + "you can set the associations.")); } RegisterBrowserDetector(sourceListBox.Page); string sourceId = sourceListBox.ClientID; string targetId = targetListBox.ClientID; string funcPrefix = "lba_" + sourceId + targetId; addButton.Attributes.Add("onClick", funcPrefix + "_Add();"); removeButton.Attributes.Add("onClick", funcPrefix + "_Remove();"); sourceListBox.Page.ClientScript.RegisterClientScriptBlock(typeof(ControlExtender), funcPrefix, "<script language=\"javascript\"> \n" + "<!-- \n" + "function " + funcPrefix + "_Add() \n" + "{ \n" + " var f = document.forms[0]; \n" + " var idx = f." + sourceId + ".selectedIndex; \n" + " if(idx==-1) \n" + " return; \n"+ " if (NSX) \n" + " { \n" + " f."+ targetId + ".options[f." + targetId + ".length] = f." + sourceId + ".options[idx].text; \n" + " f."+ targetId + ".options[idx] = null; \n" + " } \n" + " else if (IE4) \n" + " { \n" + " var newOpt = document.createElement(\"OPTION\"); \n"+ " newOpt.text = f."+ sourceId + ".options[idx].text; \n" + " newOpt.value = f."+ sourceId + ".options[idx].value; \n" + " f."+ targetId + ".add(newOpt); \n" + " } \n" + " f." + sourceId + ".options.remove(idx); \n" + "} \n" + "function " + funcPrefix + "_Remove() \n" + "{ \n" + " var f = document.forms[0]; \n" + " var idx = f." + targetId + ".selectedIndex; \n" + " if(idx==-1) \n" + " return; \n"+ " if (NSX) \n" + " { \n" + " f."+ sourceId + ".options[f." + sourceId + ".length] = f." + targetId + ".options[idx].text; \n" + " f."+ sourceId + ".options[idx] = null; \n" + " } \n" + " else if (IE4) \n" + " { \n" + " var newOpt = document.createElement(\"OPTION\"); \n"+ " newOpt.text = f."+ targetId + ".options[idx].text; \n" + " newOpt.value = f."+ targetId + ".options[idx].value; \n" + " f."+ sourceId + ".add(newOpt); \n" + " } \n" + " f." + targetId + ".options.remove(idx); \n" + "} \n" + "//--> \n" + "</script> \n"); }
/// <summary> /// 清理表单 /// </summary> /// <param name="control"></param> public static void ClearForm(WebUI.Control control) { if (control == null) { return; } foreach (WebUI.Control ctl in control.Controls) { Type type = ctl.GetType(); #region 处理服务器控件 if (type == typeof(WebUI.WebControls.TextBox))//文本框 { WebUI.WebControls.TextBox box = ((WebUI.WebControls.TextBox)ctl); box.Text = ""; if (box.Attributes["isNumber"] != null) { box.Text = "0"; } } else if (type == typeof(WebUI.WebControls.DropDownList))//选择框 { ((WebUI.WebControls.DropDownList)ctl).SelectedIndex = -1; } else if (type == typeof(WebUI.WebControls.HiddenField))//隐藏域 { ((WebUI.WebControls.HiddenField)ctl).Value = ""; } else if (type == typeof(WebUI.WebControls.RadioButton))//单选框 { WebUI.WebControls.RadioButton rb = (WebUI.WebControls.RadioButton)ctl; rb.Checked = false; } else if (type == typeof(WebUI.WebControls.CheckBox))//复选框 { WebUI.WebControls.CheckBox ck = (WebUI.WebControls.CheckBox)ctl; ck.Checked = false; } else if (type == typeof(WebUI.WebControls.CheckBoxList))//复选框列表 { WebUI.WebControls.CheckBoxList ck = (WebUI.WebControls.CheckBoxList)ctl; foreach (WebUI.WebControls.ListItem li in ck.Items) { li.Selected = false; } } else if (type == typeof(WebUI.WebControls.RadioButtonList))//单框列表 { WebUI.WebControls.RadioButtonList ck = (WebUI.WebControls.RadioButtonList)ctl; foreach (WebUI.WebControls.ListItem li in ck.Items) { li.Selected = false; } } else if (type == typeof(WebUI.WebControls.ListBox))//列表框 { WebUI.WebControls.ListBox ck = (WebUI.WebControls.ListBox)ctl; foreach (WebUI.WebControls.ListItem li in ck.Items) { li.Selected = false; } } #endregion #region 处理不同Html控件 else if (type == typeof(WebUI.HtmlControls.HtmlInputText))//文本域 { WebUI.HtmlControls.HtmlInputText ct = (WebUI.HtmlControls.HtmlInputText)ctl; ct.Value = ""; if (ct.Attributes["isNumber"] != null) { ct.Value = "0"; } } else if (type == typeof(WebUI.HtmlControls.HtmlTextArea))//文本域 { WebUI.HtmlControls.HtmlTextArea ct = (WebUI.HtmlControls.HtmlTextArea)ctl; ct.Value = ""; } else if (type == typeof(WebUI.HtmlControls.HtmlSelect))//选择域 { WebUI.HtmlControls.HtmlSelect ct = (WebUI.HtmlControls.HtmlSelect)ctl; ct.SelectedIndex = -1; } else if (type == typeof(WebUI.HtmlControls.HtmlInputHidden))////隐藏域 { WebUI.HtmlControls.HtmlInputHidden ct = (WebUI.HtmlControls.HtmlInputHidden)ctl; ct.Value = ""; if (ct.Attributes["isNumber"] != null) { ct.Value = "0"; } } else if (type == typeof(WebUI.HtmlControls.HtmlInputRadioButton))//单选域 { WebUI.HtmlControls.HtmlInputRadioButton rb = (WebUI.HtmlControls.HtmlInputRadioButton)ctl; rb.Checked = false; } else if (type == typeof(WebUI.HtmlControls.HtmlInputCheckBox))//复选域 { WebUI.HtmlControls.HtmlInputCheckBox ck = (WebUI.HtmlControls.HtmlInputCheckBox)ctl; ck.Checked = false; } else if (type == typeof(WebUI.HtmlControls.HtmlInputPassword))//密码域 { WebUI.HtmlControls.HtmlInputPassword ck = (WebUI.HtmlControls.HtmlInputPassword)ctl; ck.Value = ""; } #endregion } }
/// <summary> /// 填充model /// </summary> /// <param name="entity"></param> /// <param name="control"></param> public static void FillModel(Object entity, WebUI.Control control) { if (entity == null || control == null) { return; } NameValueCollection formData = HttpContext.Current.Request.Form; PropertyInfo[] propertyList = entity.GetProperties(); foreach (PropertyInfo pi in propertyList) { string ctlId = string.Format(IdFormat, pi.Name); WebUI.Control ctl = control.FindControl(ctlId); if (ctl == null) { #region 处理HMTL标签 if (formData[ctlId] != null) { entity.SetPropertyValue(pi.Name, formData[ctlId]); } #endregion continue; } Type ctlType = ctl.GetType(); #region 处理服务器控件 if (ctlType == typeof(WebUI.WebControls.TextBox))//文本框 { entity.SetPropertyValue(pi.Name, ((WebUI.WebControls.TextBox)ctl).Text); } else if (ctlType == typeof(WebUI.WebControls.Image))//图片 { entity.SetPropertyValue(pi.Name, ((WebUI.WebControls.Image)ctl).ImageUrl); } else if (ctlType == typeof(WebUI.WebControls.DropDownList))//选择框 { entity.SetPropertyValue(pi.Name, ((WebUI.WebControls.DropDownList)ctl).SelectedValue); } else if (ctlType == typeof(WebUI.WebControls.HiddenField))//隐藏域 { entity.SetPropertyValue(pi.Name, ((WebUI.WebControls.HiddenField)ctl).Value); } else if (ctlType == typeof(WebUI.WebControls.RadioButton))//单选框 { WebUI.WebControls.RadioButton rb = (WebUI.WebControls.RadioButton)ctl; if (rb.Checked) { entity.SetPropertyValue(pi.Name, rb.Text); } else { entity.SetPropertyValue(pi.Name, ""); } } else if (ctlType == typeof(WebUI.WebControls.CheckBox))//复选框 { WebUI.WebControls.CheckBox ck = (WebUI.WebControls.CheckBox)ctl; if (ck.Checked) { entity.SetPropertyValue(pi.Name, ck.Text); } else { entity.SetPropertyValue(pi.Name, ""); } } else if (ctlType == typeof(WebUI.WebControls.CheckBoxList))//复选框列表 { WebUI.WebControls.CheckBoxList ck = (WebUI.WebControls.CheckBoxList)ctl; string rs = ""; foreach (WebUI.WebControls.ListItem li in ck.Items) { if (li.Selected) { rs += "," + li.Value; } } if (rs.Length > 1) { rs = rs.Substring(1); } entity.SetPropertyValue(pi.Name, rs); } else if (ctlType == typeof(WebUI.WebControls.RadioButtonList))//单框列表 { WebUI.WebControls.RadioButtonList ck = (WebUI.WebControls.RadioButtonList)ctl; entity.SetPropertyValue(pi.Name, ck.SelectedValue); } else if (ctlType == typeof(WebUI.WebControls.ListBox))//列表框 { WebUI.WebControls.ListBox ck = (WebUI.WebControls.ListBox)ctl; string rs = ""; foreach (WebUI.WebControls.ListItem li in ck.Items) { if (li.Selected) { rs += "," + li.Value; } } if (rs.Length > 1) { rs = rs.Substring(1); } entity.SetPropertyValue(pi.Name, rs); } #endregion #region 处理不同Html控件 else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputText))//文本域 { WebUI.HtmlControls.HtmlInputText ct = (WebUI.HtmlControls.HtmlInputText)ctl; entity.SetPropertyValue(pi.Name, ct.Value); } else if (ctlType == typeof(WebUI.HtmlControls.HtmlTextArea))//文本域 { WebUI.HtmlControls.HtmlTextArea ct = (WebUI.HtmlControls.HtmlTextArea)ctl; entity.SetPropertyValue(pi.Name, ct.Value); } else if (ctlType == typeof(WebUI.HtmlControls.HtmlSelect))//选择域 { WebUI.HtmlControls.HtmlSelect ct = (WebUI.HtmlControls.HtmlSelect)ctl; entity.SetPropertyValue(pi.Name, ct.Items[ct.SelectedIndex].Value); } else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputHidden))////隐藏域 { WebUI.HtmlControls.HtmlInputHidden ct = (WebUI.HtmlControls.HtmlInputHidden)ctl; entity.SetPropertyValue(pi.Name, ct.Value); } else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputRadioButton))//单选域 { WebUI.HtmlControls.HtmlInputRadioButton rb = (WebUI.HtmlControls.HtmlInputRadioButton)ctl; if (rb.Checked) { entity.SetPropertyValue(pi.Name, rb.Value); } } else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputCheckBox))//复选域 { WebUI.HtmlControls.HtmlInputCheckBox ck = (WebUI.HtmlControls.HtmlInputCheckBox)ctl; if (ck.Checked) { entity.SetPropertyValue(pi.Name, ck.Value); } } else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputPassword))//密码域 { WebUI.HtmlControls.HtmlInputPassword ck = (WebUI.HtmlControls.HtmlInputPassword)ctl; entity.SetPropertyValue(pi.Name, ck.Value); } #endregion } }
/// <summary> /// 填充表单 /// </summary> /// <param name="entity"></param> /// <param name="control"></param> public static void FillForm(WebUI.Control control, Object entity) { if (entity == null || control == null) { return; } PropertyInfo[] propertyList = entity.GetProperties(); foreach (PropertyInfo pi in propertyList) { WebUI.Control ctl = control.FindControl(string.Format(IdFormat, pi.Name)); if (ctl == null) { continue; } Type ctlType = ctl.GetType(); #region 处理服务器控件 if (ctlType == typeof(WebUI.WebControls.TextBox))//文本框 { if (entity.GetPropertyValue(pi.Name) != null) { ((WebUI.WebControls.TextBox)ctl).Text = entity.GetPropertyValue(pi.Name).ToString(); } } else if (ctlType == typeof(WebUI.WebControls.Image))//图片 { if (entity.GetPropertyValue(pi.Name) != null) { string imageUrl = entity.GetPropertyValue(pi.Name).ToString(); if (!string.IsNullOrEmpty(imageUrl)) { ((WebUI.WebControls.Image)ctl).ImageUrl = imageUrl; } } } else if (ctlType == typeof(WebUI.WebControls.DropDownList))//选择框 { if (entity.GetPropertyValue(pi.Name) != null) { ((WebUI.WebControls.DropDownList)ctl).SelectedValue = entity.GetPropertyValue(pi.Name).ToString(); } } else if (ctlType == typeof(WebUI.WebControls.HiddenField))//隐藏域 { if (entity.GetPropertyValue(pi.Name) != null) { ((WebUI.WebControls.HiddenField)ctl).Value = entity.GetPropertyValue(pi.Name).ToString(); } } else if (ctlType == typeof(WebUI.WebControls.RadioButton))//单选框 { WebUI.WebControls.RadioButton rb = (WebUI.WebControls.RadioButton)ctl; if (entity.GetPropertyValue(pi.Name) != null) { rb.Checked = entity.GetPropertyValue(pi.Name).ToString() == rb.Text ? true : false; } } else if (ctlType == typeof(WebUI.WebControls.CheckBox))//复选框 { WebUI.WebControls.CheckBox ck = (WebUI.WebControls.CheckBox)ctl; if (entity.GetPropertyValue(pi.Name) != null) { ck.Checked = entity.GetPropertyValue(pi.Name).ToString() == ck.Text ? true : false; } } else if (ctlType == typeof(WebUI.WebControls.CheckBoxList))//复选框列表 { WebUI.WebControls.CheckBoxList ck = (WebUI.WebControls.CheckBoxList)ctl; if (entity.GetPropertyValue(pi.Name) != null) { string sel = entity.GetPropertyValue(pi.Name).ToString(); foreach (WebUI.WebControls.ListItem li in ck.Items) { if (sel.IndexOf(",") > -1 && (sel.IndexOf(li.Value + ",") > -1 || sel.IndexOf("," + li.Value) > -1)) { li.Selected = true; } else if (sel.IndexOf(",") == -1 && sel == li.Value) { li.Selected = true; } else { li.Selected = false; } } } } else if (ctlType == typeof(WebUI.WebControls.RadioButtonList))//单框列表 { WebUI.WebControls.RadioButtonList ck = (WebUI.WebControls.RadioButtonList)ctl; if (entity.GetPropertyValue(pi.Name) != null) { ck.SelectedValue = entity.GetPropertyValue(pi.Name).ToString(); } } else if (ctlType == typeof(WebUI.WebControls.ListBox))//列表框 { WebUI.WebControls.ListBox ck = (WebUI.WebControls.ListBox)ctl; if (entity.GetPropertyValue(pi.Name) != null) { string sel = entity.GetPropertyValue(pi.Name).ToString(); foreach (WebUI.WebControls.ListItem li in ck.Items) { if (sel.IndexOf(",") > -1 && (sel.IndexOf(li.Value + ",") > -1 || sel.IndexOf("," + li.Value) > -1)) { li.Selected = true; } else if (sel.IndexOf(",") == -1 && sel == li.Value) { li.Selected = true; } else { li.Selected = false; } } } } #endregion #region 处理不同Html控件 else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputText))//文本域 { WebUI.HtmlControls.HtmlInputText ct = (WebUI.HtmlControls.HtmlInputText)ctl; if (entity.GetPropertyValue(pi.Name) != null) { ct.Value = entity.GetPropertyValue(pi.Name).ToString(); } } else if (ctlType == typeof(WebUI.HtmlControls.HtmlTextArea))//文本域 { WebUI.HtmlControls.HtmlTextArea ct = (WebUI.HtmlControls.HtmlTextArea)ctl; if (entity.GetPropertyValue(pi.Name) != null) { ct.Value = entity.GetPropertyValue(pi.Name).ToString(); } } else if (ctlType == typeof(WebUI.HtmlControls.HtmlSelect))//选择域 { WebUI.HtmlControls.HtmlSelect ct = (WebUI.HtmlControls.HtmlSelect)ctl; if (entity.GetPropertyValue(pi.Name) != null) { for (int i = 0; i < ct.Items.Count; i++) { if (ct.Items[i].Value == entity.GetPropertyValue(pi.Name).ToString()) { ct.SelectedIndex = i; } } } } else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputHidden))////隐藏域 { WebUI.HtmlControls.HtmlInputHidden ct = (WebUI.HtmlControls.HtmlInputHidden)ctl; if (entity.GetPropertyValue(pi.Name) != null) { ct.Value = entity.GetPropertyValue(pi.Name).ToString(); } } else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputRadioButton))//单选域 { WebUI.HtmlControls.HtmlInputRadioButton rb = (WebUI.HtmlControls.HtmlInputRadioButton)ctl; if (rb.Checked && entity.GetPropertyValue(pi.Name) != null) { rb.Checked = entity.GetPropertyValue(pi.Name).ToString() == rb.Value ? true : false; } } else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputCheckBox))//复选域 { WebUI.HtmlControls.HtmlInputCheckBox ck = (WebUI.HtmlControls.HtmlInputCheckBox)ctl; if (entity.GetPropertyValue(pi.Name) != null) { if (entity.GetPropertyValue(pi.Name).ToString().IndexOf("," + ck.Value) != -1) { ck.Checked = true; } } } else if (ctlType == typeof(WebUI.HtmlControls.HtmlInputPassword))//密码域 { WebUI.HtmlControls.HtmlInputPassword ck = (WebUI.HtmlControls.HtmlInputPassword)ctl; if (entity.GetPropertyValue(pi.Name) != null) { ck.Value = entity.GetPropertyValue(pi.Name).ToString(); } } #endregion } }