Created
December 27, 2015 22:52
-
-
Save miklund/c054453aba0a4bc406ba to your computer and use it in GitHub Desktop.
2009-09-09 Populate an ASP.NET view with data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Title: Populate an ASP.NET view with data | |
| # Author: Mikael Lundin | |
| # Link: http://blog.mikaellundin.name/2009/09/09/populate-an-aspnet-view-with-data.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <asp:XmlDataSource runat="server" ID="CmsPages" DataFile="~/App_Data/cms.xml" /> | |
| <asp:Repeater runat="server" DataSourceID="CmsPages"> | |
| <HeaderTemplate><ul></HeaderTemplate> | |
| <ItemTemplate> | |
| <li> | |
| <a href="<%# Eval("url") %>"><%# Eval("title") %></a> | |
| </li> | |
| </ItemTemplate> | |
| <FooterTemplate></ul></FooterTemplate> | |
| </asp:Repeater> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| protected void Page_Load(object sender, EventArgs e) | |
| { | |
| ColorsDropDownList.DataSource = new[] { "Blue", "Green", "Blue" }; | |
| this.DataBind(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <asp:DropDownList runat="server" ID="FlowersDropDownList" DataSource="<%# GetFlowers() %>" OnLoad="DataBind" /> | |
| protected void DataBind(object sender, EventArgs e) | |
| { | |
| ((Control)sender).DataBind(); | |
| } | |
| protected string[] GetFlowers() | |
| { | |
| return new[] { "Rose", "Lily", "Dragonflower" }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <asp:DropDownList runat="server" ID="NamesDropDownList" OnLoad="Names_DataBind" /> | |
| protected void Names_DataBind(object sender, EventArgs e) | |
| { | |
| var control = (BaseDataBoundControl)sender; | |
| control.DataSource = new[] { "James", "Joe", "Jimmy" }; | |
| control.DataBind(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment