Solved

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

Posted on 2011-02-14
3
333 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
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 39

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now