Abhay Phadatare
Abhay Phadatare

Reputation: 1

Microsoft JScript runtime fault

protected void Button1_Click(object sender, EventArgs e)
{
    if (bittext.Text == "")
        Response.Write("<script>alert('Enter The value first or press the correct button')</script>");
    else
    {
        b = double.Parse(bittext.Text);
        Bytetext.Text = (b / 8).ToString();
        kbtext.Text = (b / 8192).ToString();
        mbtext.Text = (b / 8388608).ToString();
        gbtext.Text = (b / 8589934592).ToString();
        tbtext.Text = (b / 8796093022000).ToString();
    }
}

If I am running this web C# code without entering anything in bittext.Text, this throws an error named:

Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.

How can I solve this?

Upvotes: 0

Views: 589

Answers (1)

Nick Craver
Nick Craver

Reputation: 630429

You can't use Response.Write() for this, you need ScriptManager.RegisterStartupScript, like this:

ScriptManager.RegisterStartupScript(this, GetType(), "alert",
  "alert('Enter The value first or press the correct button');", true);

Response.Write() just dumps that script tag whenever it is in the response, which makes for an invalid/unexpected format when the client goes to parse it, you want to properly execute a piece of JavaScript...above is the correct/provided mechanism for doing that.

Upvotes: 2

Related Questions