chobo2
chobo2

Reputation: 85765

What am I doing wrong HtmlWriter not writing attribute

I have this

foreach (var columnName in columns)
{

    writer.RenderBeginTag(HtmlTextWriterTag.A);
    writer.AddAttribute("href", null);
    writer.Write("Delete");
    writer.RenderEndTag();
}

When I get to this method in my html helper class that I made it goes through this for loop based on how many columns are in a string[] columns parameter. The first time it goes around I get this

<a>Delete</a>

2nd time it goes around

<a href="">Delete</a>

3rd time I get

<a href="">Delete</a>

and so on.

why is the first one missing the "href"? I don't understand it.

One more thing the writer is being passed also as a parameter in.

Here is a console app. I just throw quickly together

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var writer = new HtmlTextWriter(new StringWriter());

            string[] columns = new string[4];
            columns[0] = "hi";
            columns[1] = "bye";
            columns[2] = "hi";
            columns[3] = "bye";

            foreach (var columnName in columns)
            {

                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.AddAttribute("href", "g");
                writer.Write("Delete");
                writer.RenderEndTag();
            }
            Console.WriteLine(writer.InnerWriter.ToString());
        }
    }
}

Upvotes: 1

Views: 458

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94645

Change the sequence of statements:

writer.AddAttribute("href", "g");
writer.RenderBeginTag(HtmlTextWriterTag.A);                                

Upvotes: 3

Related Questions