Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-07-30
5
Medium Priority
?
231 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 2000 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 83

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

704 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