C# 如何在前台页面利用<% %>for循环遍历后台读取的DataTable然后绑定到Label控件????
C#如何在前台页面利用<%%>for循环遍历后台读取的DataTable然后绑定到Label控件????我想把后台从数据库读取出来的DataTable的内容动态绑定到前台...
C# 如何在前台页面利用<% %>for循环遍历后台读取的DataTable然后绑定到Label控件????我想把后台从数据库读取出来的DataTable的内容动态绑定到前台的Label上去。急求答案!!!
展开
7个回答
2013-03-27
展开全部
你要绑定多条记录,用DataTable或者List绑定可以用DataList,Repeater去绑定。不用For循环去读取。相关的代码例子你自己去百度下吧。
<cc1:BaseRepeater ID="rptList" runat="server">
<HeaderTemplate>
<tr class="list-head">
<td width="30%"><input id="chkSelectAll" onclick="SelectAll2(this,'memberList')" type="checkbox" runat="server" />
<cc1:BaseLinkButton ID="LastName" runat="server" Text="Name" CommandName='<%#DisplayFieldName.MemberName %>'></cc1:BaseLinkButton>
</td>
<td width="15%">
Employee #
</td>
<td width="15%">
Position
</td>
<td width="15%">
Email
</td>
<td width="15%">
Work Phone
</td>
<td with="5" align="center">
Status
</td>
<td width="5%" id="tdaction" runat="server" align="center" >Action</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr id="row" runat="server" class="list-body light">
<td><asp:CheckBox ID="chkId" runat="server" />
<asp:LinkButton ID="aView" runat="server" CommandName="view" CommandArgument='<%#"?MemberID="+ Eval("MemberID")%>'><%#EMS.Common.Utility.GetFullName(Eval("LastName").ToString(),Eval("FirstName").ToString())%></asp:LinkButton>
<asp:HiddenField ID="hddStamp" runat="server" Value='<%# Eval("StampContent")%>' />
<asp:HiddenField ID="hddId" runat="server" Value='<%# Eval("MemberID")%>' />
<asp:HiddenField ID="hddStampContent" runat="server" Value='<%# Eval("StampContent")%>' />
</td>
<td>
<%#Eval("WorkCode")%>
</td>
<td>
<%#Eval("Position")%>
</td>
<td>
<%#Eval("Email")%>
</td>
<td>
<%#Eval("WorkPhone")%>
</td>
<td align="center">
<%# Eval("ActiveStatus")%>
</td>
<td align="center" id="tdedit" runat="server">
<asp:LinkButton ID="linkBtnEdit" runat="server" CommandName="edit" CommandArgument='<%#"?MemberID="+ Eval("MemberID")%>' Text="Edit" ></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</cc1:BaseRepeater>
DataSource只要继承可遍历接口就行。
<cc1:BaseRepeater ID="rptList" runat="server">
<HeaderTemplate>
<tr class="list-head">
<td width="30%"><input id="chkSelectAll" onclick="SelectAll2(this,'memberList')" type="checkbox" runat="server" />
<cc1:BaseLinkButton ID="LastName" runat="server" Text="Name" CommandName='<%#DisplayFieldName.MemberName %>'></cc1:BaseLinkButton>
</td>
<td width="15%">
Employee #
</td>
<td width="15%">
Position
</td>
<td width="15%">
</td>
<td width="15%">
Work Phone
</td>
<td with="5" align="center">
Status
</td>
<td width="5%" id="tdaction" runat="server" align="center" >Action</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr id="row" runat="server" class="list-body light">
<td><asp:CheckBox ID="chkId" runat="server" />
<asp:LinkButton ID="aView" runat="server" CommandName="view" CommandArgument='<%#"?MemberID="+ Eval("MemberID")%>'><%#EMS.Common.Utility.GetFullName(Eval("LastName").ToString(),Eval("FirstName").ToString())%></asp:LinkButton>
<asp:HiddenField ID="hddStamp" runat="server" Value='<%# Eval("StampContent")%>' />
<asp:HiddenField ID="hddId" runat="server" Value='<%# Eval("MemberID")%>' />
<asp:HiddenField ID="hddStampContent" runat="server" Value='<%# Eval("StampContent")%>' />
</td>
<td>
<%#Eval("WorkCode")%>
</td>
<td>
<%#Eval("Position")%>
</td>
<td>
<%#Eval("Email")%>
</td>
<td>
<%#Eval("WorkPhone")%>
</td>
<td align="center">
<%# Eval("ActiveStatus")%>
</td>
<td align="center" id="tdedit" runat="server">
<asp:LinkButton ID="linkBtnEdit" runat="server" CommandName="edit" CommandArgument='<%#"?MemberID="+ Eval("MemberID")%>' Text="Edit" ></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</cc1:BaseRepeater>
DataSource只要继承可遍历接口就行。
2013-03-27
展开全部
你要绑定多条记录,用DataTable或者List绑定可以用DataList,Repeater去绑定。不用For循环去读取。相关的代码例子你自己去百度下吧。
<cc1:BaseRepeater ID="rptList" runat="server">
<HeaderTemplate>
<tr class="list-head">
<td width="30%"><input id="chkSelectAll" onclick="SelectAll2(this,'memberList')" type="checkbox" runat="server" />
<cc1:BaseLinkButton ID="LastName" runat="server" Text="Name" CommandName='<%#DisplayFieldName.MemberName %>'></cc1:BaseLinkButton>
</td>
<td width="15%">
Employee #
</td>
<td width="15%">
Position
</td>
<td width="15%">
Email
</td>
<td width="15%">
Work Phone
</td>
<td with="5" align="center">
Status
</td>
<td width="5%" id="tdaction" runat="server" align="center" >Action</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr id="row" runat="server" class="list-body light">
<td><asp:CheckBox ID="chkId" runat="server" />
<asp:LinkButton ID="aView" runat="server" CommandName="view" CommandArgument='<%#"?MemberID="+ Eval("MemberID")%>'><%#EMS.Common.Utility.GetFullName(Eval("LastName").ToString(),Eval("FirstName").ToString())%></asp:LinkButton>
<asp:HiddenField ID="hddStamp" runat="server" Value='<%# Eval("StampContent")%>' />
<asp:HiddenField ID="hddId" runat="server" Value='<%# Eval("MemberID")%>' />
<asp:HiddenField ID="hddStampContent" runat="server" Value='<%# Eval("StampContent")%>' />
</td>
<td>
<%#Eval("WorkCode")%>
</td>
<td>
<%#Eval("Position")%>
</td>
<td>
<%#Eval("Email")%>
</td>
<td>
<%#Eval("WorkPhone")%>
</td>
<td align="center">
<%# Eval("ActiveStatus")%>
</td>
<td align="center" id="tdedit" runat="server">
<asp:LinkButton ID="linkBtnEdit" runat="server" CommandName="edit" CommandArgument='<%#"?MemberID="+ Eval("MemberID")%>' Text="Edit" ></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</cc1:BaseRepeater>
DataSource只要继承可遍历接口就行。
<cc1:BaseRepeater ID="rptList" runat="server">
<HeaderTemplate>
<tr class="list-head">
<td width="30%"><input id="chkSelectAll" onclick="SelectAll2(this,'memberList')" type="checkbox" runat="server" />
<cc1:BaseLinkButton ID="LastName" runat="server" Text="Name" CommandName='<%#DisplayFieldName.MemberName %>'></cc1:BaseLinkButton>
</td>
<td width="15%">
Employee #
</td>
<td width="15%">
Position
</td>
<td width="15%">
</td>
<td width="15%">
Work Phone
</td>
<td with="5" align="center">
Status
</td>
<td width="5%" id="tdaction" runat="server" align="center" >Action</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr id="row" runat="server" class="list-body light">
<td><asp:CheckBox ID="chkId" runat="server" />
<asp:LinkButton ID="aView" runat="server" CommandName="view" CommandArgument='<%#"?MemberID="+ Eval("MemberID")%>'><%#EMS.Common.Utility.GetFullName(Eval("LastName").ToString(),Eval("FirstName").ToString())%></asp:LinkButton>
<asp:HiddenField ID="hddStamp" runat="server" Value='<%# Eval("StampContent")%>' />
<asp:HiddenField ID="hddId" runat="server" Value='<%# Eval("MemberID")%>' />
<asp:HiddenField ID="hddStampContent" runat="server" Value='<%# Eval("StampContent")%>' />
</td>
<td>
<%#Eval("WorkCode")%>
</td>
<td>
<%#Eval("Position")%>
</td>
<td>
<%#Eval("Email")%>
</td>
<td>
<%#Eval("WorkPhone")%>
</td>
<td align="center">
<%# Eval("ActiveStatus")%>
</td>
<td align="center" id="tdedit" runat="server">
<asp:LinkButton ID="linkBtnEdit" runat="server" CommandName="edit" CommandArgument='<%#"?MemberID="+ Eval("MemberID")%>' Text="Edit" ></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</cc1:BaseRepeater>
DataSource只要继承可遍历接口就行。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<table>
<thead>
<tr>
<td>编号</td>
<td>标题</td>
<td>类型</td>
<td>发布时间</td>
<td>发布人</td>
<td>图片地址</td>
<td>操作</td>
</tr>
</thead>
<tbody>
<% for (int i = 0; i < moList.Count; i++)%>
<% {%>
<tr>
<td><%= moList[i].ID %></td>
<td><%= moList[i].title %></td>
<td><%= moList[i].type %></td>
<td><%= moList[i].Date %></td>
<td><%= moList[i].people %></td>
<td><%= moList[i].picUrl %></td>
<td><a href="ShowNew.aspx?id=<%= moList[i].ID %>">详情</a>
<a href="delnew.ashx?id=<%= moList[i].ID %>">删除</a></td>
</tr>
<% } %>
</tbody>
</table>
<thead>
<tr>
<td>编号</td>
<td>标题</td>
<td>类型</td>
<td>发布时间</td>
<td>发布人</td>
<td>图片地址</td>
<td>操作</td>
</tr>
</thead>
<tbody>
<% for (int i = 0; i < moList.Count; i++)%>
<% {%>
<tr>
<td><%= moList[i].ID %></td>
<td><%= moList[i].title %></td>
<td><%= moList[i].type %></td>
<td><%= moList[i].Date %></td>
<td><%= moList[i].people %></td>
<td><%= moList[i].picUrl %></td>
<td><a href="ShowNew.aspx?id=<%= moList[i].ID %>">详情</a>
<a href="delnew.ashx?id=<%= moList[i].ID %>">删除</a></td>
</tr>
<% } %>
</tbody>
</table>
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-03-27
展开全部
new 一个 StringBuilder 对象,操做跟放百年些,然后foreach循环出datatable的值,添加到sb里面,定义一个protected权限以上的string变量 将sb值给他 如:string str = sb.Tostring() System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("读出来的值"); 前台:在lable里面<lable id="" ruant="server" text="<%=str%>" /> 就是这样,如果有样式的话 可直接添加到sb里面
sb.Append("读出来的值"); 前台:在lable里面<lable id="" ruant="server" text="<%=str%>" /> 就是这样,如果有样式的话 可直接添加到sb里面
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你可以在前台使用Repeater控件,然后设置Label的Text='<%#Eval("绑定的值")%>'然后后台遍历DataTable赋值
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询