473,508 Members | 2,329 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[C# WEB] Accessing a dynamically created label in a label array

6 New Member
Hi,

The following code creates a tab container, 5 tab panels, buttons, labels. I was able to add event handlers to the button. But how am i supposed to access the dynamically created labels? For example how do i get the text of the label changed when the corresponding button is clicked? I have given the complete code below. Please help me with this.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.HtmlControls;
  10. using System.Web.UI.WebControls;
  11. using System.Web.UI.WebControls.WebParts;
  12. using System.Xml.Linq;
  13. using System.IO;
  14. using AjaxControlToolkit;
  15.  
  16. public partial class Wdg : System.Web.UI.Page
  17. {
  18.     protected TabContainer tbCon;
  19.     protected TabPanel[] tbPanel;
  20.     protected UpdatePanel[] updPanel;
  21.     protected Label[] lblMessage;
  22.     protected Button[] btnClick;
  23.  
  24.     protected int count;
  25.     private int i;
  26.  
  27.     protected void Page_Load(object sender, EventArgs e)
  28.     {
  29.         count = 5;
  30.  
  31.         tbCon = new TabContainer();
  32.         tbCon.Width = 600;
  33.  
  34.         tbPanel = new TabPanel[count];
  35.         updPanel = new UpdatePanel[count];
  36.         lblMessage = new Label[count];
  37.         btnClick = new Button[count];
  38.  
  39.         for (i = 0; i < count; i++)
  40.         {
  41.             tbPanel[i] = new TabPanel();
  42.             tbPanel[i].HeaderText = "Tab " + i.ToString();
  43.  
  44.             updPanel[i] = new UpdatePanel();
  45.  
  46.             lblMessage[i] = new Label();
  47.             lblMessage[i].Text = "Label " + i.ToString();
  48.             lblMessage[i].ID = "lblMessage" + i.ToString();
  49.  
  50.             btnClick[i] = new Button();
  51.             btnClick[i].Text = "Button " + i.ToString();
  52.             btnClick[i].ID = "btnClick" + i.ToString();
  53.             btnClick[i].Click += new EventHandler(Wdg_Click);
  54.  
  55.             updPanel[i].ContentTemplateContainer.Controls.Add(lblMessage[i]);
  56.             updPanel[i].ContentTemplateContainer.Controls.Add(btnClick[i]);
  57.  
  58.             tbPanel[i].Controls.Add(updPanel[i]);
  59.  
  60.             tbCon.Controls.Add(tbPanel[i]);
  61.         }
  62.  
  63.         form1.Controls.Add(tbCon);
  64.     }
  65.  
  66.     void Wdg_Click(object sender, EventArgs e)
  67.     {
  68.         Button btn = (Button)sender;
  69.  
  70.         StreamWriter s = new StreamWriter(new FileStream("EventLogger.txt", FileMode.Append));
  71.         s.WriteLine("ID is " + btn.ID.ToString());
  72.         s.WriteLine(btn.ID.ToString()[btn.ID.ToString().Length - 1]);
  73.  
  74.  
  75.     /* How do i modify the contents of the label in this update panel? */
  76.  
  77.         s.Close();
  78.     }
  79. }
  80.  
Thanks
Karthik
Jul 31 '08 #1
1 1916
karthik25
6 New Member
I found out the solution, thank god!

Ans:

Expand|Select|Wrap|Line Numbers
  1. lblMessage[i - 48].Text = "Clicked"; 
I didn't try this at all!
Jul 31 '08 #2

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

Similar topics

1
2216
by: Klom Dark | last post by:
I've got a weird problem going on - I've got a table of dynamically created buttons. Each button has the X/Y value of the buttons position in the table assigned to it's CommandArgument property and...
1
3108
by: CS Wong | last post by:
Hi, I have a page form where form elements are created dynamically using Javascript instead of programatically at the code-behind level. I have problems accessing the dynamically-created...
3
3468
by: michael_vanommeren | last post by:
I have two web applications that I am working with and I am trying to do a Response.Redirect from one to the other. They are setup as separate web applications on the same IIS server. If I go...
1
1466
by: Brian | last post by:
Thanks for your time. I've created a web user control that has some properties available. I'm able to add the control dynamically (at run time) with no problem(.controls.add(). Is it possible...
7
3518
by: Jason | last post by:
I am trying to dynamically add a web user control - ctrlBlogEntry.ascx - to a page - default.aspx - (via an ASP:PlaceHolder). This web user control has two ASP:Label controls and I'm accessing...
7
3791
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a...
2
3364
by: jmarendo | last post by:
Hello, After reading through the "Table Basics - DOM - Refer to table cells" example at mredkj.com , I modified the code for my own purposes. In the modified version, I create a hyperlink and...
4
1476
by: imranabdulaziz | last post by:
Dear All, I am using asp.net2.0, C#, sql2005 using Visual studio 2005 Let Me explain the scenario I have checkboxlist containg 15 field. Based on no of checked field . I created...
4
3472
by: karthik25 | last post by:
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...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7324
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
7495
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
5627
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,...
1
5052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.