简体   繁体   中英

How to pass value from aspx.cs to aspx

I've been going through many posts to fix my problem, and for some reason, none of the solutions I tried worked. I'm simply trying to pass a value (string) from my aspx.cs to my aspx (html page).

Here is what I tried: My aspx.cs :

public enum httpVerb
{
    GET,
    POST,
    PUT,
    DELETE
}

public string endPoint { get; set; }
public httpVerb httpMethod { get; set; }

public string strResponseValue;

public string ResponseREST { get { return strResponseValue; } }
public string makeRequest()
{
    strResponseValue = string.Empty;

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);

    request.Method = httpMethod.ToString();

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode != HttpStatusCode.OK)
        {
            throw new ApplicationException("error code: " + response.StatusCode.ToString());
        }
        //Process the response stream.. (could be JSON...)
        using (Stream responseStream = response.GetResponseStream())
        {
            if (responseStream != null)
            {
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    strResponseValue = reader.ReadToEnd();
                }//End of StreamReader
            }

        }//end of using ResponseStream

    }//End of response

    return strResponseValue;

}

and here are differents test I did in my aspx: <div><%=ResponseREST%></div> <div><%=strResponseValue%></div> <div><%=makeRequest()%></div>

This is the error I get:

BC30451 'ResponseREST' is not declared. It may be inaccessible due to its protection level.

Any help would be greatly appreciated. I'm fairly new to c#, so I might be missing something simple.

Thanks

Since you are fairly new in C#, a suggestion would be not to mix C# code and HTML in the .aspx file by avoiding <%= ... %> as much as possible. It is generally a bad idea and lead to many confusions when codebase grows larger.

Another solution is to use a Literal server control in the markup wherever you want to show the ResponseREST

<asp:Literal ID="MyLiteral" runat="server"></asp:Literal>

From the code-behind (the aspx.cs file) set the value like this

MyLiteral.Text = "text I want to see";

I was able to make it work. The different solutions that you guys offered work. But only after I added that line:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LeafletHTML.aspx.cs" Inherits="LeafletHTML" %>

Thank you all for the help!

You need to assign a set value as well.

public partial class _Default : System.Web.UI.Page
{
    string strResponseValue;

    public string ResponseREST 
    { 
        get { return strResponseValue; } 
        set { strResponseValue = value; } 
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        strResponseValue = "This is a test";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM