你要想获得Repeater里服务器控件(runat="server")的值 必须先知道Repeater里服务器控件ID的命名方式 如果你的页面里的数据只是只读数据可以采用“pwroselove”提供的方法 去掉runat="server" 自己为控件的ID命名 如果你需要和后台交互 就可以用下面的方法
首先Repeater里服务器控件ID的命名方式是 Repeater.ClientID_ctl行号_控件名 行号是从00开始的两位数00 01 02... 如 Repeater1_ctl00_lblBegin 表示Repeater里第一行lblBegin的ID 给你写个例子你就明白了 代码如下
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
using (DataTable dataTable = new DataTable())
{
dataTable.Columns.AddRange(new DataColumn[] { new DataColumn("Begin"), new DataColumn("Deadline") });
dataTable.Rows.Add(new object[] { "123", "A" });
dataTable.Rows.Add(new object[] { "234", "B" });
dataTable.Rows.Add(new object[] { "345", "C" });
Repeater1.DataSource = dataTable;
Repeater1.DataBind();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml";>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<input id="lblBegin" runat="server" value='<%# Eval("Begin")%>' type="text" />
<input id="lblDeadline" runat="server" value='<%# Eval("Deadline")%>' type="text" />
<br />
<br />
</ItemTemplate>
</asp:Repeater>
</div>
<script type="text/javascript">
var repeaterId = '<%=Repeater1.ClientID %>';//Repeater的客户端ID
var rows = <%=Repeater1.Items.Count%>;//Repeater的行数
for (var i = 0; i < rows; i++) {
alert(document.getElementById(repeaterId + "_ctl" + getrownumber(i) + "_lblBegin").value);
alert(document.getElementById(repeaterId + "_ctl" + getrownumber(i) + "_lblDeadline").value);
}
function getrownumber(i) {
if (i > 10) {
return i;
}
else {
return '0' + i;
}
}
</script>
</form>
</body>
</html>