我在一个aspx页面上有以下page_load方法。现在,我想在字符串变量的另一个aspx页面中获取字符串xmlData。有没有办法让我可以在另一个页面上获取xmlData变量的值?
protected void Page_Load(object sender, System.EventArgs e)
{
string xmlData = "text for this example"
Response.Write(xmlData);
}
答案 0 :(得分:0)
protected void Page_Load(object sender, System.EventArgs e)
{
string xmlData = "text for this example";
Session["xmlData"] = xmlData;
}
protected void Page_Load(object sender, System.EventArgs e)
{
string xmlData = "text for this example";
ViewState["xmlData"] = xmlData;
}
您可以使用session或ViewState将字符串数据存储在另一个页面中。
会话保持到整个会话页面,需要销毁。 就像Session [" xmlData"] =""。下一页中的ViewState变量自动销毁。
答案 1 :(得分:0)
在这里,您可以找到发布xml http://www.codeproject.com/Articles/10430/Post-XML-Data-to-an-ASP-NET-Page-using-C的完整解决方案: 发送xml:
WebRequest req = null;
WebResponse rsp = null;
try
{
string fileName = "C:\test.xml";
string uri = "http://localhost/PostXml/Default.aspx";
req = WebRequest.Create(uri);
//req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
req.Method = "POST"; // Post method
req.ContentType = "text/xml"; // content type
// Wrap the request stream with a text-based writer
StreamWriter writer = new StreamWriter(req.GetRequestStream());
// Write the XML text into the stream
writer.WriteLine(this.GetTextFromXMLFile(fileName));
writer.Close();
// Send the data to the webserver
rsp = req.GetResponse();
}
catch(WebException webEx)
{
}
catch(Exception ex)
{
}
finally
{
if(req != null) req.GetRequestStream().Close();
if(rsp != null) rsp.GetResponseStream().Close();
}
private string GetTextFromXMLFile(string file)
{
StreamReader reader = new StreamReader(file);
string ret = reader.ReadToEnd();
reader.Close();
return ret;
}
阅读xml:
private void Page_Load(object sender, EventArgs e)
{
page.Response.ContentType = "text/xml";
// Read XML posted via HTTP
StreamReader reader = new StreamReader(page.Request.InputStream);
String xmlData = reader.ReadToEnd();
}
答案 2 :(得分:0)
这基本上就是结构。
//你无法从前端加载这个页面
public partial class page1
{
protected void Page_Load(object sender, System.EventArgs e)
{
string xmlData = "text for this example"
Response.Write(xmlData);
}
}
//第二页 - 可以从按钮点击前端加载此页面
public partial class page2
{
protected void Page_Load(object sender, System.EventArgs e)
{
string xmlData = //code here to get string from page1
}
}
答案 3 :(得分:0)
以下代码对我有用 -
StringWriter writer = new StringWriter();
Server.Execute("/page1.aspx", writer);
现在,writer具有在page1.aspx中的xmlData变量中编写的值。
谢谢你们的时间。