C#按钮不触发? [英] C# Button Not Firing?

查看:160
本文介绍了C#按钮不触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我创建一个控制的codeBhind一个ASP按钮。这里是code:

I have an ASP Button that I am creating in the CodeBhind of a Control. Here is the code:

Button SubmitButton = new Button();

protected override void CreateChildControls()
{
    SubmitButton.Text = "Submit";
    SubmitButton.Click += new EventHandler(SubmitButton_Click);
}

private void SubmitButton_Click(object sender, EventArgs e)
{
    CustomTabs.CreateNewTab();
}

Click事件将不火,虽然。它看起来像它回传和嗟事件。任何想法?

The Click event won't fire, though. It appears like it does a postback and never hits the event. Any ideas?

推荐答案

所以,你有没有解释的一些方法,但在添加提交按钮来的页面?某处应该是:

So you have some methods that are not explained, but are you adding the SubmitButton to the page? Somewhere should be:

SomeServerControl.Controls.Add(SubmitButton);

对我来说,以下工作(显然改变命名空间):

The following works for me (Obviously change namespace):

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="VS2010WEBFORMS.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>

ASPX.CS

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

namespace VS2010WEBFORMS
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        Button SubmitButton = new Button();

        protected void Page_Load(object sender, EventArgs e)
        {
            SubmitButton.Text = "Submit";
            SubmitButton.Click += new EventHandler(SubmitButton_Click);
            form1.Controls.Add(SubmitButton);
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {
            //Something
        }
    }
}

这篇关于C#按钮不触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆