使用Repeater控件,如何在后台获取前台的值? 10
我使用了Repeater,我能在前台获取数据库的值<%#Eval("id")%>可是我在后台文件.cs里如何获取id的值呢?我在前台不用<%#Eval("id")%>,而...
我使用了Repeater,我能在前台获取数据库的值<%#Eval("id")%>
可是我在后台文件.cs里如何获取id的值呢?
我在前台不用<%#Eval("id")%>,而在后台.cs文件里获得,怎么获得?
拜托各位高手解答,谢谢!!
最好有例子代码,再次感谢!! 展开
可是我在后台文件.cs里如何获取id的值呢?
我在前台不用<%#Eval("id")%>,而在后台.cs文件里获得,怎么获得?
拜托各位高手解答,谢谢!!
最好有例子代码,再次感谢!! 展开
3个回答
展开全部
你到底是想在后台获取Repeater数据源的值还是获取Repeater前台输入的值?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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 runat="server">
<div runat="server">
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table>
<tr>
<td>姓名</td>
<td>序号</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblId" Text='<%#Eval("Id") %>' runat="server"></asp:Label>
</td>
<td>
<asp:LinkButton ID="lbtID" Text='<%#Eval("name") %>'
CommandArgument='<%#Eval("ID") %>' runat="server" onclick="lbtID_Click"></asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</BODY>
</html>
后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack){
DataTable dt = new DataTable();
dt.Columns.Add("name",typeof(string));
dt.Columns.Add("Id",typeof(string));
DataRow dr = null;
for(int i=0;i<5;i++){
dr=dt.NewRow();
dr["name"]="Kevin"+i;
dr["Id"]="K"+i;
dt.Rows.Add(dr);
}
this.Repeater1.DataSource= dt;
this.Repeater1.DataBind();
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item){
string id = ((Label)e.Item.FindControl("lblId")).Text;
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + id + "')", true);
}
}
protected void lbtID_Click(object sender, EventArgs e)
{
LinkButton lbtId = (LinkButton)sender;
string id = lbtId.CommandArgument.ToString();
ScriptManager.RegisterStartupScript(this,this.GetType(),"","alert('"+id+"')",true);
}
}
不用Eval()好像不行,repeater是一个需要编辑的控件不像GridView有datakeyNames属性可以用
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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 runat="server">
<div runat="server">
<asp:Repeater ID="Repeater1" runat="server"
onitemdatabound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table>
<tr>
<td>姓名</td>
<td>序号</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblId" Text='<%#Eval("Id") %>' runat="server"></asp:Label>
</td>
<td>
<asp:LinkButton ID="lbtID" Text='<%#Eval("name") %>'
CommandArgument='<%#Eval("ID") %>' runat="server" onclick="lbtID_Click"></asp:LinkButton>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</BODY>
</html>
后台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack){
DataTable dt = new DataTable();
dt.Columns.Add("name",typeof(string));
dt.Columns.Add("Id",typeof(string));
DataRow dr = null;
for(int i=0;i<5;i++){
dr=dt.NewRow();
dr["name"]="Kevin"+i;
dr["Id"]="K"+i;
dt.Rows.Add(dr);
}
this.Repeater1.DataSource= dt;
this.Repeater1.DataBind();
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item){
string id = ((Label)e.Item.FindControl("lblId")).Text;
ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + id + "')", true);
}
}
protected void lbtID_Click(object sender, EventArgs e)
{
LinkButton lbtId = (LinkButton)sender;
string id = lbtId.CommandArgument.ToString();
ScriptManager.RegisterStartupScript(this,this.GetType(),"","alert('"+id+"')",true);
}
}
不用Eval()好像不行,repeater是一个需要编辑的控件不像GridView有datakeyNames属性可以用
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询