How to put a class in aspx file? - c#

I'm brand new to asp.net. I added a class to my aspx file and now it does not compile:
Compiler Error Message: CS1513: } expected
Line 206: }
Line 207:
Line 208: private void #__Render__control1(System.Web.UI.HtmlTextWriter #__w, System.Web.UI.Control parameterContainer) {
Line 209:
Line 210: #line 7 "c:\aspx\dummy.aspx"
I'm not even using the class yet. The class is simple enough:
public class AddressSummary
{
public string id { get; set; }
public string txt { get; set; }
}
I'm not using visual studio, just a text editor, so no replies with solutions that involve VS please.
I got the entire file down to this:
<%# Page Language="C#" Debug="true" %>
<%# Import Namespace="System.Net" %>
<%# Import Namespace="System.IO" %>
<%# Import Namespace="System.Text" %>
<%# Import Namespace="System.Xml" %>
<%# Import Namespace="System.Data" %>
<%
public class AddressSummary
{
public string id { get; set; }
public string txt { get; set; }
}
%>
Works without the class. Can somebody point me in the right direction please.

To declare a class, you need to put it in a <script> tags:
<%# Page Language="C#" Debug="true" %>
<%# Import Namespace="System.Net" %>
<%# Import Namespace="System.IO" %>
<%# Import Namespace="System.Text" %>
<%# Import Namespace="System.Xml" %>
<%# Import Namespace="System.Data" %>
<script type="text/c#" runat="server">
public class AddressSummary
{
public string id { get; set; }
public string txt { get; set; }
}
</script>
<%
// Create an instance of the class declared above
var a = new AddressSummary();
%>

Related

I am writting an mvc application and having an error when trying to get a loop of products using strongly-typed

I created a strongly typed view which view data class is the class called ProductList. I am returning to the view a list object as well that comes from getting all the products by using linq to entitites. Why am I getting an error when trying to use the product.name as shown on the code below on the view. Im very new on this so sorry if my question is bad. Any help would be very appreciated
public class ProductList
{
public IList<Product> Products { get; set; }
}
this is the controller :
public ViewResult List()
{
IQueryable<Product> allproducts= productrepository.selectAll();
return View(allproducts.ToList());
}
and this is the view :
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"Inherits="System.Web.Mvc.ViewPage<TradeIt.Models.ProductListViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
List
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% foreach (var product in Model.Products)
{ %>
<%: Html.LabelFor(product.Name) %>
<% } %>
</asp:Content>
I am getting the next error: The type argument for method 'system.web.mvc.html.labelExtensions.LabelFor... cannot be inferred from the usage . Try specifiying the type arguments explicitly'
change this
"Inherits="System.Web.Mvc.ViewPage" %>
your model is called ProductList, not ProductListViewModel

Inlining a Public Variable in a User Control

I have a user control with a variable that I am trying to display inline in the Aspx page, but it always shows as empty. I've done this in other user controls and I'm not sure why it isn't working.
Aspx...
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyClass.ascx.cs" Inherits="MyClass" %>
<%# sMyPublicString %>
Code Behind...
public partial class MYClass : System.Web.UI.UserControl
{
public string sMyPublicString = "MyValue";
}
In parent webform on Page_Load you have to add Page.DataBind()

Advice on how to create a common base class (or functionality) between a page and master?

I created a custom base class that my .aspx pages inherit from.
Since Master page's inherit from MasterPage and not Page, how can I create common functionality that are available in both my Pages and Master pages?
public class SitePage : System.Web.UI.Page
{
public SitePage()
{
}
public bool IsLoggedIn
{
//
}
public string HtmlTitle
{
//
}
}
One approach would be to put all the functionality in the Master page and always call them by going through the Master page. You can strongly type the Master property in SitePage:
public class SitePage : Page
{
public new MyMaster Master { get { return base.Master as MyMaster; } }
}
Then access the values through the Master:
this.Master.IsLoggedIn
Master Page:
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MyProject.master.cs"
Inherits="MyProject.MasterPages.MyProject" %>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
Base page:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/MyProject.Master"
AutoEventWireup="true" CodeBehind="BasePage.aspx.cs"
Inherits="MyProject.BasePage" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
</asp:Content>
Content Page:
<%# Page Title="MyProject - Home" Language="C#"
MasterPageFile="~/MasterPages/MyProject.Master" AutoEventWireup="true"
CodeFileBaseClass="MyProject.BasePage" CodeFile="Default.aspx.cs"
Inherits="MyProject.Default"
Meta_Description="Code Snippet: Master Page and Base Page"
Meta_Keywords="master, base, content" Theme="Style" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
</asp:Content>

NerdDinner DinnerFormViewModel Error CS0030 error

I am following the NerdDinner part in the Book Professional ASP.NET MVC 2. Currently i am at the part where i need to implement the DinnerFormViewModel and the Renderpartial Dinnerform.
The book contains some errors here so I tried to search on the internet and fix it myself..
I have put the DinnerFormViewModel in the Models folder this is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace NerdDinner.Models
{
public class DinnerFormViewModel : Controller
{
private static string[] _countries = new[]{
"USA",
"Ireland",
"Scotland",
"Namibia"
};
//Properties
public Dinner Dinner { get; private set; }
public SelectList Countries { get; private set; }
//Constructor
public DinnerFormViewModel(Dinner dinner)
{
Dinner = dinner;
Countries = new SelectList(_countries, dinner.Country);
}
// GET: /DinnerFormViewModel/
public ActionResult Index()
{
return View();
}
}
}
Then i have made the DinnerForm.ascx (Partial class):
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Models.DinnerFormViewModel>" %>
<%: Html.ValidationSummary("Please correct the errors and try again.") %>
<% using (Html.BeginForm()) { %>
<fieldset>
<p>
<%: Html.LabelFor(m => m.Dinner.Title) %>
<%: Html.TextBoxFor(m => m.Dinner.Title) %>
<%: Html.ValidationMessageFor(m => m.Dinner.Title, "*") %>
ETC...
and i have made the edit.aspx as follows:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Models.DinnerFormViewModel>" %>
<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
Edit: <%: Model.Dinner.Title %>
</asp:Content>
<asp:Content ID="Edit" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit Dinner</h2>
<% Html.RenderPartial("DinnerForm"); %>
</asp:Content>
Now if i start the application, an error at <% Html.RenderPartial("DinnerForm"); %> will popup saying:
c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\c8cca855\23406a1e\App_Web_dinnerform.ascx.32d6c807.tczxq3bd.0.cs(166): error CS0030: Cannot convert type 'ASP.views_dinners_dinnerform_ascx' to 'System.Web.Mvc.ViewUserControl'
I think it has something to do with the namespaces, but i can't fix the error, someone faced the same problem or someone here that can help me out?? Thank you!:)
Your partial view should inherit from System.Web.Mvc.ViewUserControl.
ViewPage is for a full view.

Pass Generic Model to View

I have this in my Page setup;
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ILocationsFormViewModel>" %>
And the interface looks like this;
public interface ILocationsFormViewModel<T>
{
List<TopLocation<T>> Locations { get; set; }
List<TrendingItem<ITrendItem>> TrendingLocations { get; set; }
}
T is either ITopHotel or ITopShop etc etc with no properties
The problem is I get this error when I goto the View;
CS0305: Using the generic type 'ILocationsFormViewModel' requires
'1' type arguments
Does anyone know how I can pass a class that has a Generic type to a view?
You probably need the generic type parameter in there:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ILocationsFormViewModel<T>>" %>

Categories

Resources