0

我有一个Gridview,在gridview 里面我有一个页脚模板。页脚模板包含一个带有两个验证控件的TextBox,即RequiredField 和RangeValidator,还有一个ImageButton 用于“添加”行。当用户按下图像按钮时,它会捕获错误,但一旦在范围内提供值并按下“添加”,什么都不会发生,我的意思是提交不会发生。当我删除 rangevalidator 时,它会按预期工作。我想问题是一个名为“autonumeric”的插件与只接受数字格式的字段绑定,文本框是字符串格式,rangevalidator 类型是整数..是什么导致了问题吗?我如何解决它。

这是FooterTemplate的代码

<FooterTemplate>
   <asp:ImageButton ID="imgbtnAdd" runat="server" CommandName="Insert" CausesValidation="true" ImageUrl="/Style%20Library/Images/add.png" ToolTip="New Record" ValidationGroup="GridViewFooterRowGroup"/>
</FooterTemplate>

这是项目模板的代码

<asp:TemplateField>
 <FooterTemplate>
    <asp:TextBox ID="txtftrQty" runat="server" Text="0" />
    <asp:RequiredFieldValidator ID="rfvQty" runat="server" ControlToValidate="txtftrQty" Text="*" Display="Dynamic" CausesValidation="True" ValidationGroup="GridViewFooterRowGroup" > </asp:RequiredFieldValidator>
    <asp:RangeValidator ID="RvFtrQty" runat="server" ErrorMessage="*" Type="Integer" MinimumValue="1" MaximumValue="999999" ControlToValidate="txtftrQty" Display="Dynamic" ForeColor="Red" CausesValidation="True" ValidationGroup="GridViewFooterRowGroup" ></asp:RangeValidator>
  </FooterTemplate>

</asp:TemplateField>
4

1 回答 1

0

Subjet:带有购物车篮逻辑的 GridView

你好,

试试下面的代码。我已将不同的 ValidationGroup 附加到 GridView 的 RowDataBound 事件中的每个控件。

ASPX 代码

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

    <h2>GridView with Shopping Cart Basket logic</h2>

    <asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="False" 
        onrowdatabound="gvProducts_RowDataBound">
        <Columns>
            <asp:TemplateField HeaderText="SN">
                <ItemTemplate>
                    <asp:Label ID="lblSN" runat="server" Text='<%# (Container.DataItemIndex + 1) %>'></asp:Label> 
                </ItemTemplate> 
            </asp:TemplateField>
            <asp:BoundField DataField="ProductName" HeaderText="Product Name" />
            <asp:BoundField DataField="ProductPrice" DataFormatString="{0:C2}" 
                HeaderText="Product Price" />
            <asp:TemplateField HeaderText="Product Quantity">
                <ItemTemplate>
                    <asp:TextBox ID="tbxProductQuantity" runat="server" MaxLength="10">
                    </asp:TextBox> 
                    <asp:RequiredFieldValidator ID="rfvtbxProductQuantity" runat="server" ErrorMessage="Product Quantity"
                         ControlToValidate="tbxProductQuantity" Display="Dynamic" SetFocusOnError="true" 
                         Text="*">
                    </asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="cvtbxProductQuantity" runat="server" ErrorMessage="Only numbers (integer) are allowed in 'Product Quantity' field."
                         ControlToValidate="tbxProductQuantity" Display="Dynamic" Operator="DataTypeCheck" SetFocusOnError="true" 
                         Text="*" Type="Integer">
                    </asp:CompareValidator>
                </ItemTemplate> 
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Add To Basket">
                <ItemTemplate>
                    <asp:Button ID="btnAddToBasket" runat="server" Text="Add To Basket"/>
                </ItemTemplate> 
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    </form>
</body>
</html>

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 Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            PopulategvProducts();
        }
    }

    private void PopulategvProducts()
    {
        List<Product> products = new List<Product>();
        for (int i = 1; i <= 20; i++)
        {
            products.Add(new Product(i,"Product-" + i.ToString(),(i * 3)));  
        }

        gvProducts.DataSource = products;
        gvProducts.DataBind();  
    }

    protected void gvProducts_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox tbx = (TextBox)e.Row.FindControl("tbxProductQuantity");
            RequiredFieldValidator rfv = (RequiredFieldValidator)e.Row.FindControl("rfvtbxProductQuantity");
            CompareValidator cv = (CompareValidator)e.Row.FindControl("cvtbxProductQuantity");
            Button btn = (Button)e.Row.FindControl("btnAddToBasket");

            string validationGroupText = "AddProductToBasket" + (e.Row.DataItemIndex + 1).ToString();

            tbx.ValidationGroup = validationGroupText;
            rfv.ValidationGroup = validationGroupText;
            cv.ValidationGroup = validationGroupText;
            btn.ValidationGroup = validationGroupText; 
        }
    }
}

public class Product
{
    private int _ProductID;
    public int ProductID
    {
        get { return _ProductID; }
        set{_ProductID = value;}
    }

    private string _ProductName;
    public string ProductName
    {
        get { return _ProductName; }
        set { _ProductName = value; }
    }

    private double _ProductPrice;
    public double ProductPrice
    {
        get { return _ProductPrice; }
        set { _ProductPrice = value; }
    }

    public Product(int productID,string productName,double productPrice)
    {
        _ProductID = productID;
        _ProductName = productName;
        _ProductPrice = productPrice; 
    }
}

或者

http://forums.asp.net/t/1275010.aspx/1

于 2013-03-06T09:36:43.803 回答