我在检索删除记录的值时遇到问题。 我在带有templatefields的gridview中使用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;
但同样的结果,任何想法? 拜托,我希望有人能帮助我。
最好的问候
答案 0 :(得分: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
}
}
答案 1 :(得分:0)
感谢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