Donate

DataKey Value Is Null In ItemDeleting Event Of ASP.NET Web Forms FormView Control

Hello,
Normally, you can access the DataKey object value of a FormView control to it's wired events. Since I'm using Entity Framework as Datasource of a FormView control instead of DataSource control wizards, the DataKey object value of the FormView control returns null instead of an ID in the ItemDeleting event of that object.
DataKey Value Is Null In ItemDeleting Event Of ASP.NET FormView Control
After putting several breakpoints to it's events, I came up with a fix that is to query the DB again and then bind it's result to the DataSource property of the FormView Control. That is call BindFormView() method when CommandName is equal to "Delete" in the ItemCommand event of the control.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
protected void FormViewBookDetails_ItemCommand(object sender,
 FormViewCommandEventArgs e)
{
    if (e.CommandName == "Cancel")
    {
        FormViewBookDetails.ChangeMode(FormViewMode.ReadOnly);
    }
    else if (e.CommandName == "Edit")
    {
        FormViewBookDetails.ChangeMode(FormViewMode.Edit);
    }
    else if (e.CommandName == "New")
    {
        FormViewBookDetails.ChangeMode(FormViewMode.Insert);
    }
    else if (e.CommandName == "Delete")
    {
        FormViewBookDetails.ChangeMode(FormViewMode.ReadOnly);
        BindFormView();
    }
}
 
private void BindFormView()
{
    var bookRecords = _context.BookDetails.ToList();
    FormViewBookDetails.DataSource = bookRecords;
    FormViewBookDetails.DataBind();
}

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Bootstrap Modal In ASP.NET MVC With CRUD Operations