2013-06-05 40 views
1

我正在使用動態創建的自定義文本框控件。我試圖通過使用HtmlTextWriter.AddAttribute方法來添加'name'屬性。但是當我在IE瀏覽器中使用開發人員工具檢查頁面時,該元素被添加了兩次。它將導致錯誤'XML5634:具有相同名稱的屬性已存在於此元素上。'在android用戶代理中。這裏是我的代碼Attibutes在自定義文本框控件中添加了兩次

<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> 

的結果是什麼原因一個el中添加了同名的attibute在這種情況下。在此先感謝

回答

1

發生這種情況,因爲你在,如果if語句結束通話base.AddAttributesToRender(writer);。而不是在這裏調用base只需添加一行即可添加id屬性:

writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ID); 
+0

感謝michael現在的工作。但任何想法爲什麼base.AddAttributesToRender(作家)適用於許多瀏覽器,但不適用於android? – Suresh

相關問題