c# - The name 'hello' does not exist in the current context -
i have in default.aspx
<%@ page title="home page" language="c#" masterpagefile="~/site.master" autoeventwireup="true" codebehind="default.aspx.cs" inherits="approvalprocess._default" %> <asp:content id="headercontent" runat="server" contentplaceholderid="headcontent"> </asp:content> <asp:content id="bodycontent" runat="server" contentplaceholderid="maincontent"> <h2> welcome asp.net! </h2> <p> learn more asp.net visit <a href="http://www.asp.net" title="asp.net website">www.asp.net</a>. </p> <p> hello, it's working <a href="http://go.microsoft.com/fwlink/?linkid=152368&clcid=0x409" title="msdn asp.net docs">documentation on asp.net @ msdn</a>. </p> <script> alert("<%= hello%>"); </script> </asp:content>
and default.aspx.cs
namespace approvalprocess { public partial class _default : system.web.ui.page { string hello = "hello"; protected void page_load(object sender, eventargs e) { } } }
the following error
compiler error message: cs0103: name 'hello' not exist in current context
if don't specify visibility defaults private. view cannot see hello
variable because view class inherits code-behind class. need declare either protected
or public
depending on how visible want (likely protected
correct in case).
protected string hello = "hello";
or
public string hello = "hello";
it might enlightening read on member visibility understand options mean can make informed choices continue develop.
http://en.wikipedia.org/wiki/class_(computer_programming)#member_accessibility
Comments
Post a Comment