Referencing a partial in Views/Shared asp.net - asp.net

I found the following post: Render partial from different folder (not shared)
However this isn't working on my page.
The file structure is as follows:
>Areas
>>NewNavigation
>>>Views
>>>>Shared
>>>>>MyCustomView
>Views
>>Shared
>>>SignInLink
I want MyCustomView to be able to render the partial found in: views/shared/signinlink
<% Html.RenderPartial("~/Views/Shared/SignInLink.ascx"); %>
Cannot resolve partial view

Try <% Html.RenderPartial("SignInLink"); %>, as the mvc runtime will search the shared directory (this is built in):
... will search the partial view in different folder:
The /Views/Home folder a file named “_MyPartial” (With aspx, ascx,
cshtml or vbhtml extension). The View/Shared folder a file named
“_MyPartial” (With aspx, ascx, cshtml or vbhtml extension).
http://coding-in.net/mvc-3-organize-your-partial-views/

Related

ASP.NET Master Page on file in folder(s)

I have a small problem with an ASP page. I have a master file and it works great on all pages in the ROOT; however, if I create a folder (say: reports and folder insider there called userRpts) and add a page, the master doesn't apply.
So, I have a fodler structure like:
/reports/UserRpts/UserData.aspx
Master is set as: MasterPageFile="~/MasterPage.master" in the <%# Page %>
Yet it doesn't apply. Any thoughts?

How to force update .ascx file content in Website project

I have Website project, which contains some .ascx and .aspx files. I have added new element <asp:TextBox ID="tb1" runat="server" ... /> in .ascx file and I have wrote some code in proper .ascx.cs file using this element: tb1.Text = "SomeText";. When I compile this project I recieve following error: The name 'tb1' does not exist in the current context.
How can I force to refresh markup of .ascx page? I use Website project and I cannot to change its type to Webapplication.
UPD: I have Website project, which has NOT .ascx.designers.cs files. And I cannot change type of my project to web application.
Unless there's something else happening here, it sounds like the designer.cs file might be out of sync. Try cutting and pasting the control back into the markup, or go into the designer file for the user control and add the TextBox manually:
protected global::System.Web.UI.WebControls.TextBox tb1;
It seems like your design file is not connecting with your code behind file.
Can you confirm if you are defining it as follows,
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="yourcontrol.ascx.cs" Inherits="CompleteNameSpace.ClassName" %>

Create code behind file after aspx has been created

I just inherited a web site that was created by a designer. The site was originally created with all *.html files. The designer renamed all the *.html files to *.aspx files. Hence there are no aspx.cs files created. I pulled the site into a new VS2012 solution. My question is, is there a way in VS 2010 to automatically create the code behind files for a an existing stand alone aspx file?
I don't know of an automated way to do this, but if there is no server side code in the existing *.aspx files then it should just be a case of adding the .cs codebehind files and then wiring them up in the <%# Page tag like so:
<%# Page Title="YourPageTitle" Language="C#" AutoEventWireup="true" CodeBehind="YourPage.aspx.cs" Inherits="YourNamespace.YourPage" %>
Note: This will not create the YourPage.aspx.designer.cs file. (I usually delete these anyway as they cause merge issues - i find it easier to add the controls i need to reference to my code-behind file manually.)
The other alternative is to just create a new "Web Form" for each page with the correct names and then copy and paste the existing markup into them. If you do have server code in the existing *.aspx files then you will need to manually copy it to the code-behind.
Based on what I found here: http://forums.asp.net/t/1229894.aspx/1
Right click on your solution explorer.
Add New Item -> Class File.
Name the file as the name of your aspx eg: Default.aspx.cs
When it asks you the file should be in app_code click "no".
In your aspx in page attribute add
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default"
Similarly in your class file that you just added remove everything. Your class should look like this:
//all namespaces go here
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
After you add the new .cs file, you may want to see the file look like a codebehind file (indented, icon, etc). To do so:
Unload the project
Edit the project
Find the new filename (file.aspx.cs) in the section with files.
Add an xml node for DependentUpon.
Save and Close the project
Reload the project
For a file Profile.aspx.cs, the xml should look something like this:
<Compile Include="Profile.aspx.cs">
<DependentUpon>Profile.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>
In Visual Studio 2012: Right click on the project --> click Add --> click Web Form --> Copy the content of your original aspx file into the new WebForm aspx --> delete the original aspx file --> Rename the new one to anything you want.
Now you should have a new aspx file with a code behind file that is ready for use
To save yourself from the drama of manually editing the project file like David Frette details, I suggest you remove the file from the project and create a new file with the same name with a code-behind. Then copy-paste the contents of the original aspx or ascx to the new files.

.aspx codefile page unable to recognise control on the page

I am getting a compilation error on a website. There is a repeater declared in the aspx file as follows:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="MyPage.aspx.cs"
     Inherits="MyClass" %>
<asp:Repeater ID="rptMyRepeater" runat="server">
<ItemTemplate>
<tr>
         <td>
…
And the class is defined as follows:
public partial class MyClass : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
{
rptMyRepeater.DataSource = GetMyDataSource();
            rptMyRepeater.DataBind();
        }
}
}
The problem I have is that rptMyRepeater is not recognised. Note that I copied these files in from another project, and so don't have a designer.cs file.
I came across this question which implies a "Convert to Web Application" would fix the problem. As I'm referencing a CodeFile rather than a CodeBehind, does this apply, or is there a better way? Is a designer file even necessary in this case?
If what you are saying is you don't have just the contents of designer.cs, add this to designer.cs:
protected global::System.Web.UI.WebControls.Repeater rptMyRepeater;
If you do not have a designer.cs file at all, add it to aspx.cs -i.e codefile- and it should work.
Simply this is the equivalent of what the designer file supposed to be doing, controls are not much different than class variables as I see.
A couple of things you could check:
Ensure that your .cs file is set to Compile in the properties
Try using CodeBehind instead of CodeFile
If your page class is inside a namespace then ensure it is fully qualified in the aspx file
If you are using a Web Application Project then right-click and Convert to Web Application

render .htm page into .aspx page

in my .aspx page i want to render a .htm page which has some data
in .aspx page i have:
<% Html.RenderPartial("/Views/Templates/HTML_Temp.htm"); %>
but this gives runtime error:
Server Error in '/' Application.
There is no build provider registered for the extension '.htm'. You can register one in the section in machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'.
how to resolve this problem...please help
try this instead:
<%= File.ReadAllText(Server.MapPath("/Views/Templates/HTML_Temp.htm")) %>
<%= System.IO.File.ReadAllText(Server.MapPath("~/Views/Templates/HTML_Temp.htm")) %>
Use #include
The simplest way to put some HTML content in your containing page is by using a server include with #include:
<!-- #include file="../Templates/HTML_Temp.htm" -->
// for relative paths
or
<!-- #include virtual="/Views/Templates/HTML_Temp.htm" -->
// for virtual paths
A better way for Asp.net up to 2.0
A better way would of course be to rename your HTML files to ASCX and create a common CS file that doesn't have any particular functionality. Use this same CS file (class relation) with all you newly created ASCX files.
A better way for Asp.net 2.0 and above
Create a master page (or multitude of them; if you're in 3.0 or higher they can be nested as well) that includes common content and develop other pages on top of your master(s).

Resources