Solved

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

Posted on 2006-11-14
7
354 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP:Radiobuttonlist/asp:RadioButtonListItem custom styling 1 42
Anyway to make "All" the default in the dropdown? 6 36
Code works but it's slow 28 70
Output Caching in IIS 2 28
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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