Repeater 里如何使用服务器控件?
在Repeater 里如何使用服务器控件?我要在Repeater 里使用HyperLink1,可Repeater 嵌套后就出错!!
--------------------编程问答-------------------- <ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"></asp:HyperLink>
</ItemTemplate> --------------------编程问答-------------------- 把你嵌套的代码贴出来看看 --------------------编程问答-------------------- 例子:
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
<asp:Repeater ID="Repeater1" runat="server" OnItemCreated="Repeater1_ItemCreated">
<HeaderTemplate>
<table>
<tr>
<td>ID</td>
<td>操作</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%#Eval("ID") %>
</td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server">入库</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(System.Int32));
for (int i = 0; i < 10; i++) {
DataRow r = dt.NewRow();
r[0] = i;
dt.Rows.Add(r);
}
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
{
LinkButton lb = (LinkButton)e.Item.FindControl("LinkButton1");
if (lb != null) {
lb.Click += new EventHandler(LinkButton1_Click); //关键在这里给LinkButton添加Click事件
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lb=(LinkButton)sender;
RepeaterItem rt = (RepeaterItem)lb.Parent; //关键在这里找到Repeater当前绑定项
Label1.Text =((DataBoundLiteralControl)rt.Controls[0]).Text;
}
}
上面给出了一个简单的例子,其它的复杂操作可以根据这个举一反三。
--------------------编程问答-------------------- 每一次回发都绑定后台数据?这不但大大浪费了系统资源(程序很慢),而且也还有逻辑错误,会出现“未能加载视图状态。正在向其中加载视图状态的控件树必须与前一请求期间用于保存视图状态的控件树相匹配.....”这类错误。 --------------------编程问答--------------------
oh yeah --------------------编程问答-------------------- 学习。。。。。。。。。。。。。 --------------------编程问答-------------------- 学习。。。。。。。。。。 --------------------编程问答-------------------- <asp:HyperLink ID="" runat="server"> </asp:HyperLink> --------------------编程问答-------------------- <asp:HyperLink ID="" runat="server"> </asp:HyperLink>
补充:.NET技术 , ASP.NET