0

I am currently trying to migrate AngularJS Code in ASPX to React. I would like to pass some variables of aspx to .jsx files which it can be used by react component.

Mainpage.aspx contains some variables which needs to be passed to Client.jsx(which is in React).

I am trying to access this variable noSessionText from .aspx file to .jsx and i am failing to achieve it.

aspx code

<asp:Content ID="BodyContent" ContentPlaceHolderID="ContentPlaceHolderMiddlePanel" runat="Server">
  var noSessionText = '<%= Resources.WebPageResource.SessionSharing_NoSessions_Text %>';
      var HeaderName = '<%= Resources.WebPageResource.SessionSharing_Header_Name %>';
  <!--test code-->
      <script src="https://unpkg.com/react@15/dist/react.js"></script>
      <script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
      <script src="https://unpkg.com/[email protected]/babel.min.js"></script>
      <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
      <script  type="text/babel" src="includes/scripts/Client.jsx"></script>

  <div id="SessionClient_root" ></div>       

    </div>
  </div>
</asp:Content>

jsx code

class Client extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            activeSessions: {},
            headerUserSession: props.HeaderName
        };
    }
    renderSessionDetails() {
        console.log("inside rendersessiondetails");
        console.log("headerusersession from aspx->react" + this.state.HeaderName);
    }

    render() {

        return (
            <div>
                <h3 className="sessionmanager__headline">SESSIONCLIENT IN REACT</h3>
                {this.renderSessionDetails()}
            </div>
            );
    }
}
ReactDOM.render(
    <Client />,
    document.getElementById('SessionClient_root')
);

Is there any way which i could achieve this. Thanks in advance for help.

2 Answers 2

1

You can register a client side script block Create javaScript variable in code behind of asp.net

This will enable you to declare javascript variables that the jsx could access.

Sign up to request clarification or add additional context in comments.

1 Comment

i was checking the page you have shared. i have already added in my .aspx file inside script tag the variables as mentioned.. but i am still not clear how can i access it in my .jsx file..
1

changes in my .aspx file let noSessionText = '<%= Resources.WebPageResource.SessionSharing_NoSessions_Text %>'; let HeaderName = '<%= Resources.WebPageResource.SessionSharing_Header_Name %>';

.jsx file to access the above variables just use the variable name alert(noSessionText);

Steps to be considered: 1. add the variables in tag with type let. var should not be used. 2. by adding it in script tag it will be globally available. 3. from other js files you can easily access the variables which are defined globally in other js files.

Comments

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.