0
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string VisitorManagementConnectionString = ConfigurationManager.ConnectionStrings["VisitorManagementConnectionString"].ConnectionString;
string strQuery = "select Id, ItemName, FoundAt, TimeIn, ImageName from LostFound order by ID";
SqlCommand cmd = new SqlCommand(strQuery);
SqlConnection con = new SqlConnection(VisitorManagementConnectionString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
ViewState["dt"] = dt;
BindGrid();
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
dt.Dispose();
}
}
protected void BindGrid()
{
GridView1.DataSource = ViewState["dt"] as DataTable;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void BtnLose_Click(object sender, EventArgs e)
{
Response.Redirect("SecurityLost.aspx");
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string ID = e.Row.Cells[0].Text;
foreach (Button button in e.Row.Cells[5].Controls.OfType<Button>())
{
if (button.CommandName == "Delete")
{
button.Attributes["onclick"] = "if(!confirm('Do you want to delete " + ID + "?')){ return false; };";
}
}
}
}
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
int ID = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["dt"] as DataTable;
dt.Rows[ID].Delete();
string VisitorManagementConnectionString = ConfigurationManager.ConnectionStrings["VisitorManagementConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(VisitorManagementConnectionString))
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM LostFound WHERE ID = @ID"))
{
cmd.Parameters.AddWithValue("@ID", ID);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
BindGrid();
}
을 할 수 없습니다.의 GridView는 삭제 값 asp.net
누군가 나를 안내 할 수 있습니까?
이 내 HTML 파일
.<asp:GridView ID="GridView1" runat="server" OnRowDataBound = "OnRowDataBound" AutoGenerateColumns = "false" OnRowDeleting="OnRowDeleting" Font-Names = "Arial" Caption = "Lose & Found" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <Columns> <asp:BoundField DataField = "ID" HeaderText = "ID" /> <asp:BoundField DataField = "ItemName" HeaderText = "Item Name" /> <asp:BoundField DataField = "FoundAt" HeaderText = "Place" /> <asp:BoundField DataField = "TimeIn" HeaderText = "Time Found" /> <asp:ImageField DataImageUrlField = "ID" DataImageUrlFormatString = "Image.aspx?ImageID={0}" ControlStyle-Width = "100" ControlStyle-Height = "100" HeaderText = "Preview Image"/> <asp:CommandField ShowDeleteButton="True" ButtonType="Button" /> </Columns> </asp:GridView>
1.Change
Page_Load()
이벤트 : 문제가 당신의 ID가 항상 0.To ID가 올바르게 두 가지 사항을 변경해야을 통해 오는 있는지 확인 있다는 것을 우리가 설립 한 의견에서 그래서
장소 OnRowDeleting의 중단 점 및 코드를 단계별로 실행합니다. 중단 점이 발생합니까? SQL이 손실되고 발견 된 테이블에서 항목이 삭제됩니까? –
@DenisWessels 값 ID가 0이고 삭제 대상이 null –
Aaaah 문제가 삭제 부분이 아니기 때문에 삭제할 올바른 ID를 가져 오지 못하고 있다는 것이 문제입니까? –