Public · Protected · Private
Fun with ASP.Net Controls inside MVC app
Type: Public  |  Created: 2012-08-16  |  Frozen: Yes
« Previous Public Blog Next Public Blog »
Comments
  • Dont want to start at very basics...

    Do you have a MVC view (say index.chtml)page where your out put is minimally

    <h2>Name: @Model.FirstName</h2>
    <h2>hi</h2><h2>Name: @Model.FirstName</h2>
    <h2>hi</h2>

    If Yes ... I will start here

    File ->  new  -> create a class as below...

        public class Myclass : Control
        {
            private string name { get; set; }
            private string url { get; set; }
            public Myclass(string aid,string aurl)
            {
                name = aid;
                url = aurl;
            }
            protected override void Render(HtmlTextWriter writer)
            {
                writer.WriteLine("<a href=\"" + url + "\"align=\"left\"  > " + name + " </a> ");
            }
       }   

    It is just a custom control that does rendering as required.

    2012-08-16 07:29
  • Now let me go to out index.chtml page.


    @section header{
    This message form top.
    @GetMyOWNControl("hotmail", "http://www.hotmail.com")
    @GetMyOWNControl("yahoo", "http://www.yahoo.com")
    @GetMyOWNControl("janaganamana", "http://www.janaganamana.net")

    }
    @helper GetMyOWNControl(String iname, string iurl)
        {
            try
            {
                Page pageHolder = new System.Web.UI.Page();
                System.Web.UI.WebControls.Panel p = new System.Web.UI.WebControls.Panel();
                p.Controls.Add(new System.Web.UI.WebControls.HyperLink() { Text = iname, NavigateUrl = iurl });
                p.Controls.Add(new PartyInvites.Myclass(iname, iurl));
                pageHolder.Controls.Add(p);
               
                using (StringWriter output = new StringWriter())
                {
                    HttpContext.Current.Server.Execute(pageHolder, output, true);
                    var outputToReturn = output.ToString();//.Replace("\"", "\\\"").Replace("\r", " ").Replace("\n", " ").Replace("\t", " ");
                    @(new HtmlString(outputToReturn))
                   // @Html.Raw(outputToReturn)

                }
            }catch (Exception){}
         }

    2012-08-16 07:33
  • may be i unnecessarily complicated it adding sections

    your layout(/master page)  may have

        <div id="main">
            <div id="header">
                @RenderSection("header", false)
            </div>

            <h2>
                gala gala</h2>
            @RenderBody()
            <div id="footer">
                @RenderSection("footer", false)
            </div>
        </div>

    Hilighted is the one that calls "@section header "

    remember you can add any control into this panel...

    p.Controls.Add(new Myclass(iname, iurl));

    now you can use any of your ASP.NET controls. -- Happy coding...

     

     

     

     

     

    2012-08-16 07:38
This blog is frozen. No new comments or edits allowed.