[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Fill javascript with data from code behind (asp.net)

Hi!

I need to run this JavaScript in the footer of my webpage (master page):

<script>
  analytics.identify('[UserId]', {
    name: '[Name]', 
    email: '[Email]' 
  });
</script>

Open in new window


UserId, Name and Email should be filled in from my asp.net code behind file. I want to fill inn all 3 fields in one operation to avoid unnecessary database calls.

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    string userId = User.UserId;
    string name = User.Name;
    string email = User.Email;

   //Put userId, name and email into the JavaScript...

  }
}

Open in new window


What is the best way to dynamically fill inn the necessary JavaScript fields in the code front file?
0
webressurs
Asked:
webressurs
  • 3
  • 2
1 Solution
 
GaryCommented:
<script>
  analytics.identify('<%=userId%>', {
    name: '<%=name%>', 
    email: '<%=email%>' 
  });
</script>

Open in new window

0
 
webressursAuthor Commented:
Thanks Gary!

How will the code behind code look like to do like this in code front?
0
 
GaryCommented:
Uh? You already have you code above
0
 
webressursAuthor Commented:
Ok, I thought I needed something more to fix this. Is it possible to move the code from Page_Load() to separate property like shown below, and still bind the data to the code front Java script?

protected void BindAnalyticsData()
{
    string userId = User.UserId;
    string name = User.Name;
    string email = User.Email;

   //Put userId, name and email into the JavaScript...
}

Open in new window

0
 
GaryCommented:
Just place the javascript above into your footer.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now