?
Solved

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

Posted on 2006-11-14
7
Medium Priority
?
361 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
[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
  • 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 Ageil
Sammy Ageil earned 500 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 500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 Ageil
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

762 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