关于获取repeater内嵌控件的值的问题

zly229190 2010-10-07 09:37:01

上边这幅图的效果效果是用repeater控件实现的。
代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TeacherManage.aspx.cs" Inherits="TeacherManager" %>

<!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>

试卷管理<br />
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="AccessDataSource1"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<table>
<tr><td colspan="3">
<asp:Label ID="lbl_id" runat="server" Text='<%#Eval("id") %>'></asp:Label>  |  科目:<%#Eval("title") %>  |  考试用时:<%#Eval("usetime") %></td></tr>
<tr>
<td><asp:LinkButton ID="LBtn_view" runat="server" CommandName="view">预 览</asp:LinkButton></td>
<td><asp:LinkButton ID="LBtn_edit" runat="server" CommandName="edit">编 辑</asp:LinkButton></td>
<td><asp:LinkButton ID="LBtn_delete" runat="server" CommandName="delete">删 除</asp:LinkButton></td>

</tr>
</table>
</ItemTemplate>
</asp:Repeater>

<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/examSys.mdb"
SelectCommand="SELECT [id], [title], [usetime], [score] FROM [exam_course]">
</asp:AccessDataSource>

</div>

</form>



</body>
</html>


在Repeater1中内嵌了三个linkbutton控件,我希望在用户点击某个按钮时,能够在后台获取相应对象的Label控件的Text值。为此我测试了一下代码:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string qid = "";
for (int i = 0; i < Repeater1.Controls.Count; i++)
{
Label lblid = (Label)Repeater1.Items[i].FindControl("lbl_id");
qid = lblid.Text;
}
switch (e.CommandName)
{

case
"view ": Response.Redirect("View.aspx?tableid="+"question_"+qid.Trim());
Response.Write(e.Item.ItemIndex);
Response.Write(e.CommandArgument); //自定义的参数信息

break;

}
}


发现无论我点击任何一行的预览按钮,程序都是从第一条记录开始执行(也就是说i总从0开始),遍历完所以的数据记录。
但执行switch语句时获取的e.CommandName变量为不存在。

请各位帮忙看看,该怎么实现我所需要的功能。

ps:因为我不是计算机相关专业的,学习编程纯属个人爱好,虽然有学过.net,但都是皮毛。所以为做个小程序,都已经提问几回啦,在此谢谢各位网友的不厌其烦的指导。再次感谢。
...全文
437 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
zp19900704 2010-10-08
  • 打赏
  • 举报
回复
楼上的方法可以
也可以linkbutton加onclick事件,事件里把sender转成linkbutton类型之后获取它的parent,然后转成RepeaterItem类型,然后获取ItemIndex,然后((Label)repeater.Items[ItemIndex].FindControl("控件ID")).Text
koukoujiayi 2010-10-07
  • 打赏
  • 举报
回复
用Repeater1_ItemCommand:
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(e.CommandName=="view" || e.CommandName=="edit" ||e.CommandName=="delete" )
{
int rowIndex=e.Item.ItemIndex;//获得点击的行号
string s = ((Label)Repeater1.Items[rowIndex].FindControl("lbl_id")).Text;//获得lbl_id的值
}
}
bourbon1795 2010-10-07
  • 打赏
  • 举报
回复
lz想获取考试用时,没有用控件绑定的话是找不到的。
zly229190 2010-10-07
  • 打赏
  • 举报
回复
大家辛苦啦
wuyq11 2010-10-07
  • 打赏
  • 举报
回复
设置CommandArgument=<%# Eval("id"(%>
dalmeeme 2010-10-07
  • 打赏
  • 举报
回复
也可以这样:
	protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string index=(e.Item.FindControl("lbl_id") as Label).Text;
switch (e.CommandName)
{
case "view": Response.Write("显示第" + index + "条记录"); break;
case "edit": Response.Write("编辑第" + index + "条记录"); break;
case "delete": Response.Write("删除第" + index + "条记录"); break;
}
}
koukoujiayi 2010-10-07
  • 打赏
  • 举报
回复
可以在Repeater1_ItemCommand获得!
也可以直接在LinkButton的Click事件中获得,我做第二种,如下:
对你的LBtn_view添加click事件
<asp:LinkButton ID="LBtn_view" runat="server" onclick="LBtn_view_Click">预 览</asp:LinkButton>
c#后台带码:
    protected void LBtn_view_Click(object sender, EventArgs e)
{
LinkButton lnk = (LinkButton)sender;
int rowIndex = ((RepeaterItem)lnk.NamingContainer).ItemIndex;//获取点击的行号
string s = ((Label)Repeater1.Items[rowIndex].FindControl("lbl_id")).Text;//获得Label的值
}
dalmeeme 2010-10-07
  • 打赏
  • 举报
回复
我不是已经帮你实现了吗?你怎么不试下?
zly229190 2010-10-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zly229190 的回复:]

补充说明:我希望当用户点击这行的“预览”按钮时,我能够在后台获得的数据是:2
[/Quote]

也就是说,我只要获得唯一的数据,它就是:2
dalmeeme 2010-10-07
  • 打赏
  • 举报
回复
	protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string index=(e.Item.Controls[1] as Label).Text;
switch (e.CommandName)
{
case "view": Response.Write("显示第" + index + "条记录"); break;
case "edit": Response.Write("编辑第" + index + "条记录"); break;
case "delete": Response.Write("删除第" + index + "条记录"); break;
}
}
bourbon1795 2010-10-07
  • 打赏
  • 举报
回复
"view ": 你看你这多个空格,字符串就不一样了。
bourbon1795 2010-10-07
  • 打赏
  • 举报
回复
用控件绑定才能用findxontrol找到
    <asp:Repeater ID="Repeater1" runat="server" 
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<table>
<tr><td colspan="3">
<asp:Label ID="lbl_id" runat="server" Text='<%#Eval("id") %>'></asp:Label>  |  科目:<%#Eval("title") %>  |  考试用时:<asp:Label
ID="Label1" runat="server" Text=‘<%#Eval("usetime") %>‘></asp:Label></td></tr>
<tr>
<td><asp:LinkButton ID="LBtn_view" runat="server" CommandName="view">预 览</asp:LinkButton></td>
<td><asp:LinkButton ID="LBtn_edit" runat="server" CommandName="edit">编 辑</asp:LinkButton></td>
<td><asp:LinkButton ID="LBtn_delete" runat="server" CommandName="delete">删 除</asp:LinkButton></td>

</tr>
</table>
</ItemTemplate>
</asp:Repeater>


        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{

case
"view": var v = Repeater1.Items[e.Item.ItemIndex].FindControl("Label1");
//v已经找到控件了 你转化下就行了
break;

}

}
zly229190 2010-10-07
  • 打赏
  • 举报
回复
也办法定点到某行的label的属性值
zly229190 2010-10-07
  • 打赏
  • 举报
回复
但是总是从头开始遍历,没法定点得到Label.Text的值。
zly229190 2010-10-07
  • 打赏
  • 举报
回复
if (string.Equals(e.CommandName, ""))
{
string a= ((TextBox)e.Item.FindControl("TextBox1")).Text;
}

还是不会执行呀,测试表明CommandName不存在。
dalmeeme 2010-10-07
  • 打赏
  • 举报
回复
取得Label的值:string index=(e.Item.Controls[1] as Label).Text;
wuyq11 2010-10-07
  • 打赏
  • 举报
回复
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (string.Equals(e.CommandName, ""))
{
string a= ((TextBox)e.Item.FindControl("TextBox1")).Text;
}
}
或protected void btn_Click(object sender, EventArgs e)
{
Button btn=sender as Button;
RepeaterItem item =btn.NamingContainer as RepeaterItem;
}
zly229190 2010-10-07
  • 打赏
  • 举报
回复
补充说明:我希望当用户点击这行的“预览”按钮时,我能够在后台获得的数据是:2

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧