On my final year project I'm using a GridView that use an ObjectDataSource to show database data. My ObjectDataSource is linked with proper "select", "update", "delete" functions that are part of a N-Tiers application.
I've tested all my functions and they work well.
But when in "Edit" mode of the GridView I cannot update my fields... However I can delete rows well.
Any suggestions?
Thanks!
UPDATE: The problem is that my asp client-side validators are preventing me from submitting the updated field in "edit" mode. I've added them to the code.
Partial code:
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="BeersObjectDataSource" runat="server"
DeleteMethod="DeleteBeer_BLL"
SelectMethod="RetrieveBeers_BLL"
UpdateMethod="UpdateBeer_BLL"
TypeName="BLL" >
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="country" Type="String" />
<asp:Parameter Name="logoPath" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="BeersObjectDataSource" PageSize="7"
onrowdeleted="GridView1_RowDeleted" onrowupdated="GridView1_RowUpdated" DataKeyNames="id">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="ID"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="Beer Name" SortExpression="name" />
<asp:BoundField DataField="country" HeaderText="Beer Country"
SortExpression="country" />
<asp:BoundField DataField="logoPath" HeaderText="Image Path"
SortExpression="logoPath" ReadOnly="True" />
<asp:ImageField DataImageUrlField="logoPath" DataImageUrlFormatString="~/{0}"
HeaderText="Logo Preview" NullDisplayText="No image"
NullImageUrl="~/images/logobeers/no-photo.jpg"
SortExpression="logoPath" ReadOnly="True">
<ControlStyle Height="100px" Width="100px" />
</asp:ImageField>
</Columns>
</asp:GridView>
<br />
<br />
<asp:TextBox ID="TextBoxBeerName" runat="server">Beer Name</asp:TextBox>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBoxBeerName" Display="Dynamic"
ErrorMessage="*"
onservervalidate="CustomValidator1_ServerValidate" SetFocusOnError="True"
ForeColor="Red"></asp:CustomValidator>
<asp:TextBox ID="TextBoxBeerCountry" runat="server">Beer Country</asp:TextBox>
<asp:CustomValidator ID="CustomValidator2" runat="server"
ControlToValidate="TextBoxBeerCountry" Display="Dynamic"
ErrorMessage="*"
onservervalidate="CustomValidator2_ServerValidate" SetFocusOnError="True"
ForeColor="Red"></asp:CustomValidator>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload to DataBase"
onclick="Button1_Click" />
<asp:Label ID="uploadStatusLabel" runat="server" Enabled="false"></asp:Label>
</form>
Partial Code behind:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
GridView1.DataBind();
}
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
if (IsValid == true)
{ /* SOME CODE ..... */ }
}
That answer help me: Why asp.net validators prevent all other postback?
I've made a ValidationGroup:
Added the property ValidationGroup="AddBeerValidationGroup"
to each CustomValidator
Partial modified Code behind:
protected void Button1_Click(object sender, EventArgs e)
{
Page.Validate("AddBeerValidationGroup");
if (Page.IsValid == true)
{ /* SOME CODE ..... */ }
}