我正在尝试从复选框选择的网格中删除记录。但它并没有删除记录。哪里出错了有人请帮我找出来。
这是我的代码 -
的Default.aspx -
<asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" BackColor="#999966" />
<asp:Button ID="btnDelete" runat="server" Text="Delete" onclick="btnDelete_Click" BackColor="#999966" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Name"
DataSourceID="SqlDataSource1"
style="padding: 0px; margin: 0px; border: thin solid #FF9933; width: 500px;"
CellPadding="0" EmptyDataText="No records found" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle BackColor="Moccasin" Width="500px" Font-Bold="True"
ForeColor="Black" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbSelectAll" runat="server" onclick="javascript:SelectAllCheckboxes1(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
</Columns>
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:HRMSConnectionString %>"
SelectCommand="SELECT [Name] FROM [Languages]">
</asp:SqlDataSource>
Default.aspx.cs -
string strcon = ConfigurationManager.ConnectionStrings["Dbconnection"].ConnectionString;
SqlCommand command;
protected void Page_Load(object sender, EventArgs e)
{
tblAdd.Visible = false;
Label1.Visible = false;
GridView1.DataBind();
if (!Page.IsPostBack)
{
fillLanguageGrid();
}
}
public void fillLanguageGrid()
{
GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataBind();
GridView1.Visible = true;
}
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox chkdelete = (CheckBox)gvrow.FindControl("chk");
if (chkdelete.Checked)
{
string name= Convert.ToString(GridView1.DataKeys[gvrow.RowIndex].Values["Name"].ToString());
deleteRecordByName(name);
}
}
fillLanguageGrid();
}
public void deleteRecordByName(string Name)
{
using (SqlConnection sqlConnection = new SqlConnection(strcon))
using (SqlCommand cmd = new SqlCommand("[dbo].[hrm_Langauges]", sqlConnection))
{
// define this to be a stored procedure
cmd.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@status", SqlDbType.VarChar, 50));
// define the parameter and set its value
cmd.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar)).Value = Name;
//open connection, execute DELETE query, close connection
sqlConnection.Open();
command.Parameters["@status"].Value = "Delete";
cmd.ExecuteNonQuery();
sqlConnection.Dispose();
}
这是我的存储过程
ALTER PROCEDURE [dbo].[hrm_Langauges]
(
@Name varchar(120),
@CreatedOn datetime,
@UpdatedOn datetime=0,
@CreatedBy bigint=0,
@UpdatedBy bigint=0,
@IsDeleted bit=0,
@status as varchar(50) = ''
)
AS
BEGIN
BEGIN TRANSACTION
DECLARE @ID int;
SELECT @ID = coalesce((select max(ID) + 1 from Languages), 1)
COMMIT
if(@status = 'Display')
BEGIN
SELECT * FROM [dbo].[Languages];
END
else if(@status = 'Add')
BEGIN
IF EXISTS(SELECT Name FROM [dbo].[Languages] WHERE Name = @Name)
Begin
Return 0
End
Else
INSERT INTO [dbo].[Languages](Name, CreatedOn,UpdatedOn,CreatedBy,UpdatedBy,IsDeleted) VALUES(@Name,@CreatedOn,@UpdatedOn,@CreatedBy,@UpdatedBy,@IsDeleted)
END
else if(@status = 'Update')
BEGIN
UPDATE [dbo].[Languages] Set Name=@Name,UpdatedOn=@UpdatedOn WHERE ID=@ID
END
else if(@status = 'Delete')
BEGIN
DELETE FROM [dbo].[Languages] WHERE NAME=@NAME
END
END
这是网格的快照 -
答案 0 :(得分:1)
在ASP.NET中,当您在服务器端控件上引发事件时,Page_Load事件始终在控件事件中的代码之前执行。
在您的情况下,您的用户选中复选框,然后按“删除”按钮。这会引发Page_Load事件,然后是btnDelete_Click事件。 Page_Load事件重新加载复选框,有效地破坏用户选中的复选框选择,然后调用按钮事件代码。但此时所选复选框未经检查,因此您的代码运行正常,但不会更改任何内容。
更改Page_Load事件添加