2017-06-14 61 views
0

我在检索删除记录的值时遇到问题。 我使用OnRowDeleting与模板列一个gridview,我将无法获取用于删除记录选定值,这是我的网格:无法检索GridView中rowdeleting的值

<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; 

但相同的结果,任何想法?请致电 ,我希望任何人都可以帮助我。

问候

+0

'MessageBox'不是asp.net功能。这是为winforms。不要在Web环境中使用'System.Windows.Form'。 – VDWWD

回答

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 
    } 
} 
0

感谢帕特里克您的解决方案帮助了我。

<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 
+0

欢迎:) –