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
- 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 System.IO;
- using AjaxControlToolkit;
- public partial class Wdg : System.Web.UI.Page
- {
- protected TabContainer tbCon;
- protected TabPanel[] tbPanel;
- protected UpdatePanel[] updPanel;
- protected Label[] lblMessage;
- protected Button[] btnClick;
- protected int count;
- private int i;
- protected void Page_Load(object sender, EventArgs e)
- {
- count = 5;
- tbCon = new TabContainer();
- tbCon.Width = 600;
- tbPanel = new TabPanel[count];
- updPanel = new UpdatePanel[count];
- lblMessage = new Label[count];
- btnClick = new Button[count];
- for (i = 0; i < count; i++)
- {
- tbPanel[i] = new TabPanel();
- tbPanel[i].HeaderText = "Tab " + i.ToString();
- updPanel[i] = new UpdatePanel();
- lblMessage[i] = new Label();
- lblMessage[i].Text = "Label " + i.ToString();
- lblMessage[i].ID = "lblMessage" + i.ToString();
- btnClick[i] = new Button();
- btnClick[i].Text = "Button " + i.ToString();
- btnClick[i].ID = "btnClick" + i.ToString();
- btnClick[i].Click += new EventHandler(Wdg_Click);
- updPanel[i].ContentTemplateContainer.Controls.Add(lblMessage[i]);
- updPanel[i].ContentTemplateContainer.Controls.Add(btnClick[i]);
- tbPanel[i].Controls.Add(updPanel[i]);
- tbCon.Controls.Add(tbPanel[i]);
- }
- form1.Controls.Add(tbCon);
- }
- void Wdg_Click(object sender, EventArgs e)
- {
- Button btn = (Button)sender;
- StreamWriter s = new StreamWriter(new FileStream("EventLogger.txt", FileMode.Append));
- s.WriteLine("ID is " + btn.ID.ToString());
- s.WriteLine(btn.ID.ToString()[btn.ID.ToString().Length - 1]);
- /* How do i modify the contents of the label in this update panel? */
- s.Close();
- }
- }
Karthik