473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Creating button event from panel

147 100+
Hello

I have a panel in my asp.net page (c#).

My code behind:

Expand|Select|Wrap|Line Numbers
  1. CheckBoxList cbTransport = new CheckBoxList();
  2.         cbTransport.Items.Add("Car");
  3.         cbTransport.Items.Add("Bike");
  4.         cbTransport.Items.Add("Truck");
  5.  
  6.         panelPitch.Controls.Add(cbTransport);
  7.  
  8.         Button btnTransport = new Button();        
  9.         btnTransport Text = "Search";
  10.  
  11.         string mpString = "page.aspx?";
  12.  
  13.         foreach(ListItem li in cbTransport.Items)
  14.         {
  15.             if (pitchItem.Selected)
  16.             {
  17.                 mpString += "p=" + li + "&";
  18.             }
  19.         }
  20.  
  21.         btnTransport.PostBackUrl = mpString;
  22.  
  23.         panelPitch.Controls.Add(cbTransport);
  24.         panelPitch.Controls.Add(btnTransport);
  25.  

The event fires when the btnTransport button is clicked but it never recognises if a check box is or isn't selected and therefore never builds the mpString (line 17).

Is there a built in function/property for this or is it best to use the findcontrol method?

Any ideas?

Thanks

Dave
May 7 '10 #1

✓ answered by Frinavale

Dave the problem is with the Scope of your controls.
You need to define them with a Page Level scope.

Recall: if you define and use an object in a method it only exists in that method...it does not exist outside of the method.

So in your case you're defining controls in a method....and they are rendered and the user is able to use the controls but when the control posts back to the server it doesn't exist at the time when events for controls are created.

So, the controls will post back to the server but the server is unable to create the post back event and therefore doesn't execute any event handling code for the controls that were defined-instantiated-destroyed in a method.

Please read over How to use Dynamic Controls in Asp.NET. It's a very quick article about how to use dynamic controls in ASP.NET and it covers the issues with them in more depth than I covered here.

-Frinny

3 1297
Frinavale
9,735 Expert Mod 8TB
Dave the problem is with the Scope of your controls.
You need to define them with a Page Level scope.

Recall: if you define and use an object in a method it only exists in that method...it does not exist outside of the method.

So in your case you're defining controls in a method....and they are rendered and the user is able to use the controls but when the control posts back to the server it doesn't exist at the time when events for controls are created.

So, the controls will post back to the server but the server is unable to create the post back event and therefore doesn't execute any event handling code for the controls that were defined-instantiated-destroyed in a method.

Please read over How to use Dynamic Controls in Asp.NET. It's a very quick article about how to use dynamic controls in ASP.NET and it covers the issues with them in more depth than I covered here.

-Frinny
May 7 '10 #2
DaveRook
147 100+
@Frinavale
Final code

Expand|Select|Wrap|Line Numbers
  1.  
  2. protected void Page_Load(object sender, EventArgs e)
  3.     {
  4. CheckBoxList cbMultiplePitch = new CheckBoxList();        
  5. cbMultiplePitch.ID = "cbMP";        
  6. cbMultiplePitch.Items.Add("0.5mm");        
  7. cbMultiplePitch.Items.Add("0.8mm");       
  8. cbMultiplePitch.Items.Add("0.8mm x 1.2mm");        
  9. cbMultiplePitch.Items.Add("1mm");        
  10. cbMultiplePitch.Items.Add("1.27mm");        
  11. cbMultiplePitch.Items.Add("2mm");        
  12. cbMultiplePitch.Items.Add("2.54mm");        panelPitch.Controls.Add(cbMultiplePitch);                       
  13.  Button btnMultiplePitch = new Button();        
  14. btnMultiplePitch.Text = "Search";        
  15. btnMultiplePitch.Click +=new EventHandler(btnMultiplePitch_Click);
  16. panelPitch.Controls.Add(btnMultiplePitch);          
  17.  }    
  18.  
  19. protected void btnMultiplePitch_Click(object sender, EventArgs e)  
  20.   {        
  21. string mpString = "page.aspx?";        
  22. CheckBoxList cb2 = CheckBoxList)panelPitch.FindControl("cbMP");               
  23.  
  24. foreach (ListItem pitchItem in cb2.Items) 
  25.        { 
  26.       if (pitchItem.Selected)
  27.               {
  28.                 mpString += "p=" + pitchItem + "&";        
  29.               }
  30.         }  
  31.       Response.Redirect(mpString);     }
May 10 '10 #3
Frinavale
9,735 Expert Mod 8TB
:) Looks good but I would still recommend that you place that code in your Page Init Event instead of the Page Load event to avoid strange behaviours in the future :)
May 10 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Carlo Marchesoni | last post by:
From an aspx page (A.aspx) I open another one (B.aspx - for table lookup). When the user selects an entry in B.aspx I would like to force a button's event in A.aspx to be fired. I guess the only...
2
by: Sam Miller | last post by:
Hi, I have a button event that won't fire. I left it on Friday and it worked fine. I came back in on Monday and it won't fire. I tried putting another button and just putting a...
4
by: Bishop | last post by:
I can't get my button event to fire inside a table. the button outside of the table works. Both make a postback. Any help appreciated. My code below. (Add button to page)
5
by: csgraham74 | last post by:
Hi guys, I posted regarding this issue the other day but i still dont have an answer to my problem. Basically i have imported asp.net pages from dreamweaver into visual studio. Ive added a...
1
by: nesr235 | last post by:
I am having problems with my Button event not firing every time. I have a web page where the customer can enter search criteria and click a (search) button. This works fine the first time. But...
1
by: jw56578 | last post by:
I make a test page: test.aspx i put one button on the page i compile i access the web server by Http://servername click button, event fires and "testing button not working" appears on the page ...
4
by: sowencheung | last post by:
Hi, all The scenario is like this: I have a master page, contains two user controls, one is a search control, another is a login control. The server-side <form> is in the master page,...
2
by: bonnie.tangyn | last post by:
Hello all How can I add an button event in CreateElement object without triggering the event? I use HTML DOM CreateElement for adding a table row with textboxes and a button. No matter what...
1
by: =?Utf-8?B?U3Bhcmt5IDcxNzc=?= | last post by:
Hi, I am facing one deployment issue, I have 3 aspx pages one for Login, other for search request form and the third for displaying the results. After it was working fine locally I deployed it on...
4
by: ShutterMan | last post by:
I have a function that creates input buttons dynamically... function $NEW_BUTTON(id, content, parentId, cssclass, onclick) { var ctrl = document.createElement("input"); ctrl.id = id;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.