Raspi Surya Raspi Surya - 6 months ago 35
C# Question

Cannot add class to aspx.cs file

I am new at programming. I have 2 files in my web site project, Translator2.aspx and Translator2.aspx.cs. I want to add new class (Erwin:Translator2) to my Translator2.aspx.cs file. However it was error like this :

Severity Code Description Project File Line Suppression State
Error 'translator2_aspx' does not contain a definition for 'Switch' and no extension method 'Switch' accepting a first argument of type 'translator2_aspx' could be found (are you missing a using directive or an assembly reference?) C:\Users\erwin.surya\Documents\Visual Studio 2017\WebSites\WebSite1\Translator2.aspx 33

Here is my Translator2.aspx code:

<%@ Page Language="C#" Async="true" AutoEventWireup="true" CodeFile="Translator2.aspx.cs" MasterPageFile="~/Site.Master" Inherits="Translator2" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">

<div class="form-group">
<asp:Label runat="server" AssociatedControlID="English" CssClass="col-md-2 control-label">English</asp:Label>
<div class="col-md-10">

<asp:TextBox Height="79px" TextMode="MultiLine" Width="452px" runat="server" ID="English" CssClass="form-control" />

</div> </div>

<div class="col-md-offset-2 col-md-10">
<asp:Button runat="server" OnClick="Submit" Height="39px" Width="100px" Text="Translate" CssClass="btn btn-default" />

<div class="col-md-offset-2 col-md-10">
<asp:Button runat="server" OnClick="Switch" Height="39px" Width="100px" Text="Switch" CssClass="btn btn-default" />

<br /><br /><br />
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="Japanese" CssClass="col-md-2 control-label">Japanese</asp:Label>
<div class="col-md-10">
<asp:TextBox Height="79px" TextMode="MultiLine" Width="452px" runat="server" ID="Japanese" CssClass="form-control" />

<br /><br />
<asp:PlaceHolder runat="server" ID="ErrorMessage" Visible="false">
<p class="text-danger">
<asp:Literal runat="server" ID="FailureText" />

Here is my Translator2.aspx.cs code:

partial class Translator2 : Page

public async void Submit(object sender, EventArgs e)

string key = "92fcf1387f844a8";
var authTokenSource = new AzureAuthToken(key.Trim());
string authToken;
authToken = await authTokenSource.GetAccessTokenAsync();

catch (HttpRequestException)
if (authTokenSource.RequestStatusCode == HttpStatusCode.Unauthorized)
Console.WriteLine("Request to token service is not authorized (401). Check that the Azure subscription key is valid.");
if (authTokenSource.RequestStatusCode == HttpStatusCode.Forbidden)
Console.WriteLine("Request to token service is not authorized (403). For accounts in the free-tier, check that the account quota is not exceeded.");

string output = "";
string text = English.Text;

string uri = "https://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + HttpUtility.UrlEncode(text) + "&from=" + "en" + "&to=" + "ja";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Headers.Add("Authorization", authToken);
using (WebResponse response = httpWebRequest.GetResponse())
using (Stream stream = response.GetResponseStream())
DataContractSerializer dcs = new DataContractSerializer(Type.GetType("System.String"));
string translation = (string)dcs.ReadObject(stream);
Console.WriteLine("Translation for source text '{0}' from {1} to {2} is", text, "en", "ja");
output = translation;
Japanese.Text = output;


class Erwin : Translator2
string from, to;
public void SwapStrings(string s1, string s2)


string temp = s1;
s1 = s2;
s2 = temp;

from = s1;
to = s2;

testing.Text = from;
testing1.Text = to;


protected void Switch(object sender, EventArgs e)
string str1 = testing.Text;
string str2 = testing1.Text;
System.Console.WriteLine("Inside Main, before swapping: {0} {1}", str1, str2);

SwapStrings(str1, str2);


Do you have any idea why I can't add Erwin:Translato2 class? everything works fine before I add the class. Did I miss something?

Answer Source

The Switch method is in a different class than the class that is powering your aspx page. If you want switch to be some kind of utility method, you should create a new class, maybe Erwin in a completely separate file (Erwin.cs), and have a switch method inside that that is public and probably static. I also note that switch doesn't return anything, so not sure what you are trying to accomplish with it.

The OnClick handler for your button should be in the code behind -- ie in the Translator2 class. Inside this handler, you could call Erwin.Switch().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download