[英]Why my button is not calling my function?
I'm trying to call a function to change the contents of a <p>
tag, it appears that my function is not being called, thanks in advance.我正在尝试调用 function 来更改
<p>
标签的内容,看来我的 function 没有被调用,在此先感谢。
I've checked that all the tags have runat="server"
, and making sure the function name is correct.我检查了所有标签是否都有
runat="server"
,并确保 function 名称正确。
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>
I assume you want to run that code server-side.我假设您想在服务器端运行该代码。 I see two problems:
我看到两个问题:
You will need to use an ASP Button control , like <asp:Button>
.您将需要使用ASP Button 控件,例如
<asp:Button>
。 Look at that documentation for details about how to use it.有关如何使用它的详细信息,请查看该文档。 The
<button>
tag is not an ASP control, so that onclick
is running client-side. <button>
标签不是 ASP 控件,因此onclick
正在客户端运行。 It is looking for a javascript function.它正在寻找 javascript function。
You declared that method inside Page_Load
and with the wrong signature for an event.您在
Page_Load
中声明了该方法,并且使用了错误的事件签名。 Move it out of Page_Load
, under the class:将其移出
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;
}
Also, Yeah
isn't the usual naming convention, but it would still work.此外,
Yeah
的,这不是通常的命名约定,但它仍然可以工作。
I am unsure if that even compiles.我不确定这是否可以编译。 Right now you have a function
Yeah()
within the function Page_Load(object sender, EventArgs e)
.现在你在 function
Page_Load(object sender, EventArgs e)
中有一个 function Yeah()
) 。
And are trying to register Yeah() for a event, despite it clearly not being a event handler candidate (totally wrong signature).并试图为一个事件注册 Yeah(),尽管它显然不是一个事件处理程序候选者(完全错误的签名)。 And also about a dozen Brackets out of scope.
还有大约十几个支架来自 scope。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.