如何设置页脚表文本框的比较有效性?

时间:2016-03-24 04:48:09

标签: c# asp.net gridview

我有一个带有两个页脚文本boxex的网格视图,

<asp:GridView ID="grdmaster" runat="server" AutoGenerateColumns="false" ShowFooter="true" DataKeyNames="ID">
<Columns>
<asp:TemplateField HeaderText="Description"> 
<ItemTemplate>
<asp:TextBox ID="txtdescription" runat="server" > </asp:TextBox>      
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lbltotal"  Font-Bold="true" runat="server" Text="Total" >            </asp:Label>
</FooterTemplate>                                                                                                                 
</asp:TemplateField>  
<asp:TemplateField HeaderText="Debit"> 
 <ItemTemplate>
<asp:TextBox ID="txtdebit" runat="server" AutoPostBack="true" OnTextChanged="txtdebit_TextChanged"> </asp:TextBox>      
</ItemTemplate> 
<FooterTemplate>
<asp:TextBox ID="txtdebit1"  Font-Bold="true" runat="server" ></asp:TextBox>
</FooterTemplate>                                                                                                               
</asp:TemplateField>  
<asp:TemplateField HeaderText="Credit"> 
<ItemTemplate>
<asp:TextBox ID="txtcredit" runat="server" AutoPostBack="true" OnTextChanged="txtcredit_TextChanged"> </asp:TextBox>    
</ItemTemplate> 
<FooterTemplate>
<asp:TextBox ID="txtcredit2"  Font-Bold="true" runat="server"></asp:Text>
</FooterTemplate> 
</asp:TemplateField>
<asp:TemplateField> 
<ItemTemplate>

<asp:LinkButton ID="btndelete" runat="server" class="btn red icn-only" OnClick="btndelete_Click"><i class="icon-remove icon-white"></i>     </asp:LinkButton>

</ItemTemplate> 
</asp:TemplateField>
</Columns>
</asp:GridView>

这里我要比较页脚Textebox txtdebit1txtcredit2值是否相同。我如何设置比较验证器。我按照google的一些方法但得到了比较验证器可能出现的错误消息找不到控制来验证文本框。是否可以为页脚表文本框设置比较验证器?

1 个答案:

答案 0 :(得分:1)

请在下面试试,

protected void grdmaster_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridFooterItem)
    {
        GridDataItem item = (GridDataItem)e.Item;

        TextBox txtdebit1 = item.FindControl("txtdebit1") as TextBox;
        TextBox txtcredit2 = item.FindControl("txtcredit2") as TextBox;

        TableCell cell = (TableCell)txtdebit1.Parent;

        CompareValidator val = new CompareValidator();
        val.ControlToCompare = txtcredit2.ID;
        val.ControlToValidate = txtdebit1.ID;
        val.Operator = ValidationCompareOperator.LessThan;
        val.Display = ValidatorDisplay.Dynamic;
        val.ErrorMessage = "Error message";
        cell.Controls.Add(val); 

    }
}