点击在asp.net按钮 [英] click on button in asp.net

查看:97
本文介绍了点击在asp.net按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态创建一个按钮,并将其放置在一个占位符,如下

I am creating a button dynamically and place it in a placeholder as below

<asp:Button ID="generateTableSchema" runat="server" Text="Generate Table" OnClick="generate_Click" />

 protected void generate_Click(object sender, EventArgs e)
    {
Button button = new Button();
button.Text = "Generate Table";
button.ID = "generateTable";
button.OnClick = hello();
PlaceHolder1.Controls.Add(button);
}

但onclick事件不点火。

but onclick event is not firing.

这是我收到的错误

System.Web.UI.WebControls.Button.OnClick(System.EventArgs)' is inaccessible due to its protection level

您好是如下...

public void hello()
    {
        Label1.Text = "heellllllllllo";
    }

什么是错在这里????

What's wrong here????

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
        }
        else
        {
            button.Click += ButtonClick;
        }
    }

@Darenü意味着像这样...

@Daren u mean like this...

推荐答案

由于您要添加的按钮编程的盟友必须添加事件处理程序。

Because you are adding the button programmatic ally you have to add the event handler.

所以这会工作。

修改
包裹内按钮的Pa​​ge_Load

EDIT Wrapped the button INSIDE Page_Load

protected void Page_Load(object sender, EventArgs e)
{
 Button button = new Button();
 button.Text = "Generate Table";
 button.ID = "generateTable";
 button.Click += hello;    /// THIS is the handler
 PlaceHolder1.Controls.Add(button);
}

ButtonClick将是你的方法的名称。

ButtonClick would be the name of your method.

    protected void hello(Object sender, EventArgs e)
    {
     // ...
    }

另外,你在运行时产生这个需要makesure这个被调用的回传也。

Also, as you're generating this at runtime you need to makesure this gets called on postbacks too.

这篇关于点击在asp.net按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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