Solved

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

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 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