Solved

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

Posted on 2016-07-30
5
88 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
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 80

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

830 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