为什么我的按钮没有调用我的函数?

Why my button is not calling my function?

我正在尝试调用一个函数来更改 <p> 标记的内容,看来我的函数没有被调用,在此先感谢。

我检查了所有标签都有 runat="server",并确保函数名称正确。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StepFollowingDemo
{
    public partial class Test1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            void Yeah()
            {
                string Some = Something.Value;
                this.Result.InnerHtml = Some;
            }
       }

    }
}

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Test1.aspx.cs" Inherits="StepFollowingDemo.Test1" %>

测试用例


<label runat="server" for="Something">Type:</label>
<input runat="server" type="Text" class="from-control" id="Something"/>
<button runat="server" type="button" onclick="Yeah();" class="btn btn-success">Project your words</button>
<br /><br />

<label runat="server" for="Result">Output: </label>
<p runat="server" id="Result"></p>

我假设您想 运行 该代码服务器端。我看到两个问题:

  1. 您需要使用 ASP Button control,例如 <asp:Button>。查看该文档以了解有关如何使用它的详细信息。 <button> 标签不是 ASP 控件,因此 onclick 是 运行ning 客户端。它正在寻找 javascript 函数。

  2. 您在 Page_Load 中声明了该方法,并且使用了错误的事件签名。把它移出Page_Load,在class下:

protected void Page_Load(object sender, EventArgs e)
{
}

protected void Yeah(object sender, CommandEventArgs e)
{
    string Some = Something.Value;
    this.Result.InnerHtml = Some;
}

此外,Yeah 不是通常的命名约定,但它仍然有效。