views:

77

answers:

1

Hi

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());
        }
    }
}
+3  A: 

Change the sequence of statements:

writer.AddAttribute("href", "g");
writer.RenderBeginTag(HtmlTextWriterTag.A);
adatapost
Would not have guess that. Thanks
chobo2
Yeah, it's a bit weird but it is easy to get used to. I stumbled on that one the first time I used a HtmlWriter. What's worse is when you go do use a HtmlGenericControl which you create first `= new HtmlGenericControl("a");` and *then* do the `.Attributes.Add("href", "g")` (if you were creating a generic control of the a tag.
Joshua