我正在创建动态控件,因为其中一个控件是一个按钮.我使用以下代码添加了按钮控件. [英] I am creating dynamic controls, in that one control is a button. i used the following code to add the button control.

查看:74
本文介绍了我正在创建动态控件,因为其中一个控件是一个按钮.我使用以下代码添加了按钮控件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以表格形式动态创建了按钮.

但是按钮事件不会触发此应用程序.

i created dynamically button in a form.

But button event are not fired this application.

Button btnContinue = new Button();
btnContinue.Attributes.Add("class", "button");
btnContinue.ID = "btnContinue";
btnContinue.Text = "Continue";
btnContinue.CausesValidation = false;
btnContinue.Click += new EventHandler(btnContinue_Click);                
form1.Controls.Add(btnContinue);

protected void btnContinue_Click(object sender, EventArgs e)
{
    ...
}

protected void Page_Init(object sender, EventArgs e)
{ 
    
}

推荐答案

首先,您必须在事件中创建按钮的新实例
按行

Button b =将发件人作为Button;

现在尝试通过实例b触发事件.
first you have to create a new instance of the button in the event
by the line

Button b = sender as Button;

now try to fire the event by the instance b.


这是必填字段验证器的示例


RequiredFieldValidator reqif =新的RequiredFieldValidator();
reqif.ID ="reqif";
reqif.ControlToValidate = txt2.ID;
reqif.ErrorMessage =输入单位号";
pnlInfo.Controls.Add(reqif);
this is the example for required field validtor


RequiredFieldValidator reqif = new RequiredFieldValidator();
reqif.ID = "reqif";
reqif.ControlToValidate = txt2.ID;
reqif.ErrorMessage = "Enter no. flats";
pnlInfo.Controls.Add(reqif);


这篇关于我正在创建动态控件,因为其中一个控件是一个按钮.我使用以下代码添加了按钮控件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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