⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hashtable-binding.aspx

📁 东软内部材料(四)asp等相关的教学案例 
💻 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>&lt;select&gt;</b> elements:<br />
<select id="MySelectList1" runat="server" /> &nbsp;
<select id="MySelectList2" runat="server" /><p />

<b>&lt;ASP:DropDownList&gt;</b> controls:<br />
<ASP:DropDownList id="MyDropDown1" runat="server" /> &nbsp;
<ASP:DropDownList id="MyDropDown2" runat="server" /><p />

<b>&lt;ASP:ListBox&gt;</b> controls:<br />
<ASP:ListBox id="MyASPList1" runat="server" /> &nbsp;
<ASP:ListBox id="MyASPList2" runat="server" /><p />

<b>&lt;ASP:DataGrid&gt;</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>&lt;ASP:Repeater&gt;</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">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</td><td nowrap="nowrap">


<b>&lt;ASP:DataList&gt;</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>&lt;ASP:CheckBoxList&gt;</b> control:<br />
<ASP:CheckBoxList id="MyCheckList" runat="server" /><p />

<b>&lt;ASP:RadioButtonList&gt;</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 + -