Create and add event for linkbutton
-
Hi all, I have a form with a button and a textbox. When i click button, linkbutton will be create on form. When i click linkbutton, "Hello" will be filled into textbox.
protected void Button1_Click(object sender, EventArgs e) { LinkButton lbtn = new LinkButton(); lbtn.ID = "MyLinkButton"; lbtn.Text = "Welcome"; lbtn.Command += new CommandEventHandler(lbtn_Command); form.Controls.Add(lbtn); } protected void lbtn_Command(object sender, CommandEventArgs e) { txt.Text = "Hello"; }
But when i click LinkButton, Command event is not raised. What can i do to raise Command event of LinkButton?
-
Hi all, I have a form with a button and a textbox. When i click button, linkbutton will be create on form. When i click linkbutton, "Hello" will be filled into textbox.
protected void Button1_Click(object sender, EventArgs e) { LinkButton lbtn = new LinkButton(); lbtn.ID = "MyLinkButton"; lbtn.Text = "Welcome"; lbtn.Command += new CommandEventHandler(lbtn_Command); form.Controls.Add(lbtn); } protected void lbtn_Command(object sender, CommandEventArgs e) { txt.Text = "Hello"; }
But when i click LinkButton, Command event is not raised. What can i do to raise Command event of LinkButton?
Problem is when u click on Button then LinkButton will be created and added in the form.But when u click on the Linkbutton at that time u can not create linkbutton againg.You have to create every postback. try to create it on Page_Load event try this code.
protected void Page_Load(object sender, EventArgs e) { CreateBtn(); } protected void Button1_Click(object sender, EventArgs e) { CreateBtn(); } public void CreateBtn() { LinkButton lnk= new LinkButton(); lnk.ID = "MyLinkButton"; lnk.Text = "Welcome"; lnk.Click += new EventHandler(lnk_Click); form.Controls.Add(lnk); } public void lnk_Click(object sender, EventArgs e) { txt.Text = "Hello"; }
Best Regard Pathan---------------------------------------------------