--这个是看了非哥的BLOG写的
<asp:Label ID="Label1" runat="server" Text='<%#GetName((sender as Label).ID.Substring(5)) %>'></asp:Label> <br /> <asp:Label ID="Label2" runat="server" Text='<%#GetName((sender as Label).ID.Substring(5)) %>'></asp:Label> <br /> <asp:Label ID="Label3" runat="server" Text='<%#GetName((sender as Label).ID.Substring(5)) %>'></asp:Label> <br /> <asp:Label ID="Label4" runat="server" Text='<%#GetName((sender as Label).ID.Substring(5)) %>'></asp:Label>
IDictionary<int, string> Dic = new Dictionary<int, string>(); protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindData(); } } public string GetName(object index) { //如果运算符的左边是非空的就返回左边,否则是右边! index = index ?? " "; int key; int.TryParse(index.ToString(), out key); return Dic[key]; } public void BindData() { Dic.Add(1, "胡果"); Dic.Add(2, "胡磊"); Dic.Add(3, "俊文"); Dic.Add(4, "刘皮"); DataBind(); }
本文展示了一个使用ASP.NET进行动态数据绑定的例子。通过在页面加载时从字典中获取姓名并显示在多个标签上,介绍了如何利用C#代码实现界面元素的数据绑定。此示例涉及ASP.NET服务器控件及C#编程。
72

被折叠的 条评论
为什么被折叠?



