Desidero aggiungere l'attributo href a un elemento di collegamento. Questo è il mio codice:Come aggiungere gli attributi correttamente al rendering con un HtmlTextWriter?
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
using (System.IO.StringWriter stringWriter = new System.IO.StringWriter(stringBuilder))
{
using (System.Web.UI.HtmlTextWriter htmlTextWriter = new System.Web.UI.HtmlTextWriter(stringWriter))
{
htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Ul);
htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.Li);
htmlTextWriter.RenderBeginTag(HtmlTextWriterTag.A);
htmlTextWriter.AddAttribute(HtmlTextWriterAttribute.Href, "http://www.google.de");
htmlTextWriter.Write("Google");
htmlTextWriter.RenderEndTag();
htmlTextWriter.RenderEndTag();
htmlTextWriter.RenderEndTag();
}
}
Rende come:
<ul>
<li><a>Google</a></li>
</ul>
Questo è quello che mi aspettavo.
<ul>
<li><a href="http://www.google.de">Google</a></li>
</ul>
Cosa non ha funzionato e come posso risolvere questo problema?
Si prega di non rispondere che potrei assemblare la stringa in qualche altro modo. Voglio sapere come ottenerlo con HtmlTextWriter
Grazie, non hanno pensato di esso, ma ha senso;) – citronas
lo fa? Non penso che abbia senso! – roryok
@roryok Lo fa (ha senso, cioè). Il metodo RenderBeginTag esegue effettivamente il rendering del tag, quindi la specifica degli attributi in seguito è inutile. –