dblwizard dblwizard - 1 month ago 13
ASP.NET (C#) Question

GridView w/ model binding UpdateMethod not getting called

I have a basic Web Forms project using Entity Frameworks & SQL Server for data. I'm trying to connect a GridView to a table and for some reason the UpdateMethod does not get called. It does a postback when the "Update" link is clicked but it never gets to the gvNames_UpdateItem. Any help would be greatly appreciated.

<asp:GridView ID="gvNames" runat="server" SelectMethod="gvNames_GetData" UpdateMethod="gvNames_UpdateItem"
DeleteMethod="gvNames_DeleteItem" CssClass="table table-hover table-striped"
UseAccessibleHeader="true" ItemType="CountyPMFraudData.NotifyName" AutoGenerateColumns="false" DataKeyNames="NotifyNamesID"
ShowHeaderWhenEmpty="true" OnPreRender="gvNames_PreRender" >
<Columns>
<asp:DynamicField DataField="NotifyNamesID" Visible="false" />
<asp:DynamicField DataField="UserID" Visible="false" />
<asp:DynamicField DataField="CorpFlag" HeaderText="Business" />
<asp:DynamicField DataField="FirstName" />
<asp:DynamicField DataField="LastName" HeaderText="Last Name/Business Name" />
<asp:DynamicField DataField="Active" HeaderText="Active" />
<asp:CommandField ShowEditButton="true" ShowCancelButton="true" ShowDeleteButton="true"/>
</Columns>
</asp:GridView>


And this is the code behind:

public void gvNames_DeleteItem(int notifyNamesID)
{
using (var dbContext = new CountyPMFraudEntities())
{
var item = new NotifyName { NotifyNamesID = notifyNamesID };
dbContext.Entry(item).State = EntityState.Deleted;

try
{
dbContext.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
ValidationError.Display(String.Format("Item with id {0} no longer exists in the database.", notifyNamesID));
}
}
}

public IEnumerable gvNames_GetData()
{
List<NotifyName> names = null;

if (HttpContext.Current.User.Identity.IsAuthenticated)
{

using (var dbContext = new CountyPMFraudEntities())
{
names = dbContext.NotifyNames.Where(NotifyName => NotifyName.UserID == _userID).ToList();
}
}

return names;
}

public void gvNames_UpdateItem(int notifyNamesID)
{

using (var dbContext = new CountyPMFraudEntities())
{
var name = dbContext.NotifyNames.Find(notifyNamesID);

if (name == null)
{
ValidationError.Display(String.Format("Item with id {0} was not found", notifyNamesID));
return;
}

TryUpdateModel(name);

if (ModelState.IsValid)
{
dbContext.SaveChanges();
}

}
}

protected void gvNames_PreRender(object sender, EventArgs e)
{
try
{
gvNames.HeaderRow.TableSection = TableRowSection.TableHeader;
}
catch (Exception)
{
//Just ignore becuase there is no data.
}

}

Answer

Ok, I figured this out. Two things were going on. First I missed adding a "ValidationSummary" and enabling ShowModelStateErrors like this:

<asp:ValidationSummary ID="vsHuman" ShowModelStateErrors="true" ValidationGroup="vgHuman" runat="server" ForeColor="Red" />

Once I did that then I realized I was getting an error "The NotifyNamesID field is required.". With this I discovered for some reason the hidden "DynamicField" did not contain the data when going back to the server, so I had to change it to a TemplateField like this:

<asp:TemplateField Visible="false">
    <ItemTemplate>
        <asp:HiddenField runat="server" Value='<%#Eval("NotifyNamesID") %>' />
    </ItemTemplate>
</asp:TemplateField>

Once I did this things began to work. Hope this helps somebody in the future.