简体   繁体   中英

Can you programmatically create form fields in C#?

Supposing I have to display details on a form by reading an XML file, and according to the no. of elements I want to display the details in form. Is it possible to do so?

Ex: Say my XML file contains the following

<Rooms>
     <Room>
           <Name>A</Name>
           <ID>1</ID>
      </Room>
      <Room>
            <Name>B</Name>
            <ID>2</ID>
      </Room>
 </Rooms>

I can display the above details in the form. in case i add another room element to the xml file,

    <Room>
          <Name>C</Name>
          <ID>3</ID>
    <Room>

I want to display that too in the form. Is that actually possible? Any other alternatives?

您可以使用LINQ-XML来解析XML文档,准备List<T>并使用诸如DataGridView,ListView等控件来显示结果。

My recommendation - translate your XML into strongly-typed objects (so a Room object with the given properties) which you can load into a collection of List<Room> .

Once you've got that, you now have the ability programmatically to add remove etc.

Now you need to bind your UI to that information - a cheap way will be to use the DataGridView control - which, in conjunction with a data source, will automatically support editing and adding/re-ordering/deleting.

If you want something more complex, you can create a custom UserControl for the Room type, and then perhaps another to act as a container for a bunch of those.

I'm not going to post any code because I think there are so many options available to you - and you haven't really explained what you've tried - that to go much further would end up making this answer very long! Want my advice - learn about the DataGridView first, how to customise the column formats and supply custom controls for that - it'll give you a good idea of what you might want from a fully roll-your-own solution.

Sure, that is possible.

But the best option depends on what do you want to do with it. Are you going to edit the data? Or just show it? There are a number of options, you could show it in grid form, or like you suggested, create controls runtime. But then you would have to know what controls and maybe different controls for different data (labels or textboxes ie or is the texbox (in case of the ID) read-only. How are you going to determine those things? Also when you do create controls make sure you make it scrollable somehow, you don't know how much data you are getting so your form could become infinitely long!

For actually creating controls you could check: http://support.microsoft.com/kb/319266

Yes it is possible. Traditionally you would loop through your nodes in the Page_Init method of you aspx.cs file and user code similar to the psuedo code below:

foreach(//loop specified here) {
  Page.Form.Controls.Add(new TextBox() {Id = "Room" + loopValue});
}

Then on post back you should be able to access your dynamic controls using a similar loop to reconstruct the Id. This video should cover the concept in more detail.

http://www.asp.net/web-forms/videos/aspnet-ajax/how-to-dynamically-add-controls-to-a-web-page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM