当单击Button
中的GridView
时,我似乎无法获取行索引或要删除的行,我需要存档的Id和完整路径,它们都在GridView
的绑定字段中,这是我的代码:
Gridview代码:
<asp:GridView ID="gdvData"
AllowSorting="False"
AllowPaging="True"
AutoGenerateColumns="False"
AutoGenerateDeleteButton="False"
runat="server"
EmptyDataText="No existen archivos cargados."
Width="100%">
<AlternatingRowStyle CssClass="alternatingrowstyle" />
<Columns>
<asp:BoundField HeaderText="Id"
DataField="Id"
Visible="false" >
<HeaderStyle CssClass="left" />
<ItemStyle CssClass="left" />
</asp:BoundField>
<asp:BoundField HeaderText="RutaCompleta"
DataField="RutaCompleta"
Visible="false" >
<HeaderStyle CssClass="left" />
<ItemStyle CssClass="left" />
</asp:BoundField>
<asp:TemplateField HeaderText="Archivo">
<ItemTemplate>
<asp:HyperLink runat="server"
CssClass="left"
Target="_blank"
NavigateUrl='<%#Eval("RutaCompleta")%>'
Text='<%#Eval("Archivo")%>'>
</asp:HyperLink>
</ItemTemplate>
<HeaderStyle CssClass="left" />
<ItemStyle HorizontalAlign="left" />
</asp:TemplateField>
<asp:TemplateField HeaderImageUrl="~/Images/page_delete.ico"
HeaderText="Eliminar">
<ItemTemplate>
<asp:ImageButton ID="ImgDelete"
runat="server"
CommandArgument="Delete"
ImageUrl="~/Images/page_delete.ico"
OnClick="btnEliminar_Click"
OnClientClick="return confirm('¿Esta seguro de eliminar este archivo?');"
ToolTip="Borrar Documento"/>
</ItemTemplate>
<HeaderStyle CssClass="center" />
<ItemStyle CssClass="center"/>
</asp:TemplateField>
</Columns>
<HeaderStyle CssClass="headerstyle" />
<PagerStyle CssClass="pagerstyle" />
<PagerTemplate>
<asp:Label ID="Label1" runat="server" Text="Mostrar filas:" />
<asp:DropDownList ID="ddlPageSize"
runat="server"
AutoPostBack="true"
CssClass="CombosBox"
>
<asp:ListItem Value="10" />
<asp:ListItem Value="15" />
<asp:ListItem Value="20" />
</asp:DropDownList>
<asp:Label ID="lblDesde" runat="server" Text="Página" />
<asp:TextBox ID="txtGoToPage"
runat="server"
AutoPostBack="true"
CssClass="gotopage"
/>
<asp:Label ID="lblHasta" runat="server" Text="de " />
<asp:Label ID="lblTotalNumberOfPages" runat="server" />
<asp:Button ID="btnAnt"
runat="server"
CommandArgument="Prev"
CommandName="Page"
CssClass="previous"
ToolTip="ant. página" />
<asp:Button ID="btnProx"
runat="server"
CommandArgument="Next"
CommandName="Page"
CssClass="next"
ToolTip="prox. página" />
</PagerTemplate>
</asp:GridView>
代码背后:
protected void btnEliminar_Click(object sender, EventArgs e)
{
try
{
Int64 intId = 0;
String strRutaCompleta = String.Empty;
GridViewRow row = (GridViewRow)(sender as Control).Parent.Parent;
Label lblId = (Label)row.FindControl("lblId");
Label lblRutaCompleta = (Label)row.FindControl("lblRutaCompleta");
intId = Convert.ToInt64(lblId.Text.ToString());
strRutaCompleta = lblRutaCompleta.Text.ToString();
/*Other part of the code*/
}
catch(Exception)
{
/*Other part of the code*/
}
}
我已经尝试过在StackOverflow上找到的各种方法,希望你们能帮助我。感谢。
答案 0 :(得分:0)
我建议使用.Parent.Parent
的{{1}}属性,并设置数据项的CommandArgument
,而不是使用ImageButton
。
这是StackOverflow上的另一篇文章,提供了一些代码:ASP.NET GridView RowIndex As CommandArgument
在我看来,您需要的只是要删除的数据的Id
。因此,不要试图使用行的索引,只需将Id
放在按钮的Id
示例:强>
CommandArgument
代码背后:
<asp:ImageButton ID="ImgDelete"
runat="server"
CommandArgument="Delete"
ImageUrl="~/Images/page_delete.ico"
OnClick="btnEliminar_Click"
CommandArgument='<%#Eval("Id")%>'
OnClientClick="return confirm('¿Esta seguro de eliminar este archivo?');"
ToolTip="Borrar Documento"/>