在“编辑”模式下,无法更新Gridview中的字段,asp验证器会阻止更新

时间:2013-11-28 12:04:11

标签: c# asp.net validation gridview customvalidator

在我的最后一年项目中,我正在使用 GridView ,它使用 ObjectDataSource 来显示数据库数据。 我的 ObjectDataSource 与正确的“select”,“update”,“delete”函数相关联,这些函数属于N层应用程序。

我已经测试了我的所有功能,但效果很好。

但是当在GridView的“ Edit”模式下,我无法更新我的字段...但是我可以很好地删除行。

有什么建议吗?

谢谢!

更新:问题是我的asp客户端验证器阻止我以“编辑”模式提交更新的字段。 我已将它们添加到代码中。

部分代码:

<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>
&nbsp;&nbsp;<asp:CustomValidator ID="CustomValidator1" runat="server" 
    ControlToValidate="TextBoxBeerName" Display="Dynamic" 
    ErrorMessage="*" 
    onservervalidate="CustomValidator1_ServerValidate" SetFocusOnError="True" 
    ForeColor="Red"></asp:CustomValidator>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBoxBeerCountry" runat="server">Beer Country</asp:TextBox>
&nbsp;&nbsp;<asp:CustomValidator ID="CustomValidator2" runat="server" 
    ControlToValidate="TextBoxBeerCountry" Display="Dynamic" 
    ErrorMessage="*" 
    onservervalidate="CustomValidator2_ServerValidate" SetFocusOnError="True" 
    ForeColor="Red"></asp:CustomValidator>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:FileUpload ID="FileUpload1" runat="server" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" Text="Upload to DataBase" 
    onclick="Button1_Click" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:Label ID="uploadStatusLabel" runat="server" Enabled="false"></asp:Label>
</form>

部分代码背后:

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 ..... */    }
}

1 个答案:

答案 0 :(得分:0)

那个答案对我有帮助: Why asp.net validators prevent all other postback?

我做了一个 ValidationGroup

为每个 CustomValidator

添加了属性ValidationGroup="AddBeerValidationGroup"

部分修改后的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    Page.Validate("AddBeerValidationGroup");

    if (Page.IsValid == true)
    {  /* SOME CODE ..... */    }
}