Click to See Complete Forum and Search --> : How to use delegates?


meenakshi_joshi09
February 25th, 2007, 06:12 AM
Hi,
I am working on an asp.net application. In my application I am dynamically genreating some control such as lable, textbox and button control. Now I want to write some code on click event of that dynamically generated button. Can anybody tell me how to use delegates for this purpose?

One more problem how to change font of dynamically generated textbox and label?




Thanks in advance......

TheCPUWizard
February 25th, 2007, 10:30 AM
Look at the code that is generated for a "static" control. You simply need to clone that methodology. If you still have problems, post a minimal yet complete example, and I (or someone else) will take a look.

meenakshi_joshi09
February 25th, 2007, 12:46 PM
Hi,

My problem is that how can I write code for that control which is not present at design time?
In case of "static control" such as button control we can write code in code behind, but how can I write code in code behind for a control that is generated dynamically and not exists at design time?

gksingh
February 26th, 2007, 05:12 AM
Hi,

My problem is that how can I write code for that control which is not present at design time?
In case of "static control" such as button control we can write code in code behind, but how can I write code in code behind for a control that is generated dynamically and not exists at design time?

you can just use, new keyword for create button dynamically and the bind the click event on that object.like
Button btn = new Button();
btn.Click +=new EventHandler(btn_Click);
Panel1.Controls.Add(btn);

//***************************
and then place the button click code as function.
public void btn_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn = (Button)sender;
btn.Text = "Gaurav";

}

i think, now u get your requirement.

Shuja Ali
February 26th, 2007, 05:28 AM
It is not all that difficult.

Take a look at Dynamic Controls in ASP.NET (http://aspnet.4guysfromrolla.com/articles/081402-1.aspx)

meenakshi_joshi09
March 1st, 2007, 12:46 PM
Thanks gksingh,
Thanks Shuja for your replies. These are realy very helpful. Now I have solved my problem.


with regards
meenakshi