2

I have an aspx page with some c# code blocks in it and I am trying to use a variable from the first block in the second block. I've tried lots of different things to make the variable public and available but haven't managed it. Can someone point out/give me a clue as to where I am going wrong please?

This is some of my code:

<%@ Page Language="C#"%>

<%
  string myVar;          
  myVar = ”Dan”;
%>

<script>

function buildHyperlinks(){
    try{
        $(<a href ='http://myServer/sites/page.aspx?params=<% Response.Write(myVar);%>'>"`
    }catch(err){
        setTimeout(buildHyperlinks(), 5000); 
    }       
}


</script>

Thanks, Dan

2 Answers 2

2

Why do you not decalre your variable in your code-behind? Then it should be available everywhere.

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

3 Comments

Hi - I'm not sure what you mean by code behind? if you mean code behind this page I don't have any yet I suppose I could include some but I'm not sure how...
If you use Visual Studio, it should always create ans .aspx and .cs file together. The Code-Behind file is linked in the <%@ Page Language="C#"%>. This is a good tutorial: Code-Behind Model Overview
Ahh - I'll give that a try and post my results - Im not using visual studio just a text editor.
1

define your variable inside script runat="server" directive, like this:

<script runat="server">
    string myVar = "Dan";
</script>

This variable should be available through all your code.

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.