dinamically linkbuttons handler

O

oterox

I've created a method for creating linkbuttons dinamically.It works fine if
the method is in the webform but if i try to put the method in a different
class it doesn't work:

--- webform1 ----- This works:

TextBox txt1 = new TextBox();
this.Controls.Add(txt1);

this.Controls.Add(createLnkButton("lbt2","lnkBtn2","cmdName2"));


public LinkButton createLnkButton(string id,string text, string cmdName)
{
LinkButton lbt = new LinkButton();
lbt.ID = id;
lbt.Text = text;
lbt.CommandName = cmdName;
lbt.Command += new CommandEventHandler(Handle_Linkbuttons);

return lbt;
}
private void Handle_Linkbuttons(object sender, CommandEventArgs e)
{

LinkButton lb = (LinkButton)sender;
txt1.Text = lb.Text + "::" +e.CommandName.ToString();

}

--------- this doesn't work: -------------------------------------

clsControls.cs_______________

public LinkButton createLnkButton(string id,string text, string cmdName)
{
LinkButton lbt = new LinkButton();
lbt.ID = id;
lbt.Text = text;
lbt.CommandName = cmdName;
lbt.Command += new CommandEventHandler(Handle_Linkbuttons);
return lbt;
}
private void Handle_Linkbuttons(object sender, CommandEventArgs e)
{
controlGrid cg = new controlGrid();
string ee = e.CommandName.ToString();
LinkButton lb = (LinkButton)sender;

txt1 = new TextBox();
txt1.Text = lb.Text; //i must pass the textbox control but i don't know
how
}

webform1_________________

clsControls c = new clsControls();
TextBox txt1 = new TextBox();
this.Controls.Add(txt1);

this.Controls.Add(c.createLnkButton("lbt2","lnkBtn2","cmdName2"));
 
V

Victor Garcia Aprea [MVP]

Hi oterox,

Not sure what you mean by "its doesn't work", any errors? just events not
firing?

You need to keep in mind that when dinamically creating controls you need to
do so in Load or earlier for them to work properly (event firing, etc). By
taking a really quick look at your code you seem to be creating some
controls in an event handler which is just too late, controls created there
won't be able to fire.

Please revise your code keeping that in mind and let us know if you still
have problems,
 
O

oterox

Thank you Victor.
The problem is in the method Handle_Linkbuttons.I don't know how to access
from clsControls.Handle_Linkbuttons to txt1 in webform1.In my code i want to
write some text into TXT1(webform1.aspx) from
Handle_Linkbuttons(clsControls.cs) .
 
V

Victor Garcia Aprea [MVP]

You can use the FindControl method to get a reference to a control, so you
can try something like this:
[C#]
TextBox tb = FindControl ("YourTextBoxID") as TextBox;

Take a look at how FindControl and naming container works for properly
finding controls.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top