[C#-WEB] Trouble finding controls in Update panel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthik25
    New Member
    • Jul 2008
    • 6

    [C#-WEB] Trouble finding controls in Update panel

    Hi All,

    I have a problem in finding control in a dynamically created updated panel. I have given the code below. Following is just a starting effort in a completely dynamic user control. I am experimenting before getting to the actual part. This is what I am trying to do:

    * Create a tab container dynamically

    * Create 5 tabs dynamically

    * Add an update panel to each of the tabs

    * Add a label to each of the update panels

    * Add an OnLoad event to each of the tabs and change the contents of the label added

    But I am not able to accomplish the last part, I am not sure how to access the label dynamically added to the update panel

    I have given the code below. Please help me in this regard.


    Code:
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using AjaxControlToolkit;
      
    public partial class EvaluationsWidget : System.Web.UI.Page
    {
       protected string[] tabnames = {"All","Contact Centers","Networks","Technology Planning"};
    
        protected Label lbl;
       
        protected void Page_Load(object sender, EventArgs e)
         {
              ArrayList al = new ArrayList();
      
              for (int i = 0; i < 20; i++)
              al.Add(i.ToString());
      
              TabContainer tabContainer = new TabContainer();
              tabContainer.ID = "tbContainerMain";
              tabContainer.Width = 500;
       
              for (int i = 0; i < tabnames.Length; i++)
              {
                  TabPanel tabPanel = new TabPanel();
                  tabPanel.ID = "evaluationsPanel" + i.ToString();
                  tabPanel.HeaderText = tabnames[i];
                  tabPanel.Load += new EventHandler(tabPanel_Load);
      
                  UpdatePanel updPanel = new UpdatePanel();
                  updPanel.ID = "evaluationsUpdatePanel" + i.ToString();
                  updPanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
      
                  lbl = new Label();
                  lbl.ID = "lbl" + i.ToString();
                  lbl.Text = "Label " + i.ToString();
                  updPanel.ContentTemplateContainer.Controls.Add(lbl);
      
                  tabPanel.Controls.Add(updPanel);
                  tabContainer.Controls.Add(tabPanel);
              }
    
              form1.Controls.Add(tabContainer);
          }
      
          void tabPanel_Load(object sender, EventArgs e)
          {
              TabPanel tbPanel = (TabPanel)sender;
      
              //This doen't work, I need help here
              this.lbl.Text = "Clicked";
           }
        }


    Thanks,

    Karthik
    Last edited by karthik25; Jul 31 '08, 05:57 PM. Reason: More descriptive title change
  • TTCEric
    New Member
    • Jul 2008
    • 26

    #2
    You need a 'Handles TabControl.OnLo ad' appended to the end of the function signature like this

    private sub MyTabControl(By Val sender As System.Object, ByVal e As System.EventArg s) Handles MyTabControl.On Load

    Comment

    • karthik25
      New Member
      • Jul 2008
      • 6

      #3
      Hi Eric,

      Thank you for your reply. What you've said is for VB. Net. I looked and found out that "handles" keyword and its functionality isn't available in C#. And I also found out that "AutoEventWireu p" should be set to true in C# as "handles" cannot be used in C#.

      I have set auto event wireup to true in my page, Still it doesn't work. Please note that the label is created dynamically and added to the update panel. How am I supposed to access that label in the update panel?

      Thanks again for replying

      Comment

      • TTCEric
        New Member
        • Jul 2008
        • 26

        #4
        Oops. Didnt notice it was C#.

        Ok, I found a link related to what you need. A good google keyword search is C# Event Handling. There are actually several ways to handle events in C#. This one I found most easier to follow:

        Comment

        • karthik25
          New Member
          • Jul 2008
          • 6

          #5
          Thanks a lot for your replies Eric. I was able to solve the problem! Thanks again!

          Comment

          Working...