Solved

I need my aspx page to reference a varible defined in code behind

Posted on 2011-02-14
3
354 Views
Last Modified: 2012-05-11
I usually code in MVC and need to access the Model. But using code behind is different.

Please tell me how to create a variable in Page Load that can be accessed on the page? It's a reference of an object, actually.

Thanks,
newbieweb
0
Comment
Question by:newbieweb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 350 total points
ID: 34889684
If you're talking about WebForms, then you just need to declare a variable at the Page level and make it public:
public class MyPage : Page
{
       public string myString = "Hello World";

       protected void Page_Load()
       {
             ....
       }
}

Open in new window

Then access it in the ASPX page with:
<%= myString %>

Open in new window

0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 150 total points
ID: 34889686
In general the tag wrapper is <%=   %>  

In this example, intiparams is a variable that I've set in my codebehind and then passed to the rest of the control.  The highlited line is:
         
               <param name="InitParams" value="<%=InitParams %>" />

and the whole control is:



 <div  id="SilverlightContainer" style="width:100%;height:100%">
         
             <object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
                    width="100%" height="80%" id="silverlightControl">
               <param name="source" value="ClientBin/myCode.xap"/>
               <param name="onError" value="onSilverlightError" />
               <param name="enableGPUAcceleration" value="true" />
               <param name="background" value="white" />
               <param name="minRuntimeVersion" value="3.0.40624.0" />
               <param name="InitParams" value="<%=InitParams %>" />               <param name="autoUpgrade" value="true" />

                    <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0"  style="text-decoration:none">
                        <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
                        style="border-style:none"/>
                    </a>
            </object>

        </div>


Note this is for a silverlight control but in general it works the same.
0
 

Author Closing Comment

by:newbieweb
ID: 34890038
Thanks!
0

Featured Post

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question