ImageButton验证组无法正常工作

时间:2013-03-06 09:09:57

标签: asp.net

我有一个Gridview和gridview内部我有一个页脚模板..页脚模板包含 一个带有两个验证控件的TextBox,即RequiredField和RangeValidator,还有一个ImageButton为“Add”行。当用户按下图像按钮时,它会捕获错误但是一旦提供范围内的值并按“添加”没有任何反应意味着提交不会发生。当我删除rangevalidator然后它按预期工作。我想问题是,一个名为“autonumeric”的插件与仅接受数字格式的字段绑定,而textbox是字符串格式,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>

1 个答案:

答案 0 :(得分: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