ASP.NET C# 客户端点击按钮后在服务端自动生成LinkButton,无法链接Click事件?
代码如下:--------------------------------------------------button1按钮的click事件:protectedvoi...
代码如下:
--------------------------------------------------
button1按钮的click事件:
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
LinkButton lb = new LinkButton();
lb.ID = "lb_" + i.ToString();
lb.Text = i.ToString();
lb.Click += new EventHandler(lb_Click);
this.form1.Controls.Add(lb);
}
}
--------------------------------------------------
自动生成的linkbutton的click 事件:
void lb_Click(object sender, EventArgs e)
{
LinkButton lb=(LinkButton)sender;
TextBox1.Text = "按钮" + lb.Text;
}
--------------------------------------------------
我发现把button事件里的函数放到page_load里,就可以实现,但系统需求一定要客户点了按钮之后再自动生成,我应该怎么改代码?
小弟新手`` 请各位大大赐教`` 展开
--------------------------------------------------
button1按钮的click事件:
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 5; i++)
{
LinkButton lb = new LinkButton();
lb.ID = "lb_" + i.ToString();
lb.Text = i.ToString();
lb.Click += new EventHandler(lb_Click);
this.form1.Controls.Add(lb);
}
}
--------------------------------------------------
自动生成的linkbutton的click 事件:
void lb_Click(object sender, EventArgs e)
{
LinkButton lb=(LinkButton)sender;
TextBox1.Text = "按钮" + lb.Text;
}
--------------------------------------------------
我发现把button事件里的函数放到page_load里,就可以实现,但系统需求一定要客户点了按钮之后再自动生成,我应该怎么改代码?
小弟新手`` 请各位大大赐教`` 展开
2个回答
2011-04-24
展开全部
看看我的例子
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"server=BRUCE-PC\SQLEXPRESS;database=BizPort;user id=sa;password=sa");
con.Open();
//SqlCommand cmd = new SqlCommand("select * from BankInfo", con);
SqlDataAdapter sda = new SqlDataAdapter("select * from BankInfo", con);
DataSet tb = new DataSet();
sda.Fill(tb);
con.Close();
Response.Write(tb.Tables[0].Rows.Count.ToString());
GridView1.DataSource = tb;
GridView1.DataBind();
int i = 1;
foreach (DataRow row in tb.Tables[0].Rows)
{
LinkButton buton = new LinkButton();
buton.ID = "link" + i.ToString();
buton.Text = row["BankName"].ToString();
buton.PostBackUrl = "http://www.baidu.com";
this.Page.Form.Controls.Add(buton);
this.Page.Form.Controls.Add(new LiteralControl("\r\nfdas</br>"));
i++;
}
//}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"server=BRUCE-PC\SQLEXPRESS;database=BizPort;user id=sa;password=sa");
con.Open();
//SqlCommand cmd = new SqlCommand("select * from BankInfo", con);
SqlDataAdapter sda = new SqlDataAdapter("select * from BankInfo", con);
DataSet tb = new DataSet();
sda.Fill(tb);
con.Close();
Response.Write(tb.Tables[0].Rows.Count.ToString());
GridView1.DataSource = tb;
GridView1.DataBind();
int i = 1;
foreach (DataRow row in tb.Tables[0].Rows)
{
LinkButton buton = new LinkButton();
buton.ID = "link" + i.ToString();
buton.Text = row["BankName"].ToString();
buton.PostBackUrl = "http://www.baidu.com";
this.Page.Form.Controls.Add(buton);
this.Page.Form.Controls.Add(new LiteralControl("\r\nfdas</br>"));
i++;
}
//}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询