Solved

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

Posted on 2006-11-14
7
351 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
ID: 17945073
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
ID: 17945336
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
ID: 17945342
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:webressurs
ID: 17945355
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
ID: 17945405
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
ID: 17945447
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
ID: 17945579
My last message was ment to "prajapati84", the code from "sammy1971" and "mmarinov" worked PERFECT :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where to begin studying? 5 80
ASP:DataGrid and how to bind to a BoundColumn 8 37
Authentication of Web Services 3 46
Message not shown 5 29
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

932 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

12 Experts available now in Live!

Get 1:1 Help Now