我需要Asp.Net网站项目中的数据绑定方面的帮助。我在前端构建一个数据列表,现在想要绑定一个名为" Test"到这个数据列表。我怎么能得到这个?
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" %>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
DataList1.DataSource = "Test";
DataList1.DataBind();
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent1" Runat="Server">
<form id="form1" runat="server">
<div class="contentText">
<div class="row">
<label for="name">Name:</label>
<input type="text" id="name" name=""><br><br>
</div>
<div class="row">
<label for="address">Address:</label>
<input type="text" id="address" name=""><br><br>
</div>
<div class="row">
<label for="phone">Phone:</label>
<input type="tel" id="phone" name=""><br><br>
</div>
<div class="row">
<label for="email">Email:</label>
<input type="email" id="email" name="">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:DataList ID="DataList1" runat="server">
</asp:DataList>
</div>
</div>
</form>
</asp:Content>
希望有人能帮助我。感谢...
答案 0 :(得分:1)
DataList是一个控件,应该用于显示实现IListSource
或IEnumerable
接口的可重复数据列表。如果你想绑定一个值只是使用标签代替。但无论如何,如果你真的想强制将单个值绑定到DataList这里是一个例子:
<强> .ASPX:强>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label Text='<%# Eval("Value") %>' runat="server" />
</ItemTemplate>
</asp:DataList>
代码背后:
protected void Button1_Click(object sender, EventArgs e)
{
DataList1.DataSource = new List<object> { new { Value = "Test" } };
DataList1.DataBind();
}