【发布时间】:2011-07-18 02:07:24
【问题描述】:
我想删除 Gridview 中的一个特定行。所以我需要ID,如何获取GridView_RowDeleting事件中行的ID?
这里是GridView的列列表,
<Columns>
<asp:TemplateField HeaderText="S.No." ControlStyle-Width="100%" ItemStyle-Width="30px">
<ItemTemplate>
<asp:Label ID="lblSrno" runat="server" Text=''
<%# Container.DataItemIndex + 1 %>'></asp:Label>
</ItemTemplate>
<ControlStyle Width="100%" />
<ItemStyle Width="30px" />
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label runat="server" ID="lblDrawingID" Text=''
<%# Bind("DrawingID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustDrawingNbr" HeaderText="Customer Drawing No." />
<asp:TemplateField HeaderText="Status" ControlStyle-Width="100px" ItemStyle-Width="100px">
<ItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server" Width="100px" Enabled="false">
</asp:DropDownList>
</ItemTemplate>
<ControlStyle Width="100px" />
<ItemStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label runat="server" ID="lblDrawingPGMAStatusID" Text=''
<%# Bind("StatusID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label runat="server" ID="lblDrawingPGMAID" Text=''
<%# Bind("PGMAID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="true" HeaderText="Action" ItemStyle-HorizontalAlign="Center"
ItemStyle-VerticalAlign="Middle">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:CommandField>
</Columns>
这里我用这个 GridView 填充了一个数据表。 DataTable 列是 DrawingID、CustDrawingNbr、JobOrderID、StatusID、PGMAID、SeqNo。 所以我想在单击删除按钮时获取 DrawingID。
到目前为止, 我尝试了以下事情。但是什么都没有给出预期的结果。
标签 lblDraw = (标签)gvApplicablePGMASearch.Rows[e.RowIndex].Cells[2].FindControl("lblDrawingID"); int drawid = Convert.ToInt32(lblDraw.Text);
string drawid = gvApplicablePGMASearch.DataKeys[e.RowIndex].Value.ToString();
- 字符串 id = gvApplicablePGMASearch.SelectedDataKey.Value.ToString();
但以上都没有给出想要的结果。
【问题讨论】:
-
在我看来 id 应该带有“删除”事件..
-
@Bugai。我不能得到你。请详细说明。
-
你为什么在 TemplateFields 中使用所有这些标签,为什么不使用 asp:BoundField 并设置 DataField?如果您只是将它们用于隐藏 ID,您可能更愿意只在 DataKeyNames 中列出 ID,例如DataKeyNames="DrawingID,StatusID,PGMAID".