asp.net - How to hard-code a GridView in my .aspx file? -
i want have gridview static data in it, not linked database or data source, , hard-code directly in aspx file.
i'm brand new asp.net , have no idea i'm doing, , whatever reason can't find online how this.
i'm trying create one-column table heading of "hello world" , 2 data items, "hello" , world". here i'm trying, nothing showing on page when run it:
<asp:gridview id="gridview" runat="server"> <columns> <asp:templatefield headertext ="hello world"> <itemtemplate> <asp:label id="lblhello" runat ="server" text ="hello"/> </itemtemplate> <itemtemplate> <asp:label id="lblworld" runat ="server" text ="world"/> </itemtemplate> </asp:templatefield> </columns> </asp:gridview>
you want assign either ienumerable, dataset or datatable display data in gridview.
<asp:gridview id="gridview" runat="server" autogeneratecolumns="false"> <columns> <asp:templatefield headertext="hello world"> <itemtemplate> <asp:label id="lblhello" runat="server" text='<%# eval("text1") %>' /> <asp:label id="lblworld" runat="server" text='<%# eval("text2") %>' /> </itemtemplate> </asp:templatefield> </columns> </asp:gridview> public class item { public string text1 { get; set; } public string text2 { get; set; } } protected void page_load(object sender, eventargs e) { gridview.datasource = new list<item> { new item {text1 = "hello", text2 = "world"} }; gridview.databind(); }
updated:
<script runat="server"> protected void page_load(object sender, eventargs e) { gridview.datasource = new dictionary<string, string> { { "hello", "world" } }; gridview.databind(); } </script> <asp:gridview id="gridview" runat="server" autogeneratecolumns="false"> <columns> <asp:templatefield headertext="hello world"> <itemtemplate> <asp:label id="lblhello" runat="server" text='<%# eval("key") %>' /> <asp:label id="lblworld" runat="server" text='<%# eval("value") %>' /> </itemtemplate> </asp:templatefield> </columns> </asp:gridview>
Comments
Post a Comment