C# 在自定义文本框控件中添加两次属性
我正在使用动态创建的自定义文本框控件。我正在尝试使用HtmlTextWriter.AddAttribute方法添加“name”属性。但是,当我使用IE浏览器中的开发人员工具检查页面时,该属性在元素上添加了两次。这将导致错误“XML5634:Android用户代理中此元素上已存在同名属性”。这是我的密码C# 在自定义文本框控件中添加两次属性,c#,asp.net,custom-controls,custom-server-controls,C#,Asp.net,Custom Controls,Custom Server Controls,我正在使用动态创建的自定义文本框控件。我正在尝试使用HtmlTextWriter.AddAttribute方法添加“name”属性。但是,当我使用IE浏览器中的开发人员工具检查页面时,该属性在元素上添加了两次。这将导致错误“XML5634:Android用户代理中此元素上已存在同名属性”。这是我的密码 <table id="tblTester" runat=server> <tr> <td> <asp:Label ID="Lab
<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>
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>
在这种情况下,在一个元素中添加同名属性的原因是什么。发生这种情况是因为您正在调用
base.AddAttributesToRender(writer)如果使用if
语句,则代码>位于末尾。这里不需要调用base
,只需添加一行即可添加id
属性:
writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ID);
这是因为您正在调用base.AddAttributesToRender(writer)如果使用if
语句,则代码>位于末尾。这里不需要调用base
,只需添加一行即可添加id
属性:
writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ID);
谢谢,迈克尔,现在可以用了。但是你知道为什么base.AddAttributesToRender(writer)在很多浏览器上都能正常工作,但在android上却不行吗?谢谢,michael,现在可以了。但是你知道为什么base.AddAttributesToRender(writer)在很多浏览器上都能正常工作,但在android上却不行吗?