【问题标题】:how to set compare validatior for footer table text boxes?如何为页脚表格文本框设置比较验证器?
【发布时间】:2016-03-24 13:12:30
【问题描述】:

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

<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 的值是否相同。如何设置比较验证器。我遵循了谷歌的一些方法,但收到了错误消息,例如比较验证器找不到控制验证文本框。是否可以为页脚表格文本框设置比较验证器?

【问题讨论】:

  • 尝试从后面的代码中为页脚项动态添加比较验证器。
  • @ Piyush Khatri,我不太了解从后面的代码中执行此操作。如果您不介意请解释一下我该如何执行它。
  • 你看过CompareValidator Control中的例子了吗?

标签: c# asp.net gridview


【解决方案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); 

    }
}

【讨论】:

  • ,我会努力回复你的
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多