0

I have a rather simple problem which I can't seem to work out.

I have a javascript code block;

<script>
 var user = {
       id: 1,
       username: 'myname' 
};

window.location.href = '<%# Page.GetRouteUrl("member", New With {.member = --username--})%>';
</script>

In the above code, I need to replace --username-- with user.username from my javascript object.. I would be grateful if you could help me accomplish this.

EDIT

I cannot get username from server side as username is the name of the user from facebook which I use the javascript API to get...

1
  • Why dont you get the username from server side and use in GetRouteUrl? Commented Nov 22, 2013 at 16:02

3 Answers 3

2

You should use some placeholder for username on server side, and replace it with real username on client side:

<script>
 var user = {
       id: 1,
       username: 'myname' 
};

window.location.href = '<%# Page.GetRouteUrl("member", New With {.member = "__USERNAME__"})%>'.replace('__USERNAME__', user.username);
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

very interesting solution that I have not thought of before.. Will try this and let you know.. Are you sure this is like the most optimal and correct way to do though? I mean is this how it is correctly made normally?
I accepted this answer because it simply 'works'. I am not too confortable with this solution however, it sounds better than having a server side literal.
real solution (without a literal) is to create a server-side action (for mvc) or some other handler (non-mvc); than post userId to that action from js, and redirect to correct url from server code. I can add a full sample for MVC, if needed.
0

Something like:

window.location.href = '<%# Page.GetRouteUrl("member", New With {.member = %>' + user.username + '<%})%>';

Comments

0

Simply you can't

explanation: at time 0: The asp.net code block will be evaluated at the server side

at time 1: The evaluated markup will be sent to the client (transforming all the server side code block to HTML)

at time 1.1: The JavaScript will be evaluated on the browser, so here you can't access the past

the golden rule is:

  • Server Side code can generate Client Side Code (e.g. Literal Control can generate HTML and JavaSCript)
  • Client Side code cannot generate Server Side Code (JavaScript or HTML concatenation cannot generate Literal Control)

e.g. passing data from server side to client side

<div><asp:Literal runat="server" text="ServerSideVariableOnly" /></div>

OR

<script>
 var pageurl = '<asp:Literal runat="server" text="AnotherServerSideVariable" />'
</script>

to do what you want you have two options:

  1. you have to make an ajax call (client <--- server) to get further information or,
  2. populate the required data before loading the page.

1 Comment

Well, you might be right. Just because you are right, I had to accept a 'hacky' solution. I cannot form my string in server side since it gets set in client side however I am sure this would also be a great answer for those who might run into a different situation than I currently have.. Thanks for the answer man..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.