Search code examples
htmlasp.netmaster-pagesascx

ASP - Master Page and User Controls


I'm currently trying to use a UserControl in my master page; however my ascx file doesn't like my code, two parts specifically:

  1. @Html (Doesn't exist in current context)
  2. Model. (Doesn't exist in current context)

.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ConveyancingUserControl.ascx.cs" Inherits="xxx.Controllers.ConveyancingUserControl" %>


<div id="cwContainer" class="ui-widget-content">

    <div id="cwHead">
        <p class="cwhTitle">Conveyancing Quotation</p>
    </div>


    <div id="cwBody">
        <%using (Html.BeginForm("Home", "Xxxx", FormMethod.Post, new { onsubmit = "document.getElementById('xxxxBusy').style.display = 'inline';", @class = "xxxxForm" }))
            {%>  

        <table>
            <tr>
                <td>What are you doing?</td>
                <td> <%: @Html.DropDownListFor(Quote => Model.sessionQuote.quoteType, Model.sessionQuote.quoteTypeList, new { style = "width:150px" })%> </td>
            </tr>

        </table>

        <%} %>

    </div>

    <div id="cwFoot"></div>

</div>

ascx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mvc;

namespace ASP4HFWClaimsPortal.Controllers
{
    public partial class ConveyancingUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

.Master:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<xxx.Models.XxxxSession>" %>
     <!-- The main body of each page -->
    <div id="MainBody" runat="server" >
        <ul class="xxxxMenu"> 
            <li class="xx"> <%: Html.ActionLink("Home", "Home", "x", null, new { onclick="document.getElementById('xx').style.display = 'inline';", @class="x", @title="Home" })%></li> 
            <li class="x"> <%: Html.ActionLink("About", "About", "Xxxx", null, new { onclick="document.getElementById('xx').style.display = 'inline';", @class="x", @title="About" })%></li>    
        </ul>   

        <section class="content-wrapper main-content clear-fix">

            <!-- Additional BODY content for each individual page is inserted here -->
            <asp:ContentPlaceHolder ID="MainContent" runat="server" />

        </section>
        <uc1:conveyancingusercontrol ID="ConveyancingUserControl1" runat="server" />



    </div>

Web.config:

<controls>
        <add src="~/Controllers/ConveyancingUserControl.ascx" tagName="ConveyancingUserControl" tagPrefix="uc1"/>
      </controls>

I realise that my ascx file needs some kind of reference to find my model I'm just not sure how to do this... or if it's even possible. With regards to the 'Html.' issue, I guess this is something similar.

Apologies if these are silly questions... ASP seems to be one of those skills i jump into every now and again so I never get time to really explore it.


Solution

  • @Html is Razor syntax. You need to use the <%, <: etc syntax for ASP.

    Model exists in MVC, you need to look at code-behind to populate your controls.