databindexample.aspx

来自「asp.net专家200问(含源代码解决法案」· ASPX 代码 · 共 45 行

ASPX
45
字号

<HTML>
	<HEAD>
		<title>将 Hashtable 绑定到 RadioButtonList Web 控件示例</title>
		<script language="C#" runat="server">
			void Page_Load(object sender, System.EventArgs e)
			{
				if(!IsPostBack)
				{
					// 创建 Hashtable 对象并填充数据
					Hashtable hash = new Hashtable();
					hash.Add("北京", "010");
					hash.Add("广州", "020");
					hash.Add("上海", "021");
					hash.Add("天津", "022");
					hash.Add("成都", "028");
					hash.Add("济南", "0531");

					// 进行数据绑定
					RadioButtonList1.DataSource = hash;
					RadioButtonList1.DataTextField = "Key";
					RadioButtonList1.DataValueField = "Value";
					RadioButtonList1.DataBind();
				}
			}
			
			void SubmitBtn_Click(object sender, System.EventArgs e)
			{
				Label1.Text = "区号是:" + RadioButtonList1.SelectedValue;
			}
		
		</script>
	</HEAD>
	<body>
		<form id="Form1" method="post" runat="server">
			<h3>将Hashtable绑定到RadioButtonList Web控件示例</h3>
			<asp:RadioButtonList id="RadioButtonList1" runat="server"></asp:RadioButtonList>
			<asp:Button id="SubmitBtn" runat="server" Text="查看区号" OnClick="SubmitBtn_Click"></asp:Button>
			<br>
			<br>
			<asp:Label id="Label1" runat="server">Label</asp:Label>
		</form>
	</body>
</HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?