Destiny Dawn
Destiny Dawn

Reputation: 1477

HTML In Textbox Error

I'm trying to allow HTML in my asp textbox but when I go as much as clicking the button it returns with this error:

Uncaught Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 404."

One the serverside I am using Server.HtmlEncode to encode the HTML in the textbox but still getting no result. I tried using break points, AutoEventWireup="false", ValidateRequest="false", and setting the page validation to false in the web.config. I also tried using ValidateRequest="false" on the page it's self. It's also returning this error A potentially dangerous Request.Form value was detected from the client (ctl00$ContentPlaceHolder1$testBox="<test"). The server side and client side code is below:

--Client side--
<asp:TextBox runat="server" ID="testBox" />
<asp:Button runat="server" OnClick="testHtmlEncode" />

--Server side--
protected void testHtmlEncode(Object sender, EventArgs e) {
console.write(Server.HtmlEncode(testBox.Text));
}

Upvotes: 1

Views: 3077

Answers (2)

Rob
Rob

Reputation: 5588

I got it to work with this aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox runat="server" ID="txtValue"></asp:TextBox>
        <asp:Button runat="server" ID="btnSubmit" OnClick="btnSubmit_Click" Text="submit"/>
    </div>
    </form>
</body>
</html>

this web.config

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5" requestValidationMode="2.0"/>
  </system.web>
</configuration>

this .aspx.cs

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Console.WriteLine(txtValue.Text);
    }
}

Upvotes: 1

Rob
Rob

Reputation: 5588

Try <httpRuntime requestValidationMode="2.0" /> in your web.config

Along with ValidateRequest="false" on the page.

Add the ID="btnSubmit" to you button

EDIT:

First, I would Google your original error. You should be able to make that work. Try stopping the development web server and re-running (icon in your taskbar).

https://www.google.com/?q=A+potentially+dangerous+Request.Form+value+was+detected+from+the+client

For a jQuery solution:

In .aspx.cs

[WebMethod]
  public static void MyMethod(string myTextBoxValue)
  {
    //do something
  }

In .aspx

var txtValue= $('#<%=testBox.ClientID%>').val();
        if (!txtValue) {
            txtValue= 0;
        }
        var objJSON = {
            myTextBoxValue: txtValue
        };
        var encoded = JSON.stringify(objJSON);

$.ajax({
  type: "POST",
  url: "PageName.aspx/MyMethod",
  data: encoded,
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
  }
});

EDIT 2: Make sure you aren't doing straight sql calls with whatever is in the text box, you will open yourself open to sql inject attacks. That's why asp.net was blocking the "<" in the first place.

EDIT 3 Not sure if it would matter, but your asp:Button has no ID attribute

Upvotes: 1

Related Questions