Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-14
3
Medium Priority
?
358 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 1400 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 600 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 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