473,657 Members | 2,805 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 1942
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
2226
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 the name of a common command (btn_Command) assigned to it's Command property. The creation of the table is done by a function called drawGeo. drawGeo is called during the initial Page_Load (!IsPostBack), but should be called by btn_Command on...
1
3119
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 elements and would like to seek a solution for this. I had looked through several articles for accessing programatically-created dynamic elements such as: 1)
3
3476
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 directly to the second application in a browser, all of my events fire correctly. However, if I have the first application do a Response.Redirect to the second, for some reason none of my events on the second application ever get fired. After I...
1
1478
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 to set property values of a dynamically added web user control at run time? Can you provide a link or code snippet? I can do it for a typical web control (textbox, label, etc), but I can't figure out how to do it for a web user control(ascx...
7
3524
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 their "Text" properties via public properties. However, after I have instantiated the ctrlBlogEntry control in the PageLoad method of default.aspx, it is failing when I try to set the text of the label controls in the ctrlBlogEntry control. Here...
7
3802
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 database search that I am unable to figure out how to access. (The search is implemented in Php/MySQL.) The user enters search values for: name, address1, city, .... etc., ..... and for each of these they also select whether the search should...
2
3379
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 place it in the last cell of each row that I create dynamically using DOM methods. Everything is working well (that is, just like the original example) except for something related to the function behind my link. The link simply calls a function...
4
1489
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 dropdownlistbox and label dynamically through loop and assign id as dropdownlist + no of iteration. Now my question is how I access these control as when I am accessing one of the control I am getting nullobjectreference error stating “Object reference not set...
4
3483
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 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
0
8403
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
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 we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.