Semil Sebastian
Semil Sebastian

Reputation: 519

how to set compare validatior for footer table text boxes?

I have a grid view with two footer text 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>

here I want to compare the footer Textebox txtdebit1 and txtcredit2 values are same or not.How can I set compare validator for.I followed some methods from google but got error message like Compare validator could not find control to validate text box.Is it possible to set compare validator for footer table text box?

Upvotes: 2

Views: 85

Answers (1)

INDIA IT TECH
INDIA IT TECH

Reputation: 1898

Please try below,

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); 

    }
}

Upvotes: 1

Related Questions