0

我为 ASP.NET WebForms 编写自己的控件。只有一个按钮。单击时,标签需要显示文本“Hello World”。但它没有发生。当我单击按钮时,标签文本不会改变。为什么?我的控制:

public class MyButton : Control, IPostBackEventHandler
{
    protected override void Render(HtmlTextWriter writer)
    {
        writer.AddAttribute("type", "submit");
        writer.AddAttribute("value", "ClickMe");
        writer.AddAttribute("name", "BtnChange");
        writer.RenderBeginTag(HtmlTextWriterTag.Input);
        writer.RenderEndTag();

        base.Render(writer);
    }

    public delegate void OnClickEventHandler(object sender, EventArgs args);

    public event OnClickEventHandler Click1;

    public void RaisePostBackEvent(string eventArgument)
    {
        Click1(this, new EventArgs());
    }
}

我的 ASP 页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestButton.aspx.cs" Inherits="Hello.TestButton" %>
<%@ Register assembly="Hello" namespace="Hello" tagPrefix="MyButton" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
  <body>
    <form id="form1" runat="server">
      <div>
      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
      <MyButton:MyButton runat="server" OnClick1="MyButton_OnClick"></MyButton:MyButton>
      </div>
    </form>
  </body>
</html>

和页面处理程序:

namespace Hello
{
  public partial class TestButton : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void MyButton_OnClick(object sender, EventArgs args)
    {
        Label1.Text = "Hello World!";
    }
  }
}
4

1 回答 1

0

您应该使用控件的 UniqueID 属性作为按钮的名称属性。

于 2013-06-04T15:14:18.290 回答