2013-06-05 7 views
1

동적으로 만든 사용자 지정 텍스트 상자 컨트롤로 작업하고 있습니다. HtmlTextWriter.AddAttribute 메서드를 사용하여 'name'특성을 추가하려고합니다. IE Explorer에서 개발자 도구를 사용하여 페이지를 검사 할 때 요소에 속성이 두 번 추가됩니다. 'XML5634 :이 요소에 같은 이름의 특성이 이미 있습니다..' 안드로이드 사용자 에이전트. 여기에 내 코드사용자 지정 텍스트 상자 컨트롤에서 Attributes가 두 번 추가되었습니다.

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

의 결과입니다 하나의 엘에서 동일한 이름을 가진 attibute 이 경우에는 미리 감사드립니다.

답변

1

if 문이 끝나면 base.AddAttributesToRender(writer);을 호출하기 때문에 이런 일이 발생합니다. 여기에 base을 호출하는 대신 id 속성을 추가하는 행을 추가하십시오.

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

감사합니다. 하지만 base.AddAttributesToRender (작성자)가 많은 브라우저에서 잘 작동하는 이유에 대한 아이디어는 있지만 Android에서는 그렇지 않습니다. – Suresh