Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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 …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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