Solved

Make variable "code behind", use it "code front"

Posted on 2006-11-14
7
350 Views
Last Modified: 2012-05-05
I need to make a variable "MyVariable" code behind, and use the variable value "code front" to give the <DIV> correct ID... The code below dont work, but I hope it gives a clue on what I need... Thanks for all help!!

----------------
ASPX
----------------
<div class="post" ID="<% #MyVariable %>">
   Some text...
</div>


----------------
ASPX.CS
----------------
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        String MyVariable = "FirstItem"
    }    
    else
    {
        String MyVariable = "Item"
     }
}
0
Comment
Question by:webressurs
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 5

Expert Comment

by:prajapati84
Comment Utility
Try this one...

<div class="post" ID="<%= MyVariable %>" >
   Some text...
</div>

Regards,
Mukesh
0
 
LVL 27

Assisted Solution

by:Sammy
Sammy earned 125 total points
Comment Utility
the only way for a variable to be open to the aspx is by having it declared as a public class level var not in the page load
to see this work do this
before this line protected void Page_Load(object sender, EventArgs e)
declare your variable like so
public string MyVar=string.Empty;

in your page load you try to change the value of the variable as you wish

if (!Page.IsPostBack)
    {
       MyVar = "FirstItem"
    }    
    else
    {
        MyVar = "Item"
     }

in the aspx code just type
<div lass="post" id='<%=MyVar%>'>
    some text here
    </div>

HTH
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 125 total points
Comment Utility
Hi webressurs,

this will not work because to use variables in aspx page the variables must be public
so you have to change a little bit your code to:

public string MyVariable;
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        MyVariable = "FirstItem";
    }    
    else
    {
        MyVariable = "Item";
     }
}

and use the prajapati84 comment

Cheers!
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:webressurs
Comment Utility
That gives me this errormessage:
"Compiler Error Message: CS0103: The name 'MyVariable' does not exist in the current context"


This works, but is it the correct way to do it:
-----------------
ASPX
-----------------
<div class="post" id="<asp:Literal runat='server' ID='litClass' />">

-----------------
ASPX.CS
-----------------
this.litClass.Text = "firstPost";

0
 
LVL 28

Expert Comment

by:mmarinov
Comment Utility
webressurs,
actually i wouldn't do with this usage of literal. is it working with the accepted answers ?

regards
0
 
LVL 27

Expert Comment

by:Sammy
Comment Utility
I just tested the code and it worked fine for me without using any literals.
MyVariable' does not exist in the current context usually is thrown when you try to access a private variable or data of any kind.
make sure MyVariable is declared as public string in the class not the page load event!
0
 
LVL 1

Author Comment

by:webressurs
Comment Utility
My last message was ment to "prajapati84", the code from "sammy1971" and "mmarinov" worked PERFECT :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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 …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

18 Experts available now in Live!

Get 1:1 Help Now