0

I am creating custom HtmlHelper in ASP.NET MVC 4 that needs to render ASP.NET standard web controls like <asp:Panel> or <asp:Button>. What I mean by this is: from my HtmlHelper, I want my browser to render them same as it is a ASP.NET web form application.

I was searching on the net about this topic, but didn't find anything specific that might help me how to realize this.

If anyone has any idea how this can be realized or has any experience with this, please share it :)

rene
  • 41,474
  • 78
  • 114
  • 152
  • 1
    There is nothing like `server side controls` in `ASP.NET MVC`. Don't connect `ASP.NET web-forms` with `ASP.NET MVC`. – Aditya Singh Jul 11 '14 at 11:10
  • @wiz kid I understand that MVC doesn't use server side controls. I was just looking for a way my browser to render them as it does when creating ASP.NET web forms. –  Jul 11 '14 at 11:12
  • I think you are asking for equivalent of a asp:button. There is no a @html.button in MVC. You will need to a use an Html button for this purpose. Refer to this page for more info. http://stackoverflow.com/questions/5955571/theres-no-html-button – causita Jul 11 '14 at 16:10
  • Nope, i am asking how to generate standard web controls, as i wrote. Anyways, i found out a solution how to do that :) Thanks for the help –  Jul 11 '14 at 16:49

1 Answers1

1

If anyone has similar problem or wants to know how it can be done, here is my code.

public static MvcHtmlString Panel(this HtmlHelper html)
{
            Panel pnl = new Panel();
            pnl.ID = "mainPanel";
            pnl.BorderStyle = BorderStyle.Solid;
            pnl.BorderWidth = Unit.Pixel(1);
            pnl.BorderColor = System.Drawing.Color.Black;

            Label lblTitle = new Label();
            lblTitle.Text = "Title";
            TextBox txtTitle = new TextBox();
            txtTitle.ID = "txtTitle";
            lblTitle.Attributes.Add("for", "txtTitle");

            Label lblMessage = new Label();
            lblMessage.Text = "Message";
            TextBox txtMessage = new TextBox();
            txtMessage.TextMode = TextBoxMode.MultiLine;
            txtMessage.ID = "txtMessage";

            Label lblDepartment = new Label();
            lblDepartment.Text = "Department";
            DropDownList lstDepartment = new DropDownList();
            lstDepartment.ID = "lstDepartment";
            ListItemCollection collection = new ListItemCollection();
            collection.Add(new ListItem("Department1"));
            collection.Add(new ListItem("Department3"));
            collection.Add(new ListItem("NoDepartment"));

            lstDepartment.DataSource = collection;
            lstDepartment.DataBind();

            pnl.Controls.Add(lblTitle);
            pnl.Controls.Add(txtTitle);
            pnl.Controls.Add(lblMessage);
            pnl.Controls.Add(txtMessage);
            pnl.Controls.Add(lblDepartment);
            pnl.Controls.Add(lstDepartment);

            HtmlTextWriter writer = new HtmlTextWriter(new StringWriter());
            pnl.RenderControl(writer);

            return MvcHtmlString.Create(writer.InnerWriter.ToString());
}

Basically, you use the classes of the web controls you need and you create the HTML. HtmlTextWriter is used to render the things you coded for that purpose.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636