Solved

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

Posted on 2016-07-30
5
53 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 78

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net bundle 8 36
C# code editing and collaboration 3 44
Image(2) 3 26
Fixing my modal from bootstrap 18 13
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now