【问题标题】:Cannot retrieve value on rowdeleting in GridView无法在 GridView 中检索行删除值
【发布时间】:2017-06-14 18:26:12
【问题描述】:

我在检索值以删除记录时遇到问题。 我在带有模板字段的网格视图中使用 OnRowDeleting,我无法检索要删除的记录选定值,这是我的网格:

<asp:GridView ID="gvw_Cli_Emp_EmpData" runat="server" AutoGenerateColumns="false"
        CssClass="mGrid" PagerStyle-CssClass="pgr" Width="50%" AutoGenerateDeleteButton="True" 
        AlternatingRowStyle-CssClass="alt" Font-Size="Small" OnRowDeleting="Borrando">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <a href="#" onclick="window.open('Clientes_Empleados_Detalle.aspx?cliCod= <%#Eval("ClienteCodigo1").ToString() 
                        + "&EmpNom=" + Eval("Empleado1").ToString()
                        + "&EmpCod=" + Eval("IdCliEmp").ToString()
                        + "&idDepart=" + Eval("IdDepartamento").ToString()
                        + "&Depart=" + Eval("Departamento1").ToString()
                        + "&EmpNiv=" + Eval("NivelAcceso1").ToString()    
                        %> ','PrintMe','height=500px,width=800px,scrollbars=1');">Editar</a>
                </ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="CLIENTE">
                <ItemTemplate>
                    <asp:Label ID="lbl_CliEmp_CliCod" runat="server" Text='<%# Eval("ClienteCodigo1") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="CODIGO">
                <ItemTemplate>
                    <asp:Label ID="lbl_CliEmp_EmpId" runat="server" Text='<%# Eval("IdCliEmp") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="EMPLEADO">
                <ItemTemplate>
                    <asp:Label ID="lbl_CliEmp_EmpNom" runat="server" Text='<%# Eval("Empleado1") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="ID DEP">
                <ItemTemplate>
                    <asp:Label ID="lbl_CliEmp_DepId" runat="server" Text='<%# Eval("IdDepartamento") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="DEPARTAMENTO">
                <ItemTemplate>
                    <asp:Label ID="lbl_CliEmp_EmpDep" runat="server" Text='<%# Eval("Departamento1") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="NIVEL">
                <ItemTemplate>
                    <asp:Label ID="lbl_CliEmp_EmpNiv" runat="server" Text='<%# Eval("NivelAcceso1") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
    </asp:GridView>

对于删除:

protected void Borrando(object sender, GridViewDeleteEventArgs e)
{
    string cell = gvw_Cli_Emp_EmpData.Rows[e.RowIndex].Cells[0].Text; //this retuns me ""
    int EmpCliCod = Convert.ToInt32(cell);

    DialogResult dialogResult = MessageBox.Show(new Form { TopMost = true }, "Delete?", "Confirma", MessageBoxButtons.YesNo);
    if (dialogResult == DialogResult.Yes)
    {
        cliEmpBL.clientesEmpleados_SupEmpleado(EmpCliCod); //here execute deletion record from my datalayer 
        Page.ClientScript.RegisterStartupScript(this.GetType(), "AlertScript", "alert('Deleted!');", true);
    }
    else if (dialogResult == DialogResult.No)
    { }
}

所以,如您所见,返回我“”。 我试过解决这个问题,使用:

        System.Windows.Forms.Label EmpId = e.Item.FindControl("lbl_CliEmp_CliCod") as System.Windows.Forms.Label;
    string val = EmpId.Text;

但同样的结果,你知道吗? 拜托,我希望有人能帮助我。

最好的问候

【问题讨论】:

  • MessageBox 不是 asp.net 功能。它适用于winforms。不要在网络环境中使用System.Windows.Form

标签: c# asp.net sql-server gridview


【解决方案1】:

尝试实现以下示例,您将获得所选行的 id

<asp:LinkButton ID="lnkdelete" runat="server" CommandName="Delete" CommandArgument='<%#Eval("ClienteCodigo1")%>'>Delete</asp:LinkButton>

protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "Delete")
    {
      int ID = Convert.ToInt32(e.CommandArgument);
      //now perform the delete operation using ID value
    }
}

【讨论】:

    【解决方案2】:

    感谢 Patrik,您的解决方案帮助了我。

    <asp:GridView ID="gvw_CliEmp_EmpData" runat="server" AutoGenerateColumns="false"
            CssClass="mGrid" PagerStyle-CssClass="pgr" Width="50%"  
            AlternatingRowStyle-CssClass="alt" Font-Size="Small"
            OnRowCommand="gvw_CliEmp_EmpData_RowCommand" OnRowDeleting="gvw_CliEmp_EmpData_RowDeleting">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <a href="#" onclick="window.open('Clientes_Empleados_Detalle.aspx?cliCod= <%#Eval("ClienteCodigo1").ToString() 
                            + "&EmpNom=" + Eval("Empleado1").ToString()
                            + "&EmpCod=" + Eval("IdCliEmp").ToString()
                            + "&idDepart=" + Eval("IdDepartamento").ToString()
                            + "&Depart=" + Eval("Departamento1").ToString()
                            + "&EmpNiv=" + Eval("NivelAcceso1").ToString()    
                            %> ','PrintMe','height=500px,width=800px,scrollbars=1');">Editar</a>
                    </ItemTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="CLIENTE">
                    <ItemTemplate>
                        <asp:Label ID="lbl_CliEmp_CliCod" runat="server" Text='<%# Eval("ClienteCodigo1") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CODIGO">
                    <ItemTemplate>
                        <asp:Label ID="lbl_CliEmp_EmpId" runat="server" Text='<%# Eval("IdCliEmp") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EMPLEADO">
                    <ItemTemplate>
                        <asp:Label ID="lbl_CliEmp_EmpNom" runat="server" Text='<%# Eval("Empleado1") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="ID DEP">
                    <ItemTemplate>
                        <asp:Label ID="lbl_CliEmp_DepId" runat="server" Text='<%# Eval("IdDepartamento") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="DEPARTAMENTO">
                    <ItemTemplate>
                        <asp:Label ID="lbl_CliEmp_EmpDep" runat="server" Text='<%# Eval("Departamento1") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="NIVEL">
                    <ItemTemplate>
                        <asp:Label ID="lbl_CliEmp_EmpNiv" runat="server" Text='<%# Eval("NivelAcceso1") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkdelete" runat="server" CommandName="Delete" 
                            CommandArgument='<%#Eval("IdCliEmp")%>'>Eliminar
                        </asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
    
    
            </Columns>
        </asp:GridView>
    

    和后面的代码:

    #region ==== BORRAR REGISTROS DEL INGRESO ====
    
    protected void gvw_CliEmp_EmpData_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            int EmpCliCod = Convert.ToInt32(e.CommandArgument);
    
            DialogResult dialogResult = MessageBox.Show(new Form { TopMost = true }, "Eliminar?", "Confirmar", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                cliEmpBL.clientesEmpleados_SupEmpleado(EmpCliCod); //eliminar desde DL
                Page.ClientScript.RegisterStartupScript(this.GetType(), "AlertScript", "alert('Eliminado!');", true);
            }
            else if (dialogResult == DialogResult.No)
            { }
    
            gvw_CliEmp_EmpData.DataSource = null;
            gvw_CliEmp_EmpData.DataBind();
            gvw_CliEmp_EmpData.DataSource = cliEmpBL.clientes_Empleados_cons_EmpxCliente(lbl_CliEmp_CliCod.Text);
            gvw_CliEmp_EmpData.DataBind();
        }
    
    
    }
    
    protected void gvw_CliEmp_EmpData_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {//this is because grid fired event RowDeleting which wasn't handled
    }
    #endregion
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 2014-07-06
      • 2012-09-15
      • 2016-08-09
      • 2013-03-02
      相关资源
      最近更新 更多