Solved

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

Posted on 2016-07-30
5
75 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 79

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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…
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.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

803 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