C# ASP.NET中复合控件的设计时错误
我已经创建了一个复合控件。其中没有生成错误 在复合控件中,我使用的是实体框架对象 问题是,在设计窗口中,我得到了错误消息,如图所示: 在构建过程中没有错误,复合控件工作良好 我只想从设计时间窗口中删除此错误 编辑: 这就像我添加复合控件的方式:C# ASP.NET中复合控件的设计时错误,c#,asp.net,composite,C#,Asp.net,Composite,我已经创建了一个复合控件。其中没有生成错误 在复合控件中,我使用的是实体框架对象 问题是,在设计窗口中,我得到了错误消息,如图所示: 在构建过程中没有错误,复合控件工作良好 我只想从设计时间窗口中删除此错误 编辑: 这就像我添加复合控件的方式: <tr> <td colspan="3"> <cc1:AdditionalColumnsCustomControl ID="AdditionalColumnsCustomCon
<tr>
<td colspan="3">
<cc1:AdditionalColumnsCustomControl ID="AdditionalColumnsCustomControl1" runat="server"
MainCssClass="A" ControlMode="New" TableID="1" AreValidatorsStatic="true"
CustomControlWrapper="TableWrapper" ValidationGroup="vl" />
</td>
</tr>
在这种情况下,我如何编写代码来消除这种设计表面错误
复合控件源代码:
public class AddRowCustomControl : CompositeControl
{
public void Clear()
{
Control c = (Control)FindControl(controlID);
if (c.GetType() == typeof(DropDownList))
{
DropDownList dl = c as DropDownList;
ListItem lsa = dl.SelectedItem;
lsa.Selected = false;
}
else if (c.GetType() == typeof(ListBox))
{
ListBox lb = c as ListBox;
foreach (ListItem ls in lb.Items)
{
if (ls.Selected)
{
ls.Selected = false;
}
}
}
else if (c.GetType() == typeof(RadioButtonList))
{
RadioButtonList rl = c as RadioButtonList;
foreach (ListItem ls in rl.Items)
{
if (ls.Selected)
{
ls.Selected = false;
}
}
}
else if (c.GetType() == typeof(CheckBoxList))
{
CheckBoxList cl = c as CheckBoxList;
foreach (ListItem ls in cl.Items)
{
if (ls.Selected)
{
ls.Selected = false;
}
}
}
else if (c.GetType() == typeof(CheckBox))
{
CheckBox chk = c as CheckBox;
chk.Checked = false;
}
else
{
TextBox tx = c as TextBox;
tx.Text = "";
}
}
public void Rebind()
{
Control c = (Control)FindControl(controlID);
fillControl(ref c , RowID , ColumnID);
}
public string SaveControlValuesInDB()
{
using (ExtEntities context = new ExtEntities())
{
if (RowID != null && ColumnID != null)
{
EntityData ed = context.EntityDatas.Where(p => p.ColID == ColumnID && p.RowID == RowID).FirstOrDefault();
if (ed == null)
{
ed = new EntityData();
ed.RowID = Convert.ToInt32(RowID);
ed.ColID = ColumnID;
ed.ColValue = ControlValue;
context.AddToEntityDatas(ed);
context.SaveChanges();
return "Successfully Added";
}
else
{
ed.ColValue = ControlValue;
context.SaveChanges();
return "Successfully Updated";
}
}
else
{
return "Exception Invalid Row ID";
}
}
}
public int? RowID
{
get;
set;
}
public enum Modes
{
New = 0,
Modify = 1,
ModifyInGrid = 2,
}
public Modes? ControlMode
{
get;
set;
}
public int ColumnID
{
get;
set;
}
public string ValidationGroup
{
get;
set;
}
/// <summary>
/// Specifes the Display mode of the Validators "Static or Dynamic"
/// </summary>
public bool? AreValidatorsStatic
{
get;
set;
}
/// <summary>
/// If true, the wrapper will be DIV's else Table
/// </summary>
public Wrapper CustomControlWrapper
{
get;
set;
}
public enum Wrapper
{
DivWrapper = 0,
TableWrapper = 1,
}
/// <summary>
/// Css Class Name for Custom Control
/// </summary>
public string MainCssClass
{
get;
set;
}
/// <summary>
/// Css Class Name for Custom Control Label
/// </summary>
public string LabelCssClass
{
get;
set;
}
/// <summary>
/// Css Class Name for Custom Control's Main Control
/// </summary>
public string ControlCssClass
{
get;
set;
}
/// <summary>
/// Css Class Name for Custom Control's Validators
/// </summary>
public string ValidatorCssClass
{
get;
set;
}
protected override void OnLoad(EventArgs e)
{
if (AreValidatorsStatic == null)
{
AreValidatorsStatic = false;
}
if (CustomControlWrapper == null)
{
CustomControlWrapper = Wrapper.DivWrapper;
}
if (string.IsNullOrEmpty(MainCssClass))
{
MainCssClass = "CustomControlMainClass";
}
if (string.IsNullOrEmpty(ControlCssClass))
{
ControlCssClass = "ControlCssClass";
}
if (string.IsNullOrEmpty(LabelCssClass))
{
LabelCssClass = "LabelCssClass";
}
if (string.IsNullOrEmpty(ValidatorCssClass))
{
ValidatorCssClass = "ValidatorCssClass";
}
if (ControlMode == null)
{
ControlMode = Modes.New;
}
base.OnLoad(e);
}
string controlID = "ControlID";
public string ControlValue
{
get
{
Control c = (Control)FindControl(controlID);
StringBuilder sb = new StringBuilder();
if (c.GetType() == typeof(DropDownList))
{
DropDownList dl = c as DropDownList;
sb.Append(dl.Text);
return sb.ToString();
}
else if (c.GetType() == typeof(ListBox))
{
ListBox lb = c as ListBox;
foreach (ListItem item in lb.Items)
{
if (item.Selected)
{
sb.Append(item.Text + "`");
}
}
return sb.ToString().TrimEnd('`');
}
else if (c.GetType() == typeof(RadioButtonList))
{
RadioButtonList rl = c as RadioButtonList;
sb.Append(rl.SelectedItem.Value);
return sb.ToString();
}
else if (c.GetType() == typeof(CheckBoxList))
{
CheckBoxList cl = c as CheckBoxList;
foreach (ListItem li in cl.Items)
{
if (li.Selected == true)
{
sb.Append(li.Text + "`");
}
}
return sb.ToString().TrimEnd('`');
}
else if (c.GetType() == typeof(CheckBox))
{
CheckBox chk = c as CheckBox;
return chk.Checked ? "TRUE" : "";
}
else
{
TextBox tx = c as TextBox;
return tx.Text;
}
}
}
private void AddOptions(string[] options, ref Control c)
{
if (c.GetType() == typeof(DropDownList))
{
DropDownList dl = c as DropDownList;
foreach (string item in options)
{
dl.Items.Add(item);
}
c = dl;
}
else if (c.GetType() == typeof(ListBox))
{
ListBox lb = c as ListBox;
foreach (string item in options)
{
lb.Items.Add(item);
}
c = lb;
}
else if (c.GetType() == typeof(RadioButtonList))
{
RadioButtonList rl = c as RadioButtonList;
foreach (string item in options)
{
rl.Items.Add(item);
}
c = rl;
}
else if (c.GetType() == typeof(CheckBoxList))
{
CheckBoxList cl = c as CheckBoxList;
foreach (string item in options)
{
cl.Items.Add(item);
}
c = cl;
}
}
protected override void CreateChildControls()
{
string ts = MainCssClass;
using (ExtEntities context = new ExtEntities())
{
EntityAttribute _Attribute = context.EntityAttributes.Where(p => p.ID == ColumnID).FirstOrDefault();
if (_Attribute != null)
{
Panel pnl = new Panel();
string[] _options = null;
if (_Attribute.ControlOptions != null)
{
_options = _Attribute.ControlOptions.Split('`');
}
Label lt = new Label();
lt.ID = "LabelID";
lt.Text = _Attribute.ColumnLabel;
Control c = null;
int? MaxLength = _Attribute.MaxLength;
bool Invalidate_Validator = false;
switch (_Attribute.AttributeTypeID)
{
case 1:
TextBox sl = new TextBox();
if (_Attribute.MaxLength != null)
sl.MaxLength = Convert.ToInt32(_Attribute.MaxLength);
c = sl;
break;
case 2:
TextBox ml = new TextBox();
ml.TextMode = TextBoxMode.MultiLine;
if (_Attribute.MaxLength != null)
ml.MaxLength = Convert.ToInt32(_Attribute.MaxLength);
c = ml;
break;
case 3:
DropDownList dl = new DropDownList();
c = dl;
break;
case 4:
ListBox lb = new ListBox();
lb.SelectionMode = ListSelectionMode.Multiple;
c = lb;
break;
case 5:
TextBox p = new TextBox();
p.TextMode = TextBoxMode.Password;
if (_Attribute.MaxLength != null)
p.MaxLength = Convert.ToInt32(_Attribute.MaxLength);
c = p;
break;
case 6:
CheckBox ck = new CheckBox();
Invalidate_Validator = true;
ck.Text = _options != null ? _options[0] : "N/A";
c = ck;
break;
//case 7:
// RadioButton rb = new RadioButton();
// rb.Text = _options != null ? _options[0] : "N/A";
// c = rb;
// break;
case 8:
RadioButtonList rb_list = new RadioButtonList();
c = rb_list;
break;
case 9:
CheckBoxList ck_list = new CheckBoxList();
Invalidate_Validator = true;
c = ck_list;
break;
default:
break;
}
RequiredFieldValidator req = null;
RegularExpressionValidator regx = null;
AddOptions(_options, ref c);
c.ID = controlID;
if (!Invalidate_Validator)
{
if (_Attribute.Mandatory)
{
req = new RequiredFieldValidator();
req.ControlToValidate = c.ID;
req.ErrorMessage = _Attribute.ValidationMessage;
req.Text = "*";
req.Display = AreValidatorsStatic == true ? ValidatorDisplay.Static : ValidatorDisplay.Dynamic;
req.ValidationGroup = ValidationGroup;
}
if (_Attribute.RegularExpressionValue != null)
{
regx = new RegularExpressionValidator();
regx.ControlToValidate = c.ID;
regx.ErrorMessage = _Attribute.ValidationMessage;
regx.Text = "*";
regx.ValidationGroup = ValidationGroup;
regx.ValidationExpression = _Attribute.RegularExpressionValue;
regx.Display = AreValidatorsStatic == true ? ValidatorDisplay.Static : ValidatorDisplay.Dynamic;
}
}
if (ControlMode == Modes.Modify)
{
fillControl(ref c, RowID, ColumnID);
}
if (CustomControlWrapper == Wrapper.DivWrapper)
{
pnl.Controls.Add(new LiteralControl(@"<div class=""" + MainCssClass + @"""><div class=""" + LabelCssClass + @""">"));
if (ControlMode != Modes.ModifyInGrid)
pnl.Controls.Add(lt);
pnl.Controls.Add(new LiteralControl(@"</div><div class=""" + ControlCssClass + @""">"));
pnl.Controls.Add(c);
pnl.Controls.Add(new LiteralControl(@"</div><div class=""" + ValidatorCssClass + @""">"));
if (req != null)
pnl.Controls.Add(req);
if (regx != null)
pnl.Controls.Add(regx);
pnl.Controls.Add(new LiteralControl(@"</div>"));
pnl.Controls.Add(new LiteralControl(@"</div>"));
}
else
{
pnl.Controls.Add(new LiteralControl(@"<table class=""" + MainCssClass + @"""><tr><td class=""" + LabelCssClass + @""">"));
if (ControlMode != Modes.ModifyInGrid)
pnl.Controls.Add(lt);
pnl.Controls.Add(new LiteralControl(@"</td><td class=""" + ControlCssClass + @""">"));
pnl.Controls.Add(c);
pnl.Controls.Add(new LiteralControl(@"</td><td class=""" + ValidatorCssClass + @""">"));
if (req != null)
pnl.Controls.Add(req);
if (regx != null)
pnl.Controls.Add(regx);
pnl.Controls.Add(new LiteralControl(@"</td></tr>"));
pnl.Controls.Add(new LiteralControl(@"</table>"));
}
Controls.Add(pnl);
}
}
}
private void fillControl(ref Control c, int? RowID, int ColumnID)
{
using (ExtEntities context = new ExtEntities())
{
EntityData obj = context.EntityDatas.Where(p => p.RowID == RowID && p.ColID == ColumnID).FirstOrDefault();
if (obj != null)
{
string[] values = obj.ColValue.Split('`');
string value = obj.ColValue;
if (c.GetType() == typeof(DropDownList))
{
DropDownList dl = c as DropDownList;
dl.Items.FindByText(value).Selected = true;
}
else if (c.GetType() == typeof(ListBox))
{
ListBox lb = c as ListBox;
foreach (var item in values)
{
lb.Items.FindByText(value).Selected = true;
}
}
else if (c.GetType() == typeof(RadioButtonList))
{
RadioButtonList rl = c as RadioButtonList;
foreach (var item in values)
{
rl.Items.FindByText(item).Selected = true;
}
}
else if (c.GetType() == typeof(CheckBoxList))
{
CheckBoxList cl = c as CheckBoxList;
foreach (var item in values)
{
cl.Items.FindByText(value).Selected = true;
}
}
else if (c.GetType() == typeof(CheckBox))
{
CheckBox chk = c as CheckBox;
chk.Checked = value == "TRUE" ? true : false;
}
else
{
TextBox tx = c as TextBox;
tx.Text = value;
}
}
}
}
公共类AddRowCustomControl:CompositeControl
{
公共空间清除()
{
控件c=(控件)FindControl(控件ID);
if(c.GetType()==typeof(DropDownList))
{
DropDownList dl=c作为DropDownList;
ListItem lsa=dl.SelectedItem;
lsa.Selected=false;
}
else if(c.GetType()==typeof(ListBox))
{
列表框lb=c作为列表框;
foreach(在磅项目中列出项目ls)
{
如果(已选择ls)
{
ls.Selected=false;
}
}
}
else if(c.GetType()==typeof(RadioButtonList))
{
RadioButtonList rl=c作为RadioButtonList;
foreach(rl.Items中的列表项ls)
{
如果(已选择ls)
{
ls.Selected=false;
}
}
}
else if(c.GetType()==typeof(复选框列表))
{
复选框列表cl=c作为复选框列表;
foreach(在cl.Items中列出项目ls)
{
如果(已选择ls)
{
ls.Selected=false;
}
}
}
else if(c.GetType()==typeof(复选框))
{
复选框chk=c作为复选框;
chk.Checked=假;
}
其他的
{
TextBox tx=c作为TextBox;
tx.Text=“”;
}
}
公共无效重新绑定()
{
控件c=(控件)FindControl(控件ID);
fillControl(参考c,行ID,列ID);
}
公共字符串SaveControlValuesInDB()
{
使用(扩展上下文=新扩展()
{
if(RowID!=null&&ColumnID!=null)
{
EntityData ed=context.EntityDatas.Where(p=>p.ColID==ColumnID&&p.RowID==RowID).FirstOrDefault();
如果(ed==null)
{
ed=新实体数据();
ed.RowID=Convert.ToInt32(RowID);
ed.ColID=列ID;
ed.ColValue=控制值;
context.AddToEntityDatas(ed);
SaveChanges();
返回“添加成功”;
}
其他的
{
ed.ColValue=控制值;
SaveChanges();
返回“已成功更新”;
}
}
其他的
{
返回“异常无效行ID”;
}
}
}
公共int?RowID
{
得到;
设置
}
公共枚举模式
{
新=0,
修改=1,
ModifyInGrid=2,
}
公共模式?控制模式
{
得到;
设置
}
公共int列ID
{
得到;
设置
}
公共字符串验证组
{
得到;
设置
}
///
///指定验证器的显示模式为“静态或动态”
///
公共图书馆?是静态的
{
得到;
设置
}
///
///如果为true,则包装器将是DIV的else表
///
公共包装器CustomControlWrapper
{
得到;
设置
}
公共枚举包装器
{
DivWrapper=0,
TableWrapper=1,
}
///
///自定义控件的Css类名
///
公共字符串MainCssClass
{
得到;
设置
}
///
///自定义控件标签的Css类名
///
公共字符串标签CSCLASS
{
得到;
设置
}
///
///自定义控件的主控件的Css类名
///
公共字符串控件CSCLASS
{
得到;
设置
}
///
///自定义控件的验证程序的Css类名
///
公共字符串验证器CSSClass
{
得到;
设置
}
受保护的覆盖无效加载(事件参数e)
{
if(AreValidatorsStatic==null)
{
AreValidatorsStatic=false;
}
如果(CustomControlWrapper==null)
{
CustomControlWrapper=Wrapper.DivWrapper;
}
if(string.IsNullOrEmpty(MainCssClass))
{
MainCssClass=“CustomControlMainClass”;
}
if(string.IsNullOrEmpty(ControlCssClass))
{
ControlCssClass=“ControlCssClass”;
}
if(string.IsNullOrEmpty(LabelCssClass))
{
LabelCssClass=“LabelCssClass”;
}
if(string.IsNullOrEmpty(validatorCSCLASS))
{
ValidatorCssClass=“ValidatorCssClass”;
}
if(ControlMode==null)
{
ControlMode=模式。新建;
}
基础荷载(e);
}
字符串controlID=“controlID”;
公共字符串控制值
{
得到
{
控件c=(控件)FindControl(控件ID);
StringBuilder sb=新的StringBuilder();
if(c.GetType()==typeof(DropDownList))
{
DropDownList dl=c作为DropDownList;
sb.追加(dl.文本);
使某人返回字符串();
}
else if(c.GetType()==typeof(ListBox))
if(this.DesignMode == false)
{
//do normal instantiations of objects etc.
}
else
{
//do work related to creating a design time view of your control
}