简体   繁体   English

C#Visual Studio:如何在不激活其他字段验证器的情况下更新GridView?

[英]C# Visual Studio: How do I update GridView without activating validators for other fields?

http://puu.sh/oeilN/bf8d288fee.png http://puu.sh/oeilN/bf8d288fee.png

Hey guys, I have a GridView and I'm trying to use an event (GridView1_RowUpdated) to tell the user that the update was successful but every time i try to update the row the field validators of the other text input fields are activated. 大家好,我有一个GridView,并且尝试使用事件(GridView1_RowUpdated)告诉用户更新成功,但是每次我尝试更新该行时,都会激活其他文本输入字段的字段验证器。 What I want to know is how can I stop that? 我想知道的是我该如何阻止呢?

The C# file: C#文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ProductsAdmin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblUpdate.Text = "";
        lblErrorMessage.Text = "";
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        lblErrorMessage.Text = "";
        SqlDataSource1.InsertParameters["ProductCode"].DefaultValue = txtProdNum.Text;
        SqlDataSource1.InsertParameters["Name"].DefaultValue = txtName.Text;
        SqlDataSource1.InsertParameters["Version"].DefaultValue = txtVersion.Text;
        SqlDataSource1.InsertParameters["ReleaseDate"].DefaultValue = txtReleaseDate.Text;

        try
        {
            SqlDataSource1.Insert();
        }
        catch (Exception ex)
        {
            lblErrorMessage.Text = " A database connection error has occured: " + ex;
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        lblUpdate.Text = "";
        if (e.Exception != null) 
        {
            lblUpdate.Text = "A database error has occured: " + e;
        }
        else if (e.AffectedRows == 0)
        {
            lblUpdate.Text = "Another user may have updated this row already." + "<br/> Please try again.";
        }
        else
        {
            lblUpdate.Text = "Deletion succesful!";
        }

    }
    protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        lblUpdate.Text = "";
        if(e.Exception != null)
        {
            lblUpdate.Text = "A database error has occured: " + e;
            e.ExceptionHandled = true;
            e.KeepInEditMode = true;
        }
        else if (e.AffectedRows == 0)
        {
            lblUpdate.Text = "Another user may have updated this row already. <br/> Please try again";
        }
        else
        {
            lblUpdate.Text = "Edit succesful!";
        }
    }
}

The HTML: HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProductsAdmin.aspx.cs" Inherits="ProductsAdmin" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            width: 113px;
        }
        .auto-style3 {
            width: 143px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />

    </div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>" DeleteCommand="DELETE FROM [Products] WHERE [ProductCode] = @original_ProductCode AND [Name] = @original_Name AND [Version] = @original_Version AND [ReleaseDate] = @original_ReleaseDate" InsertCommand="INSERT INTO [Products] ([ProductCode], [Name], [Version], [ReleaseDate]) VALUES (@ProductCode, @Name, @Version, @ReleaseDate)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [Products]" UpdateCommand="UPDATE [Products] SET [Name] = @Name, [Version] = @Version, [ReleaseDate] = @ReleaseDate WHERE [ProductCode] = @original_ProductCode AND [Name] = @original_Name AND [Version] = @original_Version AND [ReleaseDate] = @original_ReleaseDate">
            <DeleteParameters>
                <asp:Parameter Name="original_ProductCode" Type="String" />
                <asp:Parameter Name="original_Name" Type="String" />
                <asp:Parameter Name="original_Version" Type="Decimal" />
                <asp:Parameter Name="original_ReleaseDate" Type="DateTime" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="ProductCode" Type="String" />
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Version" Type="Decimal" />
                <asp:Parameter Name="ReleaseDate" Type="DateTime" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Version" Type="Decimal" />
                <asp:Parameter Name="ReleaseDate" Type="DateTime" />
                <asp:Parameter Name="original_ProductCode" Type="String" />
                <asp:Parameter Name="original_Name" Type="String" />
                <asp:Parameter Name="original_Version" Type="Decimal" />
                <asp:Parameter Name="original_ReleaseDate" Type="DateTime" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ProductCode" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Horizontal" OnRowDeleted="GridView1_RowDeleted" OnRowUpdated="GridView1_RowUpdated" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:BoundField DataField="ProductCode" HeaderText="ProductCode" ReadOnly="True" SortExpression="ProductCode" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Version" HeaderText="Version" SortExpression="Version" />
                <asp:BoundField DataField="ReleaseDate" HeaderText="ReleaseDate" SortExpression="ReleaseDate" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            </Columns>
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F7F7F7" />
            <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
            <SortedDescendingCellStyle BackColor="#E5E5E5" />
            <SortedDescendingHeaderStyle BackColor="#242121" />
        </asp:GridView>
        <br />
        <asp:Label ID="lblUpdate" runat="server"></asp:Label>
        <br />
        <br />
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="lblProdNum" runat="server" Text="Product Code:"></asp:Label>
                </td>
                <td class="auto-style3">
                    <asp:TextBox ID="txtProdNum" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtProdNum" Display="Dynamic" ErrorMessage="**Product code is a required field." ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
                </td>
                <td class="auto-style3">
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName" Display="Dynamic" ErrorMessage="**Name is a required field." ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="lblVersion" runat="server" Text="Version:"></asp:Label>
                </td>
                <td class="auto-style3">
                    <asp:TextBox ID="txtVersion" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtVersion" Display="Dynamic" ErrorMessage="**Version is a required field." ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="lblReleaseDate" runat="server" Text="Release Date:"></asp:Label>
                </td>
                <td class="auto-style3">
                    <asp:TextBox ID="txtReleaseDate" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtReleaseDate" Display="Dynamic" ErrorMessage="**Release date is a required field." ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="Add Product" />
                </td>
                <td class="auto-style3">
                    <asp:Label ID="lblErrorMessage" runat="server"></asp:Label>
                </td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    </form>
</body>
</html>

使用CausesValidation阻止其运行验证器:

<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CausesValidation="False" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Visual Studio 中的 GridView 更新 Access 数据库? - How do I update an an Access database from a GridView in Visual Studio? 如何在Visual Studio 2008中使用C#为其他窗口创建工作区窗口? - How do I create a workspace window for other windows using c# in visual studio 2008? 如何在不激活C#中包含计时器的表单的情况下执行计时器控件的代码? - How do I execute the code for a timer control without activating the form that contains the timer in C#? C#Visual Studio:如何使用LINQ使用DataGridView从Edit更新数据库? - C# Visual Studio: How do I update a DB from Edit with DataGridView using LINQ? 我如何在 Visual Studio C# 中的 TextBox 中获取值,而不删除 TextBox 上已有的信息 - How do i get values into a TextBox in Visual studio C# without the information already on the TextBox gets erased 如何在没有Visual Studio的情况下重建C#ANTLR解析器? - How do I rebuild a C# ANTLR parser without Visual Studio? 如何使用C#中的其他字段从gridview编辑图像? - How to edit image from gridview with some other fields in c#? 在Visual Studio C#项目中,文件是否无需放置using语句就能识别其他文件内容? - In Visual Studio C# project do files aware the other file content without putting a using statement? 如何在Visual Studio C#上制作输出标签 - How do I make a Output Label on Visual Studio C# 如何在 Visual Studio 的 C# 中创建服务引用? - How do I create a Service Reference in C# in Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM