如何将值从aspx.cs传递给aspx

时间:2017-05-05 12:31:24

标签: c# html asp.net

我经历了很多帖子来解决我的问题,出于某种原因,我尝试过的解决方案都没有。 我只是想从我的aspx.cs传递一个值(字符串)到我的aspx(html页面)。

这是我尝试过的: 我的aspx.cs:

library(plotly)
library(tidyr)
library(plyr)

data <- spread(Orange, Tree, circumference)
data <- rename(data, c("1" = "Tree1", "2" = "Tree2", "3" = "Tree3", "4" = "Tree4", "5" = "Tree5"))

#hiding entries

p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1') %>%
  add_trace(y = ~Tree2, name = 'Tree 2') %>%
  add_trace(y = ~Tree3, name = 'Tree 3', showlegend = FALSE) %>%
  add_trace(y = ~Tree4, name = 'Tree 4') %>%
  add_trace(y = ~Tree5, name = 'Tree 5')

##no legend

p <- plot_ly(data, x = ~age, y = ~Tree1, type = 'scatter', mode = 'lines', name = 'Tree 1',showlegend = FALSE) %>%
  add_trace(y = ~Tree2, name = 'Tree 2') %>%
  add_trace(y = ~Tree3, name = 'Tree 3', showlegend = TRUE) %>%
  add_trace(y = ~Tree4, name = 'Tree 4') %>%
  add_trace(y = ~Tree5, name = 'Tree 5')

这里是我在aspx中做的不同测试: 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; } <div><%=ResponseREST%></div> <div><%=strResponseValue%></div>

这是我得到的错误:

  

BC30451&#39; ResponseREST&#39;没有宣布。由于其保护级别,它可能无法访问。

非常感谢任何帮助。我对c#很新,所以我可能会遗漏一些简单的东西。

由于

3 个答案:

答案 0 :(得分:1)

由于您是C#中的新手,因此建议尽量避免使用.aspx来混合<%= ... %>文件中的C#代码和HTML。这通常是一个坏主意,当代码库变大时会导致许多混淆。

另一种解决方案是在标记中使用Literal服务器控件,只要您想显示ResponseREST

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

从代码隐藏(aspx.cs文件)中设置像这样的值

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

答案 1 :(得分:1)

我能够让它发挥作用。你们提供的不同解决方案有效。但只有在我添加该行之后:

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

谢谢大家的帮助!

答案 2 :(得分:0)

您还需要指定一个设定值。

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";
    }
}