我在web项目之外的,另外一个类库项目里某个类里,动态创建了一个按钮。如何创建这个按钮的事件呢?做了几次都不成功。
//这个事件是由UI层页面一个按钮单击触发的,这个无关紧要吧。
void CreateButton()
{
System.Web.UI.Page currentPage = HttpContext.Current.Handler as Page;
Button b = new Button();
b.Click += new EventHandler(b_Click);//与下句的顺序调换,仍旧不行。
currentPage.FindControl("form1").Controls.Add(ButtonExcStep2);
}void ButtonExcStep2_Click(object sender, EventArgs e)
{
//该按钮的一些事件,跟踪了,根本就不执行。
}
查过资料说如果在aspx.cs页面这么写,必须放在page_load里!ispostback外,这样的位置。
可问题是我动态创建的不在aspx.cs页面,那怎么做呢?请注意,在"非aspx.cs"动态创建按钮,创建它的事件。谢谢
//这个事件是由UI层页面一个按钮单击触发的,这个无关紧要吧。
void CreateButton()
{
System.Web.UI.Page currentPage = HttpContext.Current.Handler as Page;
Button b = new Button();
b.Click += new EventHandler(b_Click);//与下句的顺序调换,仍旧不行。
currentPage.FindControl("form1").Controls.Add(ButtonExcStep2);
}void ButtonExcStep2_Click(object sender, EventArgs e)
{
//该按钮的一些事件,跟踪了,根本就不执行。
}
查过资料说如果在aspx.cs页面这么写,必须放在page_load里!ispostback外,这样的位置。
可问题是我动态创建的不在aspx.cs页面,那怎么做呢?请注意,在"非aspx.cs"动态创建按钮,创建它的事件。谢谢
//刚才代码贴错了,抱歉。
void CreateButton()
{
System.Web.UI.Page currentPage = HttpContext.Current.Handler as Page;
Button b = new Button();
b.Click += new EventHandler(b_Click);//与下句的顺序调换,仍旧不行。
currentPage.FindControl("form1").Controls.Add(b);
}
void b_Click(object sender, EventArgs e)
{
//该按钮的一些事件,跟踪了,根本就不执行。
}
{
Button btn;
btn = new Button();
btn.ID = "btn1";
btn.Text = "Add the infomation to the survey";
btn.Click += btn_Click;
btn.Style.Add("margin", "20px");
}
public void btn_Click(object sender, EventArgs e)
{
//do something
}
namespace TestSurvey
{
public class Class1 : Microsoft.SharePoint.WebPartPages.WebPart
{
private TextBox txt;
private Button btn;
private Label lbl; public Class1()
{ } public void btn_Click(object sender, EventArgs e)
{
SPSite site;
SPWeb web;
site =
new SPSite(string.Format("{0}/{1}", "http://wqc:9000", ""));
if (SPContext.Current.Web.CurrentUser != null)
{
if (site != null)
{
web = site.RootWeb;
if (web != null)
{
try
{
SPList list = web.Lists["职场调查"];
var flag = 0;
foreach (SPListItem temp in list.Items)
{
if (temp["创建者"].ToString().Contains(SPContext.Current.Web.CurrentUser.Name))
{
flag++;
}
}
if(flag > 0)
{
System.Web.HttpContext.Current.Response.Write("<script type='text/javascript'>alert('您已经参与过调查了!');</script>");
}
else if (!string.IsNullOrEmpty(txt.Text))
{
SPListItem item = list.Items.Add();
item["1.人为什么要活着"] = txt.Text;
item.Update();
System.Web.HttpContext.Current.Response.Write(
"<script type='text/javascript'>alert('谢谢,调查完毕!');</script>");
}
web.Close();
site.Close();
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write(
"<script type='text/javascript'>alert('对不起,调查异常!');</script>");
}
}
}
}
}
protected override void CreateChildControls()
{
base.CreateChildControls();
lbl = new Label();
lbl.ID = "lbl1";
lbl.Text = "1.人为什么要活着";
lbl.Style.Add("margin", "20px");
txt = new TextBox();
txt.ID = "txt1";
txt.Style.Add("margin", "20px");
btn = new Button();
btn.ID = "btn1";
btn.Text = "Add the infomation to the survey";
btn.Click += btn_Click;
btn.Style.Add("margin", "20px");
Controls.Add(lbl);
Controls.Add(txt);
Controls.Add(btn);
}
}
}
asp.net页面是个容器 void CreateButton(System.Web.UI.Page currentPage)
{
Button b = new Button();
b.Click += new EventHandler(b_Click);//与下句的顺序调换,仍旧不行。
currentPage.FindControl("form1").Controls.Add(ButtonExcStep2);
}
{
public void CreateButton(EventHandler handler)
{
Page currentPage = HttpContext.Current.Handler as Page;
Button b = new Button();
b.Click += handler;
b.Text = "点一下看看";
currentPage.FindControl("form1").Controls.Add(b);
}
} protected void Page_Load(object sender, EventArgs e)
{
Class1 cl = new Class1();
cl.CreateButton(new EventHandler(ButtonExcStep2_Click));
}
protected void ButtonExcStep2_Click(object sender, EventArgs e)
{
Response.Write("调用了");
}
不想在UI层的aspx和aspx.cs上添加任何动态按钮相关的代码和控件, 全部由WEB项目之外的这个类库来完成这个功能。
我的例子里Class1就是你的类库中的类,你妹看到我是按照你的例子来写的吗
{
Class1 cl = new Class1();
cl.CreateButton(new EventHandler(ButtonExcStep2_Click));
}
protected void ButtonExcStep2_Click(object sender, EventArgs e)
{
Response.Write("调用了");
}
可是这些代码,不是在aspx.cs页面里吗?
{
public void CreateButton()
{
Page currentPage = HttpContext.Current.Handler as Page;
Button b = new Button();
b.Click += new EventHandler(b_Click);
b.Text = "点一下看看";
currentPage.FindControl("form1").Controls.Add(b);
} void b_Click(object sender, EventArgs e)
{
HttpContext.Current.Response.Write("调用了");
}
}
public class Class1
{
public void CreateButton()
{
Page currentPage = HttpContext.Current.Handler as Page;
Button b = new Button();
b.Click += new EventHandler(b_Click);
b.Text = "点一下看看";
currentPage.FindControl("form1").Controls.Add(b);
} void b_Click(object sender, EventArgs e)
{
HttpContext.Current.Response.Write("调用了");
}
}
{
Class1 cl = new Class1();
cl.CreateButton();
}
我在一楼的帖子里已经说了,用UI层的aspx.cs里的某个按钮事件来触发“void CreateButton()”函数。调用类库,用aspx上的某个按钮的后台事件(aspx.cs里的单击事件)来触发void CreateButton()嘛~
{
private bool Added
{
get { return ViewState["Added"] != null; }
set { ViewState["Added"] = value; }
}
public void CreateButton()
{
Added = true;
Page currentPage = HttpContext.Current.Handler as Page;
Button b = new Button();
b.Click += new EventHandler(b_Click);
b.Text = "点一下看看";
currentPage.FindControl("form1").Controls.Add(b);
} void b_Click(object sender, EventArgs e)
{
HttpContext.Current.Response.Write("调用了");
}
protected override void OnLoad(EventArgs e)
{
if (Added)
{
CreateButton();
}
}
} public partial class _Default : Class1
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
CreateButton();
}
}
{
private bool Added
{
get { return ViewState["Added"] != null && Convert.ToBoolean(ViewState["Added"]); }
set { ViewState["Added"] = value; }
} public void CreateButton()
{
if (!Added)
{
Added = true;
Page currentPage = HttpContext.Current.Handler as Page;
Button b = new Button();
b.Click += new EventHandler(b_Click);
b.Text = "点一下看看";
currentPage.FindControl("form1").Controls.Add(b);
}
} void b_Click(object sender, EventArgs e)
{
HttpContext.Current.Response.Write("调用了");
}
protected override void OnLoad(EventArgs e)
{
if (Added)
{
Added = false;
CreateButton();
}
}
}
我以前做動態生成控件時在page_init裡實現。