📄 hashtable-binding.aspx
字号:
<%@Page Language="C#"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Repeated-Value Data Binding to a HashTable</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:9pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
</style></head>
<body bgcolor="#ffffff">
<span class="heading">Repeated-Value Data Binding to a HashTable</span><hr />
<!--------------------------------------------------------------------------->
<form runat="server">
<table border="0">
<tr><td nowrap="nowrap">
HTML <b><select></b> elements:<br />
<select id="MySelectList1" runat="server" />
<select id="MySelectList2" runat="server" /><p />
<b><ASP:DropDownList></b> controls:<br />
<ASP:DropDownList id="MyDropDown1" runat="server" />
<ASP:DropDownList id="MyDropDown2" runat="server" /><p />
<b><ASP:ListBox></b> controls:<br />
<ASP:ListBox id="MyASPList1" runat="server" />
<ASP:ListBox id="MyASPList2" runat="server" /><p />
<b><ASP:DataGrid></b> control:<br />
<ASP:DataGrid id="MyDataGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<ASP:BoundColumn HeaderText="Key" DataField="Key" />
<ASP:BoundColumn HeaderText="Value" DataField="Value" DataFormatString="{0:c}" />
</Columns>
</ASP:DataGrid><p />
<b><ASP:Repeater></b> control:<br />
<ASP:Repeater id="MyRepeater" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Key") %> =
<%# DataBinder.Eval(Container.DataItem, "Value") %><br />
</ItemTemplate>
</ASP:Repeater><p />
</td><td nowrap="nowrap"> </td><td nowrap="nowrap">
<b><ASP:DataList></b> control:<br />
<ASP:DataList id="MyDataList" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Key") %> - value:
<%# DataBinder.Eval(Container.DataItem, "Value", "{0:E}") %>
</ItemTemplate>
</ASP:DataList><p />
<b><ASP:CheckBoxList></b> control:<br />
<ASP:CheckBoxList id="MyCheckList" runat="server" /><p />
<b><ASP:RadioButtonList></b> control:<br />
<ASP:RadioButtonList id="MyRadioList" runat="server" /><p />
</td></tr>
</table>
</form>
<!--------------------------------------------------------------------------->
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// create a HashTable of values to bind to
Hashtable tabValues = new Hashtable(5);
tabValues.Add("Microsoft", 49.56);
tabValues.Add("Sun", 28.33);
tabValues.Add("IBM", 55);
tabValues.Add("Compaq", 20.74);
tabValues.Add("Oracle", 41.1);
// set the DataSource property of the controls
// first <select> displays the Keys in the HashTable
MySelectList1.DataSource = tabValues;
MySelectList1.DataTextField = "Key";
// second one displays the Values in the HashTable
// and uses the Keys as the <option> values
MySelectList2.DataSource = tabValues;
MySelectList2.DataValueField = "Key";
MySelectList2.DataTextField = "Value";
// same applies to ASP: controls, except here
// we can also specify the format of the Key
MyDropDown1.DataSource = tabValues;
MyDropDown1.DataTextField = "Key";
MyDropDown2.DataSource = tabValues;
MyDropDown2.DataValueField = "Key";
MyDropDown2.DataTextField = "Value";
MyDropDown2.DataTextFormatString = "{0:F}";
MyASPList1.DataSource = tabValues;
MyASPList1.DataTextField = "Key";
MyASPList2.DataSource = tabValues;
MyASPList2.DataValueField = "Key";
MyASPList2.DataTextField = "Value";
MyASPList2.DataTextFormatString = "{0:C}";
// to bind a HashTable to a DataGrid, Repeater or DataList
// we have to specify the columns. This is done within the
// element definitions above using an <ItemTemplate>, so
// we just have to specify the DataSource in the code here
MyDataGrid.DataSource = tabValues;
MyRepeater.DataSource = tabValues;
MyDataList.DataSource = tabValues;
// in the CheckboxList we'll display the Title and
// use the Value as the control value
MyCheckList.DataSource = tabValues;
MyCheckList.DataValueField = "Value";
MyCheckList.DataTextField = "Key";
// in the RadioList we'll display and format the
// Value and use the Key as the control value
MyRadioList.DataSource = tabValues;
MyRadioList.DataValueField = "Key";
MyRadioList.DataTextField = "Value";
MyRadioList.DataTextFormatString = "Percentage rate {0:F}%";
// finally, bind all the controls on the page
Page.DataBind();
}
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -