点击templatefield按钮后,无法获取gridview行

本文关键字:获取 gridview templatefield 按钮 点击 | 更新日期: 2023-09-27 18:11:14

当单击GridView中的Button时,我似乎无法获得行索引或要消除的行,我需要存档的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,希望你们能帮助我。谢谢。

点击templatefield按钮后,无法获取gridview行

不使用.Parent.Parent,我建议使用ImageButtonCommandArgument属性并设置数据项的Id

这是StackOverflow上的另一篇文章,给出了很好的代码:. NET GridView RowIndex As commandparameter

在我看来,你所需要的只是你想要删除的数据的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"/>

代码:

protected void btnEliminar_Click(object sender, EventArgs e)
{
    try
    {
        Int64 intId = 0;
        ImageButton btn = (sender as ImageButton)
        if(btn != null)
        {
            Int64 tempId;
            if(Int64.TryParse(btn.CommandArgument, out tempId))
            {
                intId = intId;
                /*Other part of the code*/
            }
        }
    }
    catch(Exception)
    {
        /*Other part of the code*/
    }
}