Is there anyway to auto-generate ASP.NET controls based on fields in a SQLDataSource? It is really easy to create a form in WinForms by dragging the fields onto the form. You can even determine which control will be used (like a dropdown). Is there anyway to do this in ASP.NET? I don't want a DetailsView since I need to have separate controls that are create开发者_如何学God.
Yes you can dynamically add controls at runtime.
So an XML example rather than SQLDatasource is easier to illustrate. Say I have an xml config file which I will use to render my controls dynamically.
You append controls to an <asp:panel />
Say I have 5 xmlNodes that correspond to 5 controls I want to add dynamically (just one shown here), an example node could be:
<controls>
<control id="txtFirstName" type="textbox" x="100" y="200" initialValue="overType me" />
</controls>
Your page will read in the Xml document (or fill a dataset with results from your SQL Database) and you loop through your results set:
For each node as xmlnode in myNodes 'myNodes used myXMLDocument.selectNodes()
Dim myControl as Control
select case node.attributes("type").value
case "textbox"
myControl = new Textbox
case "dropdownlist"
myControl = new Dropdownlist
End select
myControl.ID = node.attributes("id").value
myControl.Attributes.Add("style", "position:absolute; top:" + node.Attributes("y").Value + "px; left:" + node.Attributes("x").Value + "px;")
myPanel.Controls.Add(myControl)
Next
Reading them out again isn't so easy, but let me know if you need help with that too.. p
精彩评论