我有一个列表视图,我想从列表视图中选择多个项目,点击按钮需要对所选行进行一些操作。但是我无法从列表视图中获取值项目,下面是代码我试过了,我在这里做错了吗?
标记
<asp:ListView ID="LvOrders" runat="server"
GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1"
OnPagePropertiesChanging="OnPagePropertiesChanging">
<LayoutTemplate>
<table id="product-master" class="table table-bordered table-striped">
<tr>
<th><asp:CheckBox ID="ChkBox" runat="server" /></th>
<th>Order #</th>
<th>User Name</th>
<th>Purchase Date</th>
<th>Amount</th>
</tr>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
<tr>
<td colspan="3">
<asp:DataPager ID="DataPager1" runat="server"
class="btn-group btn-group-sm"
PagedControlID="LvOrders"
PageSize="10">
<Fields>
<asp:NextPreviousPagerField PreviousPageText="<"
FirstPageText="|<"
ShowPreviousPageButton="true"
ShowFirstPageButton="true"
ShowNextPageButton="false"
ShowLastPageButton="false"
ButtonCssClass="btn btn-default"
RenderNonBreakingSpacesBetweenControls="false"
RenderDisabledButtonsAsLabels="false" />
<asp:NumericPagerField ButtonType="Link"
CurrentPageLabelCssClass="btn btn-primary disabled"
RenderNonBreakingSpacesBetweenControls="false"
NumericButtonCssClass="btn btn-default"
ButtonCount="10"
NextPageText="..."
NextPreviousButtonCssClass="btn btn-default" />
<asp:NextPreviousPagerField NextPageText=">"
LastPageText=">|"
ShowNextPageButton="true"
ShowLastPageButton="true"
ShowPreviousPageButton="false"
ShowFirstPageButton="false"
ButtonCssClass="btn btn-default"
RenderNonBreakingSpacesBetweenControls="false"
RenderDisabledButtonsAsLabels="false" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1">
</asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td><asp:CheckBox ID="ChkBx" runat="server" /></td>
<td><%# Eval("OrderNo") %></td>
<td><%# Eval("Name") %></td>
<td><%# Eval("Created") %></td>
<td><%# Eval("Amount") %></td>
</ItemTemplate>
</asp:ListView>
C#代码
string res;
protected void btnAssign_Click(object sender, EventArgs e)
{
foreach (ListViewItem row in LvOrders.Items)
{
CheckBox cb = (CheckBox)row.FindControl("ChkBox");
if (cb != null)
{
if (cb.Checked == true)
{
ListViewItem item = LvOrders.Items[LvOrders.SelectedIndex];
String c = item.FindControl("OrderNo").ToString();
res = res + "" + c;// printing selected OrderNo
}
}
}
}
答案 0 :(得分:2)
你写过item.FindControl("OrderNo").ToString();
但是在ASPX页面中你定义了<td><%# Eval("OrderNo") %></td>
所以,你没有定义任何控件,仍然想找到控件。因此,这将导致错误,选项是使用Label,以防您想要找到控件及其值。
PS:标记为答案,如果有帮助
答案 1 :(得分:2)
您必须分配这样的ID。
<div id="aside" class="box">
<div class="padding box">
<!-- Logo (Max. width = 200px) -->
<p id="logo">
<a href="#">
<img src="tmp/logo.gif" alt="Our logo" title="Visit Site" /></a>
</p>
</div>
<!-- /padding -->
<ul class="box">
<li><a href="OPDRegister.aspx">New Patient Register</a></li>
<li><a href="OldPatientRegistration.aspx">Old Patient Register</a></li>
<li><a href="DetailRegistration.aspx">Detail Patient Register</a></li>
<li id="submenu-active"><a href="#">Report</a>
<!-- Active -->
<ul>
<li><a href="#">New Patient Report</a></li>
<li><a href="#">Old Patient Report</a></li>
<li><a href="#">Detail Patient Report</a></li>
</ul>
</li>
</ul>
</div>
现在在代码隐藏
<ItemTemplate>
<td>
<asp:CheckBox ID="ChkBx" runat="server" />
</td>
<td>
<asp:label ID="OrderNo" runat="server"
Text='<%# Eval("OrderNo") %>'>
</asp:label>
</td>
<td><%# Eval("Name") %></td>
<td><%# Eval("Created") %></td>
<td><%# Eval("Amount") %></td>
</ItemTemplate>