tags:

views:

116

answers:

3

Hi

I have a nice set of html and now I need to use RenderBeginTag, RenderEndTag type of code in my custom control render method. Is there any tools for converting html C# code? It's just too much work for nothing, if I start coding this manually.

A: 

Hi there.

I'm not sure there is a way to do this. The closest I could find was this site which generates HTML code into Response.Write calls.

Cheers. Jas.

Jason Evans
A: 

When writing custom WebControl classes, you can still use standard ASP.NET controls, avoiding the need to use the tag rendering methods:

[ToolboxData("<{0}:MyCustomControl runat=server></{0}:MyCustomControl>")]
public class MyCustomControl : CompositeControl
{
    public MyCustomControl()
    {

    }

    public string Text
    {
        get
        {
            object o = ViewState["Text"];
            return ((o == null) ? "Set my text!" : (string)o);
        }
        set
        {
            ViewState["Text"] = value;
        }
    }

    protected override void CreateChildControls()
    {
        // Create controls
        var label = new Label();
        label.ID = "innerLabel";
        label.Text = this.Text;

        // Add controls
        this.Controls.Add(label);

        // Call base method
        base.CreateChildControls();
    }
}

Or you can use the tags in the render methods like so:

[ToolboxData("<{0}:MyCustomControl runat=server></{0}:MyCustomControl>")]
public class MyCustomControl : CompositeControl
{
    public MyCustomControl()
    {

    }

    public override void RenderControl(HtmlTextWriter writer)
    {
        base.RenderEndTag(writer);

        if (!this.DesignMode)
        {
            var label = new Label();
            label.Text = "Hello!";
            label.RenderControl(writer);
        }
    }
}

Hopefully this answers your question :s

Codesleuth
This leads to same problem. I have something like 10 different page layouts with few hundred lines of html. Instead of writing RenderTags I would have to write code likeTable t = new Table();this.Control.Add(t);TableRow tr = new TableRow();t.Rows.Add(tr);It don't get my any closer to my original target. If somebody knows how to create this type of code from html, is also a good option.Jani
Jani
See the first code sample that uses `CreateChildControls`.
Codesleuth
A: 

[ToolboxData("<{0}:MyCustomControl runat=server>")] public class MyCustomControl : CompositeControl { public MyCustomControl() {

}

public override void RenderControl(HtmlTextWriter writer)
{
    base.RenderEndTag(writer);

    if (!this.DesignMode)
    {
        var label = new Label();
        label.Text = "Hello!";
        label.RenderControl(writer);
    }
}

}

Did you just copy and paste my answer?
Codesleuth