asp.net中怎么循环repeater控件取出每行数据
展开全部
DEMO.aspx
asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="Server" Text='<%#Eval("ID") %>'></asp:Label>
<asp:Label ID="lbl_NAME" runat="Server" Text='<%#Eval("NAME") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="btn_Get" runat="Server" OnClick="btn_Get_Click" Text="遍历" />
DEMO.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("NAME");
for (int i = 0; i <= 10; i++ )
{
DataRow dr = dt.NewRow();
dr["ID"] = i;
dr["NAME"] = "NAME" + i.ToString();
dt.Rows.Add(dr);
}
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
}
protected void btn_Get_Click(object sender, EventArgs e)
{
foreach (RepeaterItem rs in Repeater1.Items)
{
string str_ID = ((Label)rs.FindControl("lbl_ID")).Text;
string str_Name = ((Label)rs.FindControl("lbl_Name")).Text;
string str_Message = "ID:" + str_ID + " Name:" + str_Name;
Response.Write("<script>alert('" + str_Message + "')</script>");
}
}
为了演示,我直接用DataTable作为数据源,遍历的方式就是这样,如果你的页面有嵌套母版页的话要注意FindControl()里的控件名称不是控件的ID,而是控件.ClientID。
asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Label ID="lbl_ID" runat="Server" Text='<%#Eval("ID") %>'></asp:Label>
<asp:Label ID="lbl_NAME" runat="Server" Text='<%#Eval("NAME") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="btn_Get" runat="Server" OnClick="btn_Get_Click" Text="遍历" />
DEMO.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("NAME");
for (int i = 0; i <= 10; i++ )
{
DataRow dr = dt.NewRow();
dr["ID"] = i;
dr["NAME"] = "NAME" + i.ToString();
dt.Rows.Add(dr);
}
this.Repeater1.DataSource = dt;
this.Repeater1.DataBind();
}
protected void btn_Get_Click(object sender, EventArgs e)
{
foreach (RepeaterItem rs in Repeater1.Items)
{
string str_ID = ((Label)rs.FindControl("lbl_ID")).Text;
string str_Name = ((Label)rs.FindControl("lbl_Name")).Text;
string str_Message = "ID:" + str_ID + " Name:" + str_Name;
Response.Write("<script>alert('" + str_Message + "')</script>");
}
}
为了演示,我直接用DataTable作为数据源,遍历的方式就是这样,如果你的页面有嵌套母版页的话要注意FindControl()里的控件名称不是控件的ID,而是控件.ClientID。
展开全部
DEMO.aspx
asp:Repeater
ID="Repeater1"
runat="server">
<ItemTemplate>
<asp:Label
ID="lbl_ID"
runat="Server"
Text='<%#Eval("ID")
%>'></asp:Label>
<asp:Label
ID="lbl_NAME"
runat="Server"
Text='<%#Eval("NAME")
%>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
<asp:Button
ID="btn_Get"
runat="Server"
OnClick="btn_Get_Click"
Text="遍历"
/>
DEMO.aspx.cs
protected
void
Page_Load(object
sender,
EventArgs
e)
{
DataTable
dt
=
new
DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("NAME");
for
(int
i
=
0;
i
<=
10;
i++
)
{
DataRow
dr
=
dt.NewRow();
dr["ID"]
=
i;
dr["NAME"]
=
"NAME"
+
i.ToString();
dt.Rows.Add(dr);
}
this.Repeater1.DataSource
=
dt;
this.Repeater1.DataBind();
}
protected
void
btn_Get_Click(object
sender,
EventArgs
e)
{
foreach
(RepeaterItem
rs
in
Repeater1.Items)
{
string
str_ID
=
((Label)rs.FindControl("lbl_ID")).Text;
string
str_Name
=
((Label)rs.FindControl("lbl_Name")).Text;
string
str_Message
=
"ID:"
+
str_ID
+
"
Name:"
+
str_Name;
Response.Write("<script>alert('"
+
str_Message
+
"')</script>");
}
}
为了演示,我直接用DataTable作为数据源,遍历的方式就是这样,如果你的页面有嵌套母版页的话要注意FindControl()里的控件名称不是控件的ID,而是控件.ClientID。
asp:Repeater
ID="Repeater1"
runat="server">
<ItemTemplate>
<asp:Label
ID="lbl_ID"
runat="Server"
Text='<%#Eval("ID")
%>'></asp:Label>
<asp:Label
ID="lbl_NAME"
runat="Server"
Text='<%#Eval("NAME")
%>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
<asp:Button
ID="btn_Get"
runat="Server"
OnClick="btn_Get_Click"
Text="遍历"
/>
DEMO.aspx.cs
protected
void
Page_Load(object
sender,
EventArgs
e)
{
DataTable
dt
=
new
DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("NAME");
for
(int
i
=
0;
i
<=
10;
i++
)
{
DataRow
dr
=
dt.NewRow();
dr["ID"]
=
i;
dr["NAME"]
=
"NAME"
+
i.ToString();
dt.Rows.Add(dr);
}
this.Repeater1.DataSource
=
dt;
this.Repeater1.DataBind();
}
protected
void
btn_Get_Click(object
sender,
EventArgs
e)
{
foreach
(RepeaterItem
rs
in
Repeater1.Items)
{
string
str_ID
=
((Label)rs.FindControl("lbl_ID")).Text;
string
str_Name
=
((Label)rs.FindControl("lbl_Name")).Text;
string
str_Message
=
"ID:"
+
str_ID
+
"
Name:"
+
str_Name;
Response.Write("<script>alert('"
+
str_Message
+
"')</script>");
}
}
为了演示,我直接用DataTable作为数据源,遍历的方式就是这样,如果你的页面有嵌套母版页的话要注意FindControl()里的控件名称不是控件的ID,而是控件.ClientID。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询