简体   繁体   中英

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.

I've checked that all the tags have runat="server" , and making sure the function name is correct.

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" %>

Test Case


<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:

  1. You will need to use an ASP Button control , like <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. It is looking for a javascript function.

  2. You declared that method inside Page_Load and with the wrong signature for an event. Move it out of Page_Load , under the 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.

I am unsure if that even compiles. Right now you have a function Yeah() within the function Page_Load(object sender, EventArgs e) .

And are trying to register Yeah() for a event, despite it clearly not being a event handler candidate (totally wrong signature). And also about a dozen Brackets out of scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM