【问题标题】:How to Edit .aspx page and Save in asp.net on button click using c#?如何使用 c# 编辑 .aspx 页面并在按钮单击时保存在 asp.net 中?
【发布时间】:2012-12-18 12:06:35
【问题描述】:

我是来自应用程序根目录的 DropdownList 中的 .aspx 页面列表。我在 RichTextBox 中显示整个 .aspx 页面内容以进行编辑。但是,当我单击保存按钮时,它显示错误:从客户端检测到潜在危险的 Request.Form 值(ctl00$MainContent$TextBox1="...xt" %>

此处的示例代码:

<asp:TextBox ID="TextBox1" runat="server" Height="314px" TextMode="MultiLine" 
        Width="771px"></asp:TextBox>

<asp:Button ID="BtnSaveContent" runat="server" onclick="BtnSaveContent_Click" 
        Text="Save Content" />

In .aspx.cs file:
private void GetFilesNames()
    {


        TextReader tr = new StreamReader(Server.MapPath("") + "/CopyText.aspx");
        TextBox1.Text = tr.ReadToEnd();
        // close the stream
        tr.Close();
    }


    protected void BtnSaveContent_Click(object sender, EventArgs e)
    {
        WritetoFile();
    }

    private void WritetoFile()
    {
        TextWriter tw = new StreamWriter(Server.MapPath("") + "/CopyText.aspx");

        // write a line of text to the file
        tw.WriteLine(TextBox1.Text);

        // close the stream
        tw.Close();
    }

页眉:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" validateRequest="false" CodeFile="CopyText.aspx.cs" Inherits="CopyText" %>

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" validateRequest = false
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

我还在页面级别和控制级别尝试了 validateRequest="false"。但不起作用! 任何想法来解决这个错误? 感谢您的帮助!

【问题讨论】:

    标签: c# asp.net writetofile textwriter


    【解决方案1】:

    你是正确的,你需要validateRequest = false

    但如果这是 .Net 4.0,您还需要将 requestValidationMode="2.0" 添加到 web.config 的 httpRuntime 配置部分:

    <system.web>
      <httpRuntime requestValidationMode="2.0"/>
    </system.web>
    

    【讨论】:

    • 请显示您的 aspx 页面的标题,例如&lt;%@ Page
    • 现在它不会抛出错误,但对文件的更改不会持续存在。关闭浏览器后,它显示“页面正在外部源更新,您要重新加载吗?”当我单击“是”时,它仍然保留上一页内容..!有什么原因吗?
    • 这个信息是什么?视觉工作室?在什么文件上? Web.Config?
    • 是VS2010 v4.0框架!消息:“文件已在源代码编辑器之外修改,您要重新加载吗?”有两个选项是/否,YESTOALL/NOTOALL!关闭浏览器后。
    • 我的意思是,这条消息在哪里显示?在 Visual Studio 中还是在浏览器中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多