0

The situation is I want to add new button in my html aspx file (existing file). But I keep getting Server Error in '/' Application (Runtime Error) in the page after adding new button. Below is the error:

enter image description here

If I remove back the html markup, the page come back OK.

enter image description here

<asp:button id="btnSelect" runat="server" onclick="select_Click" text="Select"/>     

Then, I add inside html markup page , for example below, the Select Button will show a popup message for Selected Date, it's ok like I want. But I cannot add query to save the selected date into SQl database here because the select_Click button event is inside html markup aspx page. It's look like this button is inside Content Control and I'm not allowed to add button inside the existing file of html markup aspx. Then, when I add select_Click event function in my Example.aspx.cs page the button will do nothing. Supposedly the select_Click event function is inside Example.aspx.cs file.

Anyone got any ideas why this happen??? Note: The existing file is created by previous developer.


<script runat="server">
    
    protected void submit_Click(object sender, EventArgs e)
    {
        string targetdate = Request.Form[TargetDate.UniqueID];
        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Selected Date: " + targetdate + "');", true);

        
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="container">
        <table id="table2" style="text-align: center; width: 100%">
            <tr>
                <td class="auto-style1" style="text-align: center; height: 51px;">
                    <asp:Label ID="lblTitle" runat="server" Text="Case Detail" Font-Bold="True" Font-Size="XX-Large" Font-Names="Verdana" Font-Underline="True"></asp:Label></td>
            </tr>
       </table>
   </div>

</asp:Content>

Thankyou for your time by reading this.

2
  • First step is to find out what the actual error is, then add that to your question Commented Feb 14, 2023 at 1:41
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Feb 14, 2023 at 11:27

1 Answer 1

0

Well, first there is "zero" advantage to put the code inside of a script tags with runat=server.

You will get 100% the SAME effect if you just move that code behind.

(and I suggest you do).

However, looking close at your page, we see it is a "child" page. (in other words, you have a master page in effect here).

Remember, ALL THAT using script with runat=server does is inject the code in CODE behind.

but, that means of course you have to place that good old plain jane code stub inside of the content part since that is ALSO where the current page is being injected/placed.

So, really, I would suggest that you DUMP this idea, and there is no advantage I can think of. Why put code in the markup when you WILL AND ALREADY have a code behind page?

Now, all you doing is creating two places to look at code, and worse yet, when you use ?

All it does is move the code to code behind location anyway!!!

So, your example has to be this:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <script runat="server">

        protected void submit_Click(object sender, EventArgs e)
        {
            string targetdate = Request.Form[TargetDate.UniqueID];
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Selected Date: " + targetdate + "');", true);


        }
    </script>


    <div class="container">
        <table id="table2" style="text-align: center; width: 100%">
            <tr>
                <td class="auto-style1" style="text-align: center; height: 51px;">
                    <asp:Label ID="lblTitle" runat="server" Text="Case Detail" Font-Bold="True" Font-Size="XX-Large" Font-Names="Verdana" Font-Underline="True"></asp:Label></td>
            </tr>
        </table>
    </div>

</asp:Content>

In other words, you have to place that code inside of the "content", since that is where all your markup and "code" has to be placed, and that REALLY amounts to just placing that "stuff" into the "form" tag area on a regular (non master/child page).

However, as I pointed out, really?

Just take that code, cut it out, right click, view->code, and then just paste that code into the code behind. (remove the script tags).

The result will be 100% the same operation.

So, just keep in mind that using "script" with runat="server"?

All it really does is just move that code to the code behind module, and you not find nor see any difference (then if you just had placed that code in the code behind module anyway!).

However, as noted, since you have a master/child page?

Then both markup, and any code for that "child" page?

it has to be placed inside of the content template.

I should also point out in your sample markup? I don't see a button that would run that code stub anyway - so that's missing and that makes even less sense.

So assuming you did add a button inside of that content template?

Then it most certainly could have a click event that runs that button click code stub you have.

So keep in mind the rules that apply to JavaScript code?

They do NOT apply to code behind, and thus the placement rules for script vs script=runat=server does NOT apply in ANY way at all here.

Script tags with runat=server means:

Please move that server side into the code behind code module for that given page. The code running will be 100% the same as if you had placed that server side code directly into the code behind module in the first place. (again: 100% the same result).

So, yes, you should be able to place a button inside of the content template.

like this:

enter image description here

Sign up to request clarification or add additional context in comments.

12 Comments

Yes I've done like what you said here before by placing the submit_Click event code inside module page (for example: Example.aspx.cs) but the code got Error (Runtime Error) in the html markup page because adding the new button. In other words, when I'm adding any new button in html markup page, will got Runtime Error.
check an existing page, your setup might need/want to use content2. So, yes, you should be able to drop a button into content area. flip page into design mode, and now double click on that button - you should now be jumped to code behind. However, it possible that content1 is a page heading or footer - so code would not normally go into that location. You can open up site master to take a quick look, and you see the content page - pick the content page that is inside of the form tags. (it probably has more then one content section).
see my edit for an example
Yes, I'm doing as you said and got error. I'm doing in development mode server. In my localhost don't get any problem when adding new button.
Yes, I'm doing just like you show on your video. Btw, thanks for helping
|

Your Answer

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