62,046
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="post.aspx.cs" Inherits="post" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<script language=javascript>
//此函数用于将ImageHead.aspx页面的选择结果的图片路径作为函数值。ImageHead.aspx页面功能是用户点击选择图片。
function deal() {
var someValue;
someValue = window.showModalDialog('ImageHead.aspx', '', 'Width=400;Height=200;status=no;help=no;scrollbars=no');//以窗口的形式打开页面ImageHead.aspx;
if (someValue == undefined) {
someValue = "1";
}
return document.getElementById("body").style.backgroundImage = "url('images/image" + (parseInt(someValue) + 1) + ".jpg')";//最主要功能是想把这里最终用户选择的图片路径传递给posttwo.aspx页面用于生成一个静态html页,图片路径作为静态html页的页面背景图片。
}
//此函数用于将上面函数deal()的返回值给页面的input
function canshu(pad) {
var padd = pad;
var newElement = document.getElementById("can");
newElement.value = pad;
}
</script>
<form id="form1" method="post" action="posttwo.aspx">
<div style="width:1024;height:1000;" id="body">
<input type="text" id="can" name="lcan"/>
<input type="submit" value="选择页面背景" onclick="canshu(deal())"/><%--在这里调用上面JS函数canshu()将图片路径利用form 的Post传递到页面posttwo.aspx--%>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="posttwo.aspx.cs" Inherits="posttwo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<div id="" style="width:1024;height:1000;">
<form id="form2" runat="server">
<div>
输入框1:<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox><br />
输入框2:<asp:TextBox ID="txtContent" runat="server"></asp:TextBox><br />
输入框3:<asp:TextBox ID="Jieshao" runat="server"></asp:TextBox><br />
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="生成静态页面" /><br /><%--点击此按钮生成静态页面到文件夹htm。--%>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.IO;
public partial class posttwo : System.Web.UI.Page
{
string canshu;
protected void Page_Load(object sender, EventArgs e)
{
canshu = Request.Form["lcan"];//接收post.aspx页面传来的图片路径给变量canshu
}
protected void Button1_Click(object sender, EventArgs e)
{
//源码是替换掉模板中的特征字符
string mbPath = Server.MapPath("JTHTML.htm");//读取静态模板页。
Encoding code = Encoding.GetEncoding("utf-8");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
//读取
try
{
sr = new StreamReader(mbPath, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
//根据时间自动重命名,扩展名也可以自行修改
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
str = str.Replace("$title$", txtTitle.Text);//替换Title
str = str.Replace("$content$", txtContent.Text);
str = str.Replace("$jieshao$", Jieshao.Text);
str = str.Replace("$imgurl$", canshu);//这里想用传递来的图片路径替换掉字符串$imgurl$
//生成静态文件
try
{
sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!");
}
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div style="width:1024;height:1000;background-image:$imgurl$"><!--传递到posttwo.aspx页面的图片路径最终通过生成静态,图片路径替换掉这里的字符串$imaurl$-->
<h1>$title$</h1>
<h2>$content$</h2>
<h3>$jieshao$</h3>
</div>
</body>
</html>
string canshu
{
get{return ViewState["urlparam"] as string;}
set {ViewState["urlparam"] = value;}
}
把你的canshu字段改成属性,通过ViewState持久,就满足你的要求了,你早说在Page_Load事件里面能难道canshu早就解决了
因为ASP.NET是无状态的,Page_Load和Click是两次回发了
另外注意加IsPostBack判断,这样在回发时就不会重复赋值
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
canshu = Request.Form["lcan"];//接收post.aspx页面传来的图片路径给变量canshu
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.IO;
public partial class posttwo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string canshu = Request.Form["lcan"];//接收post.aspx页面传来的图片路径给变量canshu
string mbPath = Server.MapPath("JTHTML.htm");//读取静态模板页。
Encoding code = Encoding.GetEncoding("utf-8");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
//读取
try
{
sr = new StreamReader(mbPath, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
str = str.Replace("$imgurl$",canshu);
try
{
sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!");
}
}
//protected void Button1_Click(object sender, EventArgs e)
//{
// //源码是替换掉模板中的特征字符
// string mbPath = Server.MapPath("JTHTML.htm");//读取静态模板页。
// Encoding code = Encoding.GetEncoding("utf-8");
// StreamReader sr = null;
// StreamWriter sw = null;
// string str = null;
// //读取
// try
// {
// sr = new StreamReader(mbPath, code);
// str = sr.ReadToEnd();
// }
// catch (Exception ex)
// {
// throw ex;
// }
// finally
// {
// sr.Close();
// }
// //根据时间自动重命名,扩展名也可以自行修改
// string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
// str = str.Replace("$title$", txtTitle.Text);//替换Title
// str = str.Replace("$content$", txtContent.Text);
// str = str.Replace("$jieshao$", Jieshao.Text);
// //这里想用传递来的图片路径替换掉字符串$imgurl$
// //生成静态文件
// try
// {
// sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
// sw.Write(str);
// sw.Flush();
// }
// catch (Exception ex)
// {
// throw ex;
// }
// finally
// {
// sw.Close();
// Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!");
// }
//}
}