ASP.NET Inline Coding Fundamental Questions

2019-09-19 02:46发布

I am having a difficult time debugging my assignment. The assignment is due as a single aspx page. My difficulty I believe is partially caused by my inability to properly debug my aspx page in Visual Studios by using breakpoints.

My first question: Is this possible to do?

All of the resources I discovered online have said the solution is to create a separate class file.

Before I go about doing that I just wanted to be sure that there is no other way to go about using breakpoints within a script block.

My next question pertains to the format of an inline script block. My particular confusion relates to the use of global variables. Since it is in a script tag, is there anything different I need to be aware of like when they fall out of scope? If I declare an ArrayList in the global scope of the script and use the Add method on button click, will the result be identical to that of similar code behind?

Here is some simple code to demonstrate my confusion:

<script runat="server">

    ArrayList al = new ArrayList();

    protected void Button1_Click(object sender, EventArgs e)
    {
        al.Add(TextBox1.Text);
    }

</script>

4条回答
混吃等死
2楼-- · 2019-09-19 03:05

if it doesnt get fired, please check if Button1 still has the Button1_Click event binded. When you kept playing around with the code, you might have destroyed event binding. Please check ig the event is still binded to the Button1

查看更多
小情绪 Triste *
3楼-- · 2019-09-19 03:07

From KB Article 303247:

ASP.NET supports two methods to author pages:

  • In-line code
  • Code-behind

In-Line Code

In-line code is code that is embedded directly within the ASP.NET page. The following code represents a sample ASP.NET page that includes in-line code:

Myinlinecode.aspx

<%@ Language=C# %>
<HTML>
<script runat="server" language="C#">
void MyButton_OnClick(Object sender, EventArgs e)
{
  MyLabel.Text = MyTextbox.Text.ToString();
}
</script>
<body>
  <form id="MyForm" runat="server">
     <asp:textbox id="MyTextbox" text="Hello World" runat="server"></asp:textbox>
     <asp:button id="MyButton" text="Echo Input" OnClick="MyButton_OnClick" runat="server"></asp:button>
     <asp:label id="MyLabel" runat="server"></asp:label>
  </form>
</body>
</HTML>

That example done, if you want/need something to be global, a good thing to use are extra controls in your HTML.

Consider this modified version of MS's code:

<%@ Language=C# %>
<HTML>
<script runat="server" language="C#">
void MyButton_OnClick(Object sender, EventArgs e)
{
  MyLabel.Text = MyTextbox.Text.ToString();
  hidden1.Value = DateTime.Now.ToString();
}
void Button2_Clicked(Object sender, EventArgs e)
{
  MyLabel.Text = string.Format("MyButton was clicked at {0}.", hidden1.Value);
}
</script>
<body>
  <form id="MyForm" runat="server">
     <asp:textbox id="MyTextbox" text="Hello World" runat="server"></asp:textbox>
     <asp:button id="MyButton" text="Echo Input" OnClick="MyButton_OnClick" runat="server"></asp:button>
     <asp:label id="MyLabel" runat="server"></asp:label>
     <hr/>
     <asp:HiddenField ID="hidden1" runat="server" />
     <asp:button id="Button2" text="Button2" OnClick="Button2_Clicked" runat="server"></asp:button>
  </form>
</body>
</HTML>

Notice the asp.HiddenField is used to store data on the HTML page, and it can be accessed later.

If you want to see more about this, do a search using "aspx in-line code".

查看更多
Melony?
4楼-- · 2019-09-19 03:13

This is a common problem that has been asked a number of times. See

Why can't I debug my asp.net web app

Why can't I debug my asp.net web project anymore?

Why am I unable to Debug my ASP.NET website in Visual Studio?

Unfortuantely, since it can arise from a number of sources, you pretty much have to work through the stack of possible problem resolutions.

查看更多
萌系小妹纸
5楼-- · 2019-09-19 03:13

There is absolutely no problem debugging code in the ASPX file. I do this all the time.If it will actually compile, then you should be able to set breakpoints within your code. You haven't been clear about exactly what problem you are having?

Regarding your second question, "global" variables exist for the life of the page. But note that the page only exists when it gets loaded. When the page has rendered, it is no longer needed.

查看更多
登录 后发表回答