473,768 Members | 2,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating Dynamic Controls on the fly

Hi,

I'm having a bit of trouble. I want to create a number of controls on the
fly. They are all the same control type. How is the best way to do this?

I have a name I can assign to them so I know how to reference them, but I
don't know how to create them.

Thanks
Hayden
Apr 2 '07 #1
3 1556
"Hayden Kirk kirk mobilepc.co.nz> " <hayden <dot@wrote in message
news:eF******** ******@TK2MSFTN GP04.phx.gbl...
I'm having a bit of trouble. I want to create a number of controls on the
fly. They are all the same control type. How is the best way to do this?

I have a name I can assign to them so I know how to reference them, but I
don't know how to create them.
This is a piece of code I use in conjunction with a shopping cart. It
dynamically creates one LinkButton control for every item in the cart:

private void Page_Init(objec t sender, System.EventArg s e)
{
LinkButton lnkRemove;
Dictionary<stri ng, bytedicCart = (Dictionary<str ing,
byte>)Session["Cart"];

foreach(KeyValu ePair<string, bytekvpItem in dicCart)
{
lnkRemove = new LinkButton();
lnkRemove.Enabl eViewState = true;
lnkRemove.ID = "lnkRemove_ " + kvpItem.Key;
lnkRemove.Text = "X";
lnkRemove.Comma nd += new CommandEventHan dler(lnkRemove_ Command);
lnkRemove.Comma ndName = kvpItem.Key;
lnkRemove.Visib le = false;
this.pnlCart.Co ntrols.Add(lnkR emove);
}
}

Notice that the code is in Page_Init - this is important, otherwise the
controls' events won't get wired up properly...

When the controls are originally created, they are added to an <asp:Panel>
for use later in the page creation cycle.
Apr 2 '07 #2
Sorry, to be a bit more specific,

Reading infro from the eventlog. I now want to create a listbox for each
eventlog I find on the computer. How do I create a bunch of listboxes like
that?

I was going to do:

mylistbox.Name = "listBox" + myEventLog.LogD isplayName;

to be able to reference it or am I way off?

"Hayden Kirk kirk mobilepc.co.nz> " <hayden <dot@wrote in message
news:eF******** ******@TK2MSFTN GP04.phx.gbl...
Hi,

I'm having a bit of trouble. I want to create a number of controls on the
fly. They are all the same control type. How is the best way to do this?

I have a name I can assign to them so I know how to reference them, but I
don't know how to create them.

Thanks
Hayden

Apr 2 '07 #3

Hi,

As long as you know the Log names of the EventLog that you need to read
on your computer, you cld try this:

EventLog evt = new EventLog();
evt.Log = "applicatio n";

ListBox lstEvent = new ListBox();
lstEvent.ID = evt.LogDisplayN ame;

foreach (EventLogEntry evtEntry in evt.Entries)
{
lstEvent.Items. Add(evtEntry.Me ssage);
Page.Form.Contr ols.Add(lstEven t);
}

This code would be applicable, if you plan on reading one particular log
type, say "applicatio n". But then if you plan on doing the same for every log
on the computer, you need to have the log names in some object, cld be string
with a delimiter or a collection object and loop through the object with this
code repeating for every iteration.
Apr 2 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6018
by: Glenn Owens | last post by:
I have an ASP.Net page on which there are serveral static controls (listboxes, radiobuttonlist and textboxes). These controls are used to create criteria from which the code-behind will dynamically create 1-n datagrids. When the Submit button is clicked I need to save (in viewstate) the contents of the criteria controls so that I can recreate the dynamic DataGrid(s) in the LoadViewState (overloaded method). I need to do this so that the...
1
1778
by: Hasani \(remove nospam\) | last post by:
The way the system works is, you create a user control (ascx) that will be a template and must implement the interface IPageTemplate. You then create one or more user controls (ascx) that implement the IPageContent interface. A page (aspx) must then be created that loads (using Page.LoadControl) the page template, and the page content. The page content adds itself to the page template. The page template is then added to the aspx's control...
2
1523
by: Matt | last post by:
Hi, Can someone provide some information on how to create a control dynamically based on the contents of another dynamic control. In my experience, all dynamic controls must be created in the Initialize stage of an ASP.Net page life cycle. Because viewstate is not available in the initialization stage, I cannot get the value of the 1st dynamic control to create the second control.
1
2704
by: amirmira | last post by:
I have a multithreaded application is ASP.NET that checks the status of a process every 2 seconds. After the thread has completed, the values of dynamically added controls need to be changed depending on the return value from the process just completed on the second thread. The problem I am having is that while the code seems to work correctly (I am performing a response.write and writting out the values as a HTML comment), the final...
2
4571
by: Patrick | last post by:
I want to define a set of web-form templates in XML and render the equivalent web-form with ASP.NET, then process any input server controls on the form. Reading the XML file from Page_load is easy, but 1) How do I set about dynamically creating user controls (like TextBox, TextArea) --- simply Declare and initialised (new) the user controls?? How do I "place" it graphically on the form. Ideally, I want them to lay out in a table, one...
7
2227
by: Varangian | last post by:
Hello is creating controls at runtime the same as designtime ? will a page be faster if controls are created at designtime rather than at runtime ? If so why ? thanks :)
5
2029
by: SalamElias | last post by:
I am creating several chkBoxes dynamically and assigning an event handler in the Page_load as foillows ***************************** Dim chkCatOption As CheckBox = New CheckBox chkCatOption.Text = rowDS.Item("Option_Item_Desc") chkCatOption.ID = rowDS.Item("ID").ToString chkCatOption.Checked = rowDS.Item("HasOption") chkCatOption.AutoPostBack = True AddHandler chkCatOption.CheckedChanged, AddressOf AddOrUpdate
5
2000
by: Guillaume BRAUX | last post by:
Hello, What I want to do is to add a userControl to a form class witch is a different class from the one the button is generated. For example, I want to instanciate a label in "class1" and add it ("show it") on a WinForm situated in "class2" (without having to add code to class2 !) The problem is that I need to instanciate "class2" from "class1" to be able to do a class2.controls.add(myLabel) in class1..
9
3628
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have to create the dynamic controls in the OnInit stage of the lifecycle. Since data from static controls is not yet available in the OnInit stage I can't know what dynamic control I have to create.
1
1901
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating forms / html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to show - text boxes, input boxes, buttons, hyperlinks ie the usual. The data is not obtained directly from a database.
0
9576
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
9407
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9961
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8840
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...
1
7384
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5283
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
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3932
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
3534
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.