Protected Sub Page_Load(Src As Object, E As EventArgs) DataBind() End Sub |
<script language="VB" runat="server"> Sub SubmitBtn_Click(sender As Object, e As EventArgs) Page.DataBind End Sub </script> <B>帮韦小宝选择一个老婆</B> <form runat=server> <asp:DropDownList id="StateList" runat="server"> <asp:ListItem>阿珂</asp:ListItem> <asp:ListItem>曾柔</asp:ListItem> <asp:ListItem>建宁</asp:ListItem> <asp:ListItem>沐公主</asp:ListItem> <asp:ListItem>双儿</asp:ListItem> <asp:ListItem>教主夫人</asp:ListItem> </asp:DropDownList> <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat=server/> <p> 您帮韦小宝选择的老婆是 : <asp:label text='<%# StateList.SelectedItem.Text %>' runat=server/> </form> |
我们看到,那个选择老婆的地方没有使用什么控件,但是他却能正确的显示我们的选择结果,这个是就捆绑的结果,注意<%# StateList.SelectedItem.Text %>这句话,正是它让我们取得了捆绑的数据。更多的地方,我们会经常看到这样的例子,好像程序里面什么都没有,但数据已经绑定在上面了。
4.4 绑定一个数组,然后把它们用列表的方法排列出来。
<%@ Import namespace="System.Data" %> <script language="VB" runat="server"> Sub Page_Load(sender As Object, e As EventArgs) If Not IsPostBack Then Dim values as ArrayList= new ArrayList() values.Add ("阿珂") values.Add ("曾柔") values.Add ("建宁") values.Add ("沐公主") values.Add ("双儿") values.Add ("教主夫人") Dim dt As DataTable Dim dr As DataRow Dim i As Integer 'create a DataTable dt = New DataTable dt.Columns.Add(New DataColumn("编号", GetType(Integer))) dt.Columns.Add(New DataColumn("类型", GetType(String))) dt.Columns.Add(New DataColumn("是否", GetType(String))) 'Make some rows and put some sample data in For i = 1 To 5 dr = dt.NewRow() dr(0) = i dr(1) = values(i-1).ToString() If (i > 3) Then dr(2) = "是" Else dr(2) = "否" End If 'add the row to the datatable dt.Rows.Add(dr) Next DataGrid1.DataSource = new DataView(dt) DataGrid1.DataBind End If End Sub </script> <form runat=server> <B>下面是韦小宝的老婆吗?</B> <asp:DataGrid id="dataGrid1" runat="server" BorderColor="black" BorderWidth="1" GridLines="Both" CellPadding="3" CellSpacing="0" HeaderStyle-BackColor="#aaaadd" /> </form> |
<property name="Columns"> <asp:BoundColumn HeaderText="老婆的编号" DataField="编号" /> <asp:BoundColumn HeaderText="老婆的名字" DataField="名字"/> <asp:BoundColumn HeaderText="是否" DataField="是否"/> </property> |
<property name="Columns"> <asp:BoundColumn HeaderText="编号" DataField="编号" /> <asp:HyperLinkColumn HeaderText="名字" DataNavigateUrlField="名字" DataNavigateUrlFormatString="detailspage.aspx?id={0}" DataTextField="名字" Target="_new" /> <asp:BoundColumn HeaderText="是否" DataField="是否"/> </property> |
<property name="Columns"> <asp:TemplateColumn HeaderText="详细资料"> <template name="ItemTemplate"> <asp:hyperlink id=HyperLink1 NavigateUrl = "detailspage.aspx?id={0}" runat="server"> 点这里查看详细资料 </asp:hyperlink> </template> </asp:TemplateColumn> <asp:BoundColumn HeaderText="老婆的名字" DataField="名字"/> <asp:BoundColumn HeaderText="是否" DataField="是否"/> </property> |
Sub MyDataGrid_Sort(sender As Object, e As DataGridSortCommandEventArgs) SortField = e.SortField DataGrid1.DataBind End Sub |