wondering if I can do Html.RenderPartial("my user control path");

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 năm cách đây
I added following code in _ColumnsThree.cshtml:

@{ this.Html.RenderPartial("~/Views/Shared/WebUserControl1.ascx"); }

and in my user control I have only sample text "Test" right now.

I am getting this error:
The partial view '~/Views/Shared/WebUserControl1.ascx' was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Shared/WebUserControl1.ascx

My eventual goal is to show News scroller using web user control (.ascx file)

I will appreciate if anyone can reply to this.
12 năm cách đây
You're going to need to load up the control and render its contents.  Then instead of calling Html.RenderPartial, you'd call Html.Raw(resulting_html)...

Here's an example...  

*note, not tested...


@{
        string renderedControl = string.Empty;

        using(Page controlLoader = new Page())
        {

                WebUserControl1 viewControl = controlLoader.LoadControl("~/Views/Shared/WebUserControl1.ascx") as WebUserControl1; // I'm casting because the below example sets custom properties on the control.

                if(viewControl != null)
                {

                        // Populate any properties...
                        viewControl.Property1 = somevalue1;
                        viewControl.Property2 = somevalue2;

                        controlLoader.Controls.Add(viewControl);

                        StringWriter writer = new StringWriter();

                        Server.Execute(controlLoader, writer, false);

                        renderedControl = writer.ToString();
                }
        }
}

<div>@Html.Raw(renderedControl)</div>


Again, it's not tested, but something like this should work is you're dead-set on using an ASCX control.

-D
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.