Welcome, Guest. Please login or register.
Did you miss your activation email?
Your Ad Here
Pages: [1]   Go Down
  Print  
Author Topic: How to trigger nested (child) controlevent in asp.net?  (Read 747 times)
0 Members and 1 Guest are viewing this topic.
HappyFace
Javaforums.net admin
Senior Member
*

Reputation: 16
Developer @ Engineeringserver network
Offline Offline
Posts: 2549
Referrals: 12
Activity
12%

WWW Awards
« on: July 21, 2009, 06:38:29 AM »

I have this issue for the last few hours and i can't seem to get it right to get the child event to fire when adding a control in another control.

The hierarchy looks like this:

Placeholder control
   Table control
      row
      cell
      linkbutton control , i want this event to fire when i press the link.

When testing the code below with only one placeholder and one linkbutton the event does fire so i think it has to with something with creating the linkbutton dynamically in the table that is also in turn dynamically created.

So does anyone know how to trigger the child control  events?


Doesn't work:
Code
GeSHi (csharp):
  1.  
  2.               LinkButton doSomething = new LinkButton();
  3.                        doSomething .Text = "doSomething ";
  4.                        doSomething .ID = "someID";
  5.                        doSomething .Click += new EventHandler(someEvent);
  6.  
  7.                        cell[j].Controls.Add(doSomething ); // does not work
  8.                        //PlaceHolder1.Controls.Add(doSomething ); if i add the linkbutton to the placeholder it DOES work.
  9.  
Created by GeSHI 1.0.7.20

Works:
Code
GeSHi (csharp):
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. namespace EventPostBackHandlerTest
  13. {
  14.    public partial class _Default : System.Web.UI.Page
  15.    {
  16.        protected void Page_Load(object sender, EventArgs e)
  17.        {
  18.            LinkButton l1 = new LinkButton();
  19.            l1.Text = "click me";
  20.            l1.Click +=new EventHandler(clickMe);
  21.            PlaceHolder1.Controls.Add(l1);
  22.  
  23.        }
  24.        public void clickMe(object sender, EventArgs e)
  25.        {
  26.            System.Diagnostics.Debug.WriteLine("Test");
  27.        }
  28.  
  29.    }
  30.  
  31.  
  32. }
  33.  
Created by GeSHI 1.0.7.20
« Last Edit: July 21, 2009, 06:50:42 AM by HappyFace » Logged

It doesn't matter how hard you've studied; the material won't be on the exam anyway.
Admin @ www.Engineeringserver.com General Admin @ www.Javaforums.net and it's subsites Forum admin @ www.Javaforums.net Global moderator @ www.Engineeringserver.com network Java / .NET blogger @ www.Engineeringserver.com/blog
XMA performer since 2006 @ http://www.engineeringserver.com/XMA-NaN/ Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://www.javaforums.net...lery.html;sa=media;id=170
http://img222.imageshack....707/arkietomatoesmall.jpg
http://img208.imageshack.us/img208/7141/smalli.png
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
dotNETprogrammers.net/forum :: A community for .NET developers
« on: July 21, 2009, 06:38:29 AM »

Your Ad Here
 Logged
HappyFace
Javaforums.net admin
Senior Member
*

Reputation: 16
Developer @ Engineeringserver network
Offline Offline
Posts: 2549
Referrals: 12
Activity
12%

WWW Awards
« Reply #1 on: July 21, 2009, 07:00:30 AM »

Suprisingly this works, the event triggers when i click on the linkbutton. Hmm.. time to act like sherlock homo's
next step is to create the tables dynamically .


Code
GeSHi (csharp):
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. namespace EventPostBackHandlerTest
  13. {
  14.    public partial class _Default : System.Web.UI.Page
  15.    {
  16.        protected void Page_Load(object sender, EventArgs e)
  17.        {
  18.            LinkButton l1 = new LinkButton();
  19.            l1.Text = "click me";
  20.            l1.Click += new EventHandler(clickMe);
  21.  
  22.            Table t = new Table();
  23.            TableRow tr = new TableRow();
  24.            TableCell tc = new TableCell();
  25.            tc.Controls.Add(l1);
  26.            tr.Controls.Add(tc);
  27.            t.Controls.Add(tr);      
  28.            PlaceHolder1.Controls.Add(t);
  29.  
  30.        }
  31.        public void clickMe(object sender, EventArgs e)
  32.        {
  33.            System.Diagnostics.Debug.WriteLine("Test");
  34.        }
  35.  
  36.    }
  37.  
  38.  
  39. }
  40.  
Created by GeSHI 1.0.7.20


Code
GeSHi (html4strict):
  1.  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml" >
  5. Untitled Page
  6. </title></head>
  7.    <form name="form1" method="post" action="Default.aspx" id="form1">
  8. <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
  9. <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
  10. <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTUyNzUzNTM5OGRkPLC/xegbPlgCBZYPVn9plmoP9J8=" />
  11. </div>
  12.  
  13. <script type="text/javascript">
  14. <!--
  15. var theForm = document.forms['form1'];
  16. if (!theForm) {
  17.     theForm = document.form1;
  18. }
  19. function __doPostBack(eventTarget, eventArgument) {
  20.     if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
  21.         theForm.__EVENTTARGET.value = eventTarget;
  22.         theForm.__EVENTARGUMENT.value = eventArgument;
  23.         theForm.submit();
  24.     }
  25. }
  26. // -->
  27. </script>
  28.  
  29.  
  30.    <div>
  31.        <table border="0">
  32. <td><a href="javascript:__doPostBack('ctl03','')">click me</a></td>
  33. </tr>
  34. </table>
  35.  
  36.    </div>
  37.  
  38.  
  39. <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgKo8Yy+CAKgwImNC8fZ1m8QUUOoSK5tUkobOmAnTNTi" />
  40. </div></form>
  41. </body>
  42. </html>
  43.  
Created by GeSHI 1.0.7.20
« Last Edit: July 21, 2009, 07:05:11 AM by HappyFace » Logged

It doesn't matter how hard you've studied; the material won't be on the exam anyway.
Admin @ www.Engineeringserver.com General Admin @ www.Javaforums.net and it's subsites Forum admin @ www.Javaforums.net Global moderator @ www.Engineeringserver.com network Java / .NET blogger @ www.Engineeringserver.com/blog
XMA performer since 2006 @ http://www.engineeringserver.com/XMA-NaN/ Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://www.javaforums.net...lery.html;sa=media;id=170
http://img222.imageshack....707/arkietomatoesmall.jpg
http://img208.imageshack.us/img208/7141/smalli.png
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
dotNETprogrammers.net/forum :: A community for .NET developers
   


 Logged
Pages: [1]   Go Up
  Print  
 
Jump to: