C# 获取选中行的行索引
当我的C# 获取选中行的行索引,c#,asp.net,gridview,checkbox,C#,Asp.net,Gridview,Checkbox,当我的gridview中的复选框列被标记时,我将得到行索引。我的gridview在一个中继器中,当我设置gridview时,我放置了一个DataKeyNames: <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound"> <ItemTemplate> <asp:Panel ID="pBody1" runat="s
gridview
中的复选框
列被标记时,我将得到行索引。我的gridview
在一个中继器中,当我设置gridview
时,我放置了一个DataKeyNames
:
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:Panel ID="pBody1" runat="server" CssClass="cpBody">
<asp:Label ID="lblBodyText1" runat="server" />
<!-- Grid view to show products based on each category -->
<asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False" Width="998px" CellPadding="4" ForeColor="#333333" GridLines="None" ShowHeader="False" DataKeyNames="id">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="cbCheckRow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="Name" ItemStyle-Width="600px" />
<asp:BoundField DataField="categoryName" HeaderText="Category" />
<asp:TemplateField HeaderText="Quantity" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:TextBox ID="tbQuantity" runat="server" Width="60" Text='<%# DataBinder.Eval(Container.DataItem, "inventoryQuantity") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
<asp:CollapsiblePanelExtender ID="cpe1" runat="server" TargetControlID="pBody1" CollapseControlID="pHeader1"
ExpandControlID="pHeader1" Collapsed="true" ImageControlID="imgArrows1"
CollapsedImage="~/Images/downarrow.jpg"
ExpandedImage="~/Images/uparrow.jpg" TextLabelID="lblHeaderText1" CollapsedText="Show"
ExpandedText="Hide" CollapsedSize="0"
ScrollContents="false">
</asp:CollapsiblePanelExtender>
</ItemTemplate>
</asp:Repeater>
<asp:LinkButton ID="lbnConfirm" runat="server" class="btn dark" style="float: right" OnClick="lbnConfirm_Click">Confirm</asp:LinkButton>
当我运行页面时,它告诉我,对象引用未设置为该行的实例
:
foreach (GridViewRow gr in gv.Rows)
string prodID = this.gv.DataKeys[row].Value.ToString();
此外,此行的gv
:
foreach (GridViewRow gr in gv.Rows)
string prodID = this.gv.DataKeys[row].Value.ToString();
告诉我,gv
不包含缺少引用的定义。我想我在上面的代码中声明了
编辑部分:
protected void lbnConfirm_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
Panel pnl = item.FindControl("pBody1") as Panel;
GridView gv = pnl.FindControl("gvProduct") as GridView;
foreach (GridViewRow gr in gv.Rows)
{
CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbCheckRow");
if (cb.Checked)
{
string prodID = gv.DataKeys[gr.RowIndex].Value.ToString();
tempList.Add(prodID);
for (int count = 0; count < tempList.Count; count++)
{
lblTest.Text = tempList[count] + ",";
}
}
}
}
}
}
受保护的无效LBN确认\u单击(对象发送方,事件参数e)
{
foreach(Repeater1.Items中的RepeaterItem项)
{
如果(item.ItemType==ListItemType.item | | item.ItemType==ListItemType.AlternatingItem)
{
Panel pnl=item.FindControl(“pBody1”)作为面板;
GridView gv=pnl.FindControl(“gvProduct”)作为GridView;
foreach(gv.行中的GridViewRow gr)
{
复选框cb=(复选框)gr.Cells[0]。FindControl(“cbCheckRow”);
如果(cb.选中)
{
字符串prodID=gv.DataKeys[gr.RowIndex].Value.ToString();
添加(prodID);
对于(int count=0;count
获取RowDataBound事件中的行索引:
protected void gvProduct_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.commandName=="select")
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int index = e.Row.RowIndex;
CheckBox chk = (CheckBox)e.Row.FindControl("cbCheckRow");
int code = Convert.ToInt32(this.gvProduct.DataKeys[e.Row.RowIndex].Value);
}
}
}
你的方法是正确的,但是你需要考虑更多的事情:
中继器
的项目,并在中找到面板
每一项面板
中找到网格视图
,而不是在中继器
中RowIndex
而不是通过行来查找DataKey
值<asp:Label ID="lblTest" runat="server" Text=""></asp:Label>
上面的代码在我的测试中运行良好!只是您必须小心,不要在第_Load()页的回发中重新绑定中继器
希望有帮助 但我如何知道复选框列是否已选中?如中所示,我应该将我的LBN公司中的所有代码移到这里?我已经编辑了我的答案。。。!!为了帮助您了解如何使用datakeys获取值,但在gvProduct中,它告诉not不包含“gvProduct”的定义,并且没有接受第一个参数类型的扩展方法,我在上面的代码中遇到了同样的问题。此外,我在网格视图中没有按钮字段,因此无法使用命令名。只需选中复选框即可选中此链接您必须从Gridview属性中选择事件RowDatabound I已添加这些代码以测试prodID获取是否正确(抱歉,请再次检查编辑的部分)。然而,无论我检查了哪些产品,它总是返回一个奇怪的值,因为我不知道该值来自何处。哦,是的,我忘了连接prodID,这就是为什么它总是返回一个值而不是一个列表。谢谢!很高兴它帮了忙:)然而,它不断地重复记录。假设我检查了产品1,2,6,7。它返回我1,1,2,1,2,6,1,2,6,7。我猜在第一次检查时,它返回我1。在第二次检查时,它返回1,2,依此类推。我猜是因为我在使用中继器和可折叠面板扩展器,这就是为什么它不断循环的原因。有什么方法可以解决这个问题吗?@Gwen-我认为你的测试有一些问题,你不应该在循环中循环列表。我已经为此添加了测试代码,这对我很有用。你能检查一下吗?