0

I got an ASPX page with the folowing code behind

public partial class test : Page
{
    protected void test(object sender, EventArgs e)
    {
         throw new Exception("test");
    }
}

And the following ASPX code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Thumbnail</title>
</head>
<body>
    <form id="form1" runat="server">
    <div id="buttonTarget">

    </div>
    </form>
</body>
</html>

If I run the following javascript a button is added to the page:

$('#buttonTarget').html('<asp:Button runat="server" ID="tst" CssClass="buttons" OnClick="Test" Text="Test"/>');

The buttons shows the same way as an asp tag shows in element inspector. And when I click the button the server sided function is called and the site breaks with the "test" exception

I know this isn't good practice but I want to know why this works. Why does this button call the server sided function and why is it displayed as a normal button ?

--EDIT--

The aspx code was a simplified version. The actual code used a gridview control and used javascript to insert rows in the table. These rows hold the tags.

9
  • 2
    You can't do that. Your browser has no idea what ASPX tags are. You do know that the browser just runs HTML/CSS/JS etc? Commented Mar 20, 2017 at 15:31
  • I thought it would not work either but I just saw a working example. Commented Mar 20, 2017 at 15:33
  • Probably it works via callback Commented Mar 20, 2017 at 15:34
  • 2
    It is working because you are writing the JavaScript in an aspx page. Commented Mar 20, 2017 at 15:35
  • @Mamun What are you talking about? Can you expand more on this explanation. Commented Mar 20, 2017 at 15:41

1 Answer 1

1

Expanding on what @Mamun was probably saying, when the page is executing on the server, it's seeing the asp tag in the JS string and translating it into the appropriate HTML. If you view source on your page in the browser, you'll probably see something like this instead of the ASP tag in your JS call:

$('#buttonTarget').html('<input type="submit" name="ctl00$MainContent$tst" value="Test" id="MainContent_tst" class="buttons" />');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.