在自定义文本框控件中添加两次属性
本文关键字:两次 属性 添加 文本 自定义 控件 | 更新日期: 2023-09-27 18:16:57
我正在使用动态创建的自定义文本框控件。我试图通过使用 htmltextwwriter添加"name"属性。AddAttribute 方法。但是,当我在IE浏览器中使用开发人员工具检查页面时,该属性在元素上添加了两次。这会导致错误'XML5634:在此元素上已经存在同名的属性。在Android用户代理中。这是我的代码
<table id="tblTester" runat=server>
<tr>
<td>
<asp:Label ID="Label1" runat=server Text="This is the custom textbox"></asp:Label>
</td>
<td id="tdTester">
</td></tr>
</table>
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
CustomTextBox txtBox = new CustomTextBox();
txtBox.TextMode = TextBoxMode.Password;
txtBox.ID = "txtAnswerRe";
txtBox.Width = Unit.Pixel(220);
tdTester.Controls.Add(txtBox);
}
CustomTextbox.cs
public class CustomTextBox : System.Web.UI.WebControls.TextBox
{
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
if (this.TextMode == TextBoxMode.Password)
{
Page page = this.Page;
if (page != null)
{
page.VerifyRenderingInServerForm(this);
}
string uniqueID = this.UniqueID;
if (uniqueID != null)
{
writer.AddAttribute(HtmlTextWriterAttribute.Name, uniqueID);
}
writer.AddAttribute(HtmlTextWriterAttribute.Type, "password");
string text = this.Text;
if (text.Length > 0)
{
writer.AddAttribute(HtmlTextWriterAttribute.Value, text);
}
base.AddAttributesToRender(writer);
}
else
{
// If Textmode != Password
base.AddAttributesToRender(writer);
}
}
}
这是页面检查的结果
<input name="txtAnswerRe" type="password" name="txtAnswerRe" type="password" id="txtAnswerRes" /></td>
在这种情况下,在一个元素中添加相同名称的属性是什么原因
发生这种情况是因为您在if
语句的末尾调用base.AddAttributesToRender(writer);
。这里不需要调用base
,只需添加一行来添加id
属性:
writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ID);