Solved

HTML: Using variables in DIV tag (ASP.Net)?

Posted on 2016-07-30
5
172 Views
Last Modified: 2016-07-31
Hi,
In very brief terms, what I need to achieve is to replace a hardcoded 'Name' and 'Email' that are displayed in a webpage for the 'Name' and 'Email' of the current logged in user.

I currently have 2 files for 1 ASP.NET webpage  (".aspx" page and ".aspx.cs" code behind file).

In my ".aspx" file I have the following code:
            <div class="firmaMail" id="firmaMaildicom">
                Regards.<br />
                <b>John Anibal Smith</b><br />
                Team Lead<br /><br />
                THE A TEAM<br />
                Telephone: 1 800 1234567<br />
                Mobile: 1 900 9876543<br />
                Mail: Anibal@ateam.com<br />
                Web: www.ateam.com<br />
            </div>
            <asp:LinkButton ID="sendMailDicom" runat="server" ><div class="button_submit" onclick="AvisoMailDicom()">Send Email</div></asp:LinkButton>
        </div>

Open in new window

In part of my "code behind" ".aspx.cs" file I have a working code to get the 'Email' of the current logged in user. Maybe this can work but I don't know how to use it (or call it) in the aspx file:
string email = Request.Form["email"];

Open in new window

Now, if that is not possible... Is it possible to use/get both variables I need directly in the "aspx" file without calling the "aspx.cs" file?
0
Comment
Question by:José Perez
[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
5 Comments
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 41736048
You basically have two options:

1. Use Server-Side controls
With this technique, you simply need to put server-side <asp:label /> controls wherever you want to display a server-side value.
As each one of these have an ID, just use it server-side code (the aspx.cs file) to set its Text value.

2. Use Server-Side tags
This is my favorite because it makes your HTML easier to read, in my opinion.
It uses the <%= %> server-side tag that will be executed still while ASP.net is rendering your page on the server.
Bellow, I have a sample code for this, and it assumes that you have a User class that holds all the property values and that you have a public property on the page back-end (the same aspx.cs file) called User that exposes that class instance with the user information.
<div class="firmaMail" id="firmaMaildicom">
  Regards.
  <br />
  <b><%= User.Name %></%></b>
  <br /><%= User.Role %>
  <br />
  <br /> THE A TEAM
  <br /> Telephone: 1 800 1234567
  <br /> Mobile: 1 900 9876543
  <br /> Mail: <%= User.Email %>
  <br /> Web: www.ateam.com
  <br />
</div>

Open in new window

0
 
LVL 82

Expert Comment

by:David Johnson, CD, MVP
ID: 41736145
You have to have the username/email stored somewhere and then retrieve it into a variable only then can you change the hard coded part to use your variable.
0
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 41736212
If I remember correctly, you can simple make the div tag (or one of it's child tags) server-side (runat="server") and do whatever you like in the code-behind. The tag will need a unique id (id="uniqueid") attribute.

In code-behind, do something like this:
uniqueid.InnerHtml = "HTML markup"

That's all there is to it.
0
 
LVL 2

Author Comment

by:José Perez
ID: 41736743
Solved. What I finally did was the following:

In .aspx file I replaced the 2 texts in html by 2 labels:
            <div class="firmaMail" id="firmaMaildicom">
                Regards.<br />
                <b><asp:Label ID="lblName" Text="" runat="server"></asp:Label></b><br />
                Team Lead<br /><br />
                THE A TEAM<br />
                Telephone: 1 800 1234567<br />
                Mobile: 1 900 9876543<br />
                Mail: <b><asp:Label ID="lblMail" runat="server"></asp:Label></b><br />
                Web: www.ateam.com<br />
            </div>
            <asp:LinkButton ID="sendMailDicom" runat="server" ><div class="button_submit" onclick="AvisoMailDicom()">Send Email</div></asp:LinkButton>
        </div>

Open in new window


Then, in.aspx.cs file "Page_Load" method I added the session details:
            usuario_info userInfo = new usuario_info((String)Session["UserId"]);
            persona_info perInfo = new persona_info(userInfo.get_id_Persona());
            lblName.Text = perInfo.get_firstname() + " " + perInfo.get_lastname();
            lblMail.Text = perInfo.get_mail();

Open in new window


so the points will go to Alexandre for his option "2. Use Server-Side tags" because he inspired me to do it this way.
0
 
LVL 2

Author Closing Comment

by:José Perez
ID: 41736745
Not the exact solution but it definitelly help me to get me inspired to fix.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

634 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