If the user want to put 'comment' for any particular 'topic', then user will press the 'Post Comment' Button near 'that' topic.
In the code behind, i will make a textbox visible and receive the comments.
Same thing i want to implement for a quiz-like-application.
I will display question, and options for that question, users will select anyone option with the help of 'Radio Button'.
i want all these to happen dynamically, because there can any number of 'topics-comments' in the first case, and any number of questions in the second case......
Use e.g an Repeater with Buttons inside.
If you wire up an handler, you could receive a CommandArgument, so that you know where the button was located, or what id is related to the button.
De Repeater do your automatically wire up so many Eventhandlers as Comments are there.
I havn't used the REPEATER yet, so it seems a tough job for me right now.
i am now able to differentiate between the buttons through same function as follows:
Page_Load()
{
Button b = new Button();
b.ID = topic.Topic_Id + "_1"; // topic_Id is my unique ID for each topic on the blog
b.Text = "Edit";
b.ToolTip = "Edit";
b.CommandArgument = b.ID; //passing this to event handler
b.Command += new CommandEventHandler(b_Command); //handler
}
void b_Command(object sender, CommandEventArgs e)
{
System.Windows.Forms.MessageBox.Show(e.CommandArgument.ToString());
}
Nice that you got it :-)
But dont use "System.Windows.Forms.MessageBox.Show(...." in a server application.
Thats evil. On a productive envoironment, that messages wouldnt show.
Use System.Diagnostics.Debug.WriteLine(..... or something like that instead.
apurvkolte
0 Points
10 Posts
How to add event handlers to dynamic controls
Jul 08, 2008 06:44 PM|LINK
Peter Bucher
Participant
1562 Points
214 Posts
MVP
Re: How to add event handlers to dynamic controls
Jul 08, 2008 08:03 PM|LINK
Hi Apurv
Don`t got your question.....
What exactly do you mean?
If i understood, then wire up an eventhandler by every dynamic control.
Microsoft MVP - Visual Developer ASP / ASP.NET
- http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
apurvkolte
0 Points
10 Posts
Re: How to add event handlers to dynamic controls
Jul 09, 2008 07:36 AM|LINK
i have a 'for' loop, in that loop i am adding 'Button' controls.
The ID's are given uniquely by the loop variable.
But then, how can i add unique handler to each unique button.
if code like this,
Button b=new Button();
b.ID="i.ToString()";
b.Click+=new EventHandler(b_Click);
then, all the buttons have same event handler ![:S]
Peter Bucher
Participant
1562 Points
214 Posts
MVP
Re: How to add event handlers to dynamic controls
Jul 09, 2008 11:13 AM|LINK
Hi Apurv
You can`t like this.
Whats your goal finaly?
Microsoft MVP - Visual Developer ASP / ASP.NET
- http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
apurvkolte
0 Points
10 Posts
Re: How to add event handlers to dynamic controls
Jul 09, 2008 04:25 PM|LINK
I am creating something like a 'blog'.
If the user want to put 'comment' for any particular 'topic', then user will press the 'Post Comment' Button near 'that' topic.
In the code behind, i will make a textbox visible and receive the comments.
Same thing i want to implement for a quiz-like-application.
I will display question, and options for that question, users will select anyone option with the help of 'Radio Button'.
i want all these to happen dynamically, because there can any number of 'topics-comments' in the first case, and any number of questions in the second case......
Peter Bucher
Participant
1562 Points
214 Posts
MVP
Re: How to add event handlers to dynamic controls
Jul 09, 2008 04:51 PM|LINK
Hi Apurv
Use e.g an Repeater with Buttons inside.
If you wire up an handler, you could receive a CommandArgument, so that you know where the button was located, or what id is related to the button.
De Repeater do your automatically wire up so many Eventhandlers as Comments are there.
Microsoft MVP - Visual Developer ASP / ASP.NET
- http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
apurvkolte
0 Points
10 Posts
Re: How to add event handlers to dynamic controls
Jul 09, 2008 05:22 PM|LINK
Thanx Peter Bucher.
I will try using Repeaters .
apurvkolte
0 Points
10 Posts
Re: How to add event handlers to dynamic controls
Jul 09, 2008 06:19 PM|LINK
I got it ! [:)]
I havn't used the REPEATER yet, so it seems a tough job for me right now.
i am now able to differentiate between the buttons through same function as follows:
Page_Load() { Button b = new Button(); b.ID = topic.Topic_Id + "_1"; // topic_Id is my unique ID for each topic on the blog b.Text = "Edit"; b.ToolTip = "Edit"; b.CommandArgument = b.ID; //passing this to event handler b.Command += new CommandEventHandler(b_Command); //handler } void b_Command(object sender, CommandEventArgs e) { System.Windows.Forms.MessageBox.Show(e.CommandArgument.ToString()); }Peter Bucher
Participant
1562 Points
214 Posts
MVP
Re: How to add event handlers to dynamic controls
Jul 09, 2008 10:11 PM|LINK
Hi Apurv
Nice that you got it :-)
But dont use "System.Windows.Forms.MessageBox.Show(...." in a server application.
Thats evil. On a productive envoironment, that messages wouldnt show.
Use System.Diagnostics.Debug.WriteLine(..... or something like that instead.
Best Regards
Microsoft MVP - Visual Developer ASP / ASP.NET
- http://www.aspnetzone.de/blogs/peterbucher/ - Auf den Spuren von .NET
apurvkolte
0 Points
10 Posts
Re: How to add event handlers to dynamic controls
Jul 10, 2008 06:58 AM|LINK
Thanks ... !