Creating button event from panel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveRook
    New Member
    • Jul 2007
    • 147

    Creating button event from panel

    Hello

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

    My code behind:

    Code:
    CheckBoxList cbTransport = new CheckBoxList();
            cbTransport.Items.Add("Car");
            cbTransport.Items.Add("Bike");
            cbTransport.Items.Add("Truck");
    
            panelPitch.Controls.Add(cbTransport);
    
            Button btnTransport = new Button();        
            btnTransport Text = "Search";
    
            string mpString = "page.aspx?";
    
            foreach(ListItem li in cbTransport.Items)
            {
                if (pitchItem.Selected)
                {
                    mpString += "p=" + li + "&";
                }
            }
    
            btnTransport.PostBackUrl = mpString;
    
            panelPitch.Controls.Add(cbTransport);
            panelPitch.Controls.Add(btnTransport);

    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
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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

    Comment

    • DaveRook
      New Member
      • Jul 2007
      • 147

      #3
      Originally posted 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
      Final code

      Code:
       
      protected void Page_Load(object sender, EventArgs e)
          {
      CheckBoxList cbMultiplePitch = new CheckBoxList();        
      cbMultiplePitch.ID = "cbMP";        
      cbMultiplePitch.Items.Add("0.5mm");        
      cbMultiplePitch.Items.Add("0.8mm");       
      cbMultiplePitch.Items.Add("0.8mm x 1.2mm");        
      cbMultiplePitch.Items.Add("1mm");        
      cbMultiplePitch.Items.Add("1.27mm");        
      cbMultiplePitch.Items.Add("2mm");        
      cbMultiplePitch.Items.Add("2.54mm");        panelPitch.Controls.Add(cbMultiplePitch);                       
       Button btnMultiplePitch = new Button();        
      btnMultiplePitch.Text = "Search";        
      btnMultiplePitch.Click +=new EventHandler(btnMultiplePitch_Click);
      panelPitch.Controls.Add(btnMultiplePitch);          
       }    
      
      protected void btnMultiplePitch_Click(object sender, EventArgs e)  
        {        
      string mpString = "page.aspx?";        
      CheckBoxList cb2 = CheckBoxList)panelPitch.FindControl("cbMP");               
      
      foreach (ListItem pitchItem in cb2.Items) 
             { 
            if (pitchItem.Selected)
                    {
                      mpString += "p=" + pitchItem + "&";        
                    }
              }  
            Response.Redirect(mpString);     }

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        :) 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 :)

        Comment

        Working...