Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to reference a C# variable (myString) into HTML. Everytime I run the program, I get the following error:

c:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\SPListWebPart\VisualWebPart1\VisualWebPart1UserControl.ascx(27): error CS0103: The name 'myString' does not exist in the current context

Here is my code:

VisualWebPart1UserControl.ascx

  <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"   Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral,   PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs"   Inherits="SPListWebPart.VisualWebPart1.VisualWebPart1UserControl" %>


<!DOCTYPE html>
<html>
<head>
<title></title>

<link rel="Stylesheet" type="text/css" href="c:\users\administrator\documents\visual studio 2010\Projects\SPListWebPart\SPListWebPart\VisualWebPart1\Stylesheet1.css" />
<ul id="sddm">


<script type ="text/javascript" src = "c:\users\administrator\documents\visual studio 2010\Projects\SPListWebPart\SPListWebPart\VisualWebPart1\JScript1.js"></script>
<script type ="text/C#" src="C:\Users\Administrator\Documents\Visual Studio  2010\Projects\SPListWebPart\SPListWebPart\VisualWebPart1\VisualWebPart1UserControl.ascx.cs"    ></script>


<li><a href="http://maindt" 
    onmouseover="mopen('m1');"
    onmouseout="mclosetime()"><%= %></a>
    <div id="m1" 
        onmouseover="mcancelclosetime()"
        onmouseout="mclosetime()">
    <a href="">Site 1</a>
    <a href="">Site 2</a>
    <a href="">Site 3</a>
    </div>
</li>

Here is my VisualWebPart1.ascx.cs

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;


namespace SPListWebPart.VisualWebPart1
{ 
    public partial class myClass : System.Web.UI.Page
    {

        public string myString = "Hello World";

        public void Page_Load(object sender, EventArgs e)
        {


        }

    }
share|improve this question
You need to either inherit from myClass in your ascx, not include it as script tag. See msdn.microsoft.com/en-us/library/d19c0t4b%28v=vs.71%29.aspx. Or you need the runat="server" on the script tag. – BlackICE Jul 9 at 4:44
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="SPListWebPart.VisualWebPart1.VisualWebPart1UserControl" %> – mwilson Jul 9 at 4:47

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.