Solved

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

Posted on 2006-11-14
7
357 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
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate Unique ID in VB.NET 21 125
Google Maps with Webforms 1 43
Asp.Net Session Question 2 44
locate sql commands in C# visual studio Project 6 63
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 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