我有一个自定义控件,可以为表单生成标签输入对。我希望标签的文本由名称字段而不是ID字段控制。但是,当我进入自定义控件并显式设置我的标签的文本字段等于名称时,标签的文本始终是我的自定义控件的ID。为什么呢?
页面上的表格:
<FormUpgrade:FormUpgrade ID="fname" type="text" name="First Name" runat="server" />
在我的oninit
函数中,我有以下几行:
aLabel.Text = name;
this.Controls.Add(this.aLabel);
aLabel
之后永远不会被触及。
但是,如果我手动键入我的标签字段,则“文本”字段的行为与预期一致。
以下是整个自定义控件的代码:
namespace FormUpgrade
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:FormUpgrade runat=server></{0}:FormUpgrade>")]
public class FormUpgrade : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public Label aLabel = new Label();
public TextBox aField = new TextBox();
public RegularExpressionValidator regexvalid = new RegularExpressionValidator();
protected override void OnInit(EventArgs e)
{
regexvalid.ID = ID + "_validator";
if (!String.IsNullOrEmpty(ID))
{
aField.ID = ID+"_box";
aLabel.ID = ID + "_label";
aLabel.AssociatedControlID = aLabel.ID;
}
//aField.Attributes.Add("runat", "server");
// aLabel.Attributes.Add("runat", "server");
if (!String.IsNullOrEmpty(required))
{
aField.Attributes.Add("required", required);
if (required.Equals("required"))
{
aLabel.Attributes.Add("class", "required");
}
}
if (!String.IsNullOrEmpty(name))
{
aField.Attributes.Add("name", name);
aLabel.Text = name;
}
else
{
aLabel.Text = ID;
}
if (!String.IsNullOrEmpty(placeholder))
{
aField.Attributes.Add("placeholder", placeholder);
}
if (!String.IsNullOrEmpty(role))
{
aField.Attributes.Add("role", role);
}
if (!String.IsNullOrEmpty(maxlength))
{
aField.Attributes.Add("maxlength", maxlength);
}
if (!String.IsNullOrEmpty(placeholder))
{
aField.Attributes.Add("placeholder", placeholder);
}
aLabel.Attributes.Add("for", ID+"_box");
if (!String.IsNullOrEmpty(type))
{
aField.Attributes.Add("type", type);
}
//if (type.Equals("date"))
//{
// aField.Attributes.Add("class", "hasDatepicker");
//}
if(type.Equals("textarea"))
{
aField.TextMode = TextBoxMode.MultiLine;
}
if (type.Equals("password"))
{
aField.TextMode = TextBoxMode.Password;
}
aLabel.Text = name;
this.Controls.Add(this.aLabel);
this.Controls.Add(this.aField);
if(type.Equals("email"))
{
regexvalid.ValidationExpression = "[a-zA-Z0-9]+([-+.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-.][a-zA-Z0-9]+)*.[a-zA-Z0-9]+([-.][a-zA-Z0-9]+)*";
regexvalid.ControlToValidate = ID+"_box";
regexvalid.ErrorMessage = "Not a valid email address <br>";
this.Controls.Add(regexvalid);
}
else if (type.Equals("url"))
{
regexvalid.ValidationExpression = "http(s)?://([a-zA-Z0-9-]+.)+[a-zA-Z0-9-]+(/[a-zA-Z0-9- ./?%&=]*)?";
regexvalid.ControlToValidate = ID + "_box";
regexvalid.ErrorMessage = "Not a valid URL<br>";
this.Controls.Add(regexvalid);
}
else if(!String.IsNullOrEmpty(ValidationExpression))
{
regexvalid.ControlToValidate = ID + "_box";
regexvalid.ValidationExpression = ValidationExpression;
if (!String.IsNullOrEmpty(ValidationErrorMessage))
{
regexvalid.ErrorMessage = ValidationErrorMessage + "<br>";
}
this.Controls.Add(regexvalid);
}
base.OnInit(e);
}
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? "[" + this.ID + "]" : s);
}
set
{
ViewState["Text"] = value;
}
}
public string type
{
get
{
String s = (String)ViewState["type"];
return ((s == null) ? "[" + this.ID + "]" : s);
}
set
{
ViewState["type"] = value;
}
}
public string name
{
get
{
String s = (String)ViewState["name"];
return ((s == null) ? "" : s);
}
set
{
ViewState["name"] = value;
}
}
public string required
{
get
{
String s = (String)ViewState["required"];
return ((s == null) ? "" : s);
}
set
{
ViewState["required"] = value;
}
}
public string role
{
get
{
String s = (String)ViewState["role"];
return ((s == null) ? "" : s);
}
set
{
ViewState["role"] = value;
}
}
public string ValidationExpression
{
get
{
String s = (String)ViewState["ValidationExpression"];
return ((s == null) ? "" : s);
}
set
{
ViewState["ValidationExpression"] = value;
}
}
public string ValidationErrorMessage
{
get
{
String s = (String)ViewState["ValidationErrorMessage"];
return ((s == null) ? "" : s);
}
set
{
ViewState["ValidationErrorMessage"] = value;
}
}
public string placeholder
{
get
{
String s = (String)ViewState["placeholder"];
return ((s == null) ? "" : s);
}
set
{
ViewState["placeholder"] = value;
}
}
public string Display
{
get
{
String s = (String)ViewState["Display"];
return ((s == null) ? "" : s);
}
set
{
ViewState["Display"] = value;
}
}
public string maxlength
{
get
{
String s = (String)ViewState["maxlength"];
return ((s == null) ? "" : s);
}
set
{
ViewState["maxlength"] = value;
}
}
public string Value
{
get
{
String s = (String)ViewState["Value"];
return ((s == null) ? "" : s);
}
set
{
ViewState["Value"] = value;
}
}
public override void RenderBeginTag(HtmlTextWriter writer)
{
}
public override void RenderEndTag(HtmlTextWriter writer)
{
}
}
}
答案 0 :(得分:0)
看起来问题可能就在这里:
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? "[" + this.ID + "]" : s);
}
set
{
ViewState["Text"] = value;
}
}
我认为s
返回null,而是返回ID。如果Text
为空,请尝试返回空字符串。