使用CustomValidator比较Griview中的两个值 [英] Comparing two Values in Griview Using CustomValidator
问题描述
Hello Guys,
- GridView页脚中有2列(即txtAQty(可用数量)和txtQtyOff(需要的数量))和2个不同的文本框行。
-当我使用Footer Row插入数据时,它将数据保存在DataRow中。
- 我想比较两个文本框的值(即txtAQty(可用数量)和txtQtyOff(需要的数量))
-I实现了无法正常工作的CustomValidator。
- 当我插入一条记录时,它应该显示错误信息。
- 但它也插入记录并显示错误信息..
-请帮助..
先谢谢。
守则背后:
****************
< pre lang =c#> protected void CustomValidator1_ServerValidate( object sender,ServerValidateEventArgs args)
{
CustomValidator cv =(CustomValidator)sender;
GridViewRow grv1 =(GridViewRow)cv.NamingContainer;
// dim gvr As GridViewRow = cv.NamingContainer
decimal txtofstock = 0 ;
decimal txtastock = 0 ;
decimal .TryParse(((TextBox)GridView1.FooterRow.FindControl( txtQtyOff))。文本, out txtofstock);
decimal .TryParse(((TextBox)GridView1.FooterRow.FindControl( txtAQty))。文本, out txtastock);
如果(txtofstock > txtastock)
{
Response.Write( 错误);
// lblError.Text =错误;
}
}
ASPX页面:
****** ****
< 列 >
< asp:TemplateField HeaderText = < span class =code-keyword> AvailableQuantity HeaderStyle-ForeColor = Maroon HeaderStyle-BackColor = LightGray >
< ItemTemplate >
< asp:文本框 ID = txtAQty runat = server < span class =code-attribute>文字 =' <% #Eval( AQty)%> ' BorderWidth = 0 宽度 = 100% > < / asp:Textbox >
< / ItemTemplate >
< FooterTemplate >
< asp:TextBox ID = txtAQty runat = server AutoPostBack = true > < / asp:TextBox >
< / FooterTemplate >
< / asp:TemplateField >
< /列 >
< 列 >
< asp:TemplateField HeaderText = QuantityOffered HeaderStyle -ForeColor = 栗色 HeaderStyle-BackColor = LightGray >
< ItemTemplate >
< asp:TextBox ID = txtQtyOff runat = server 文字 =' <% #Eval( QtyOff)%> ' BorderWidth = 0 宽度 = 100% > < / asp:TextBox >
< / ItemTemplate >
< FooterTemplate >
< asp:TextBox ID = txtQtyOff runat = server AutoPostBack = true < span class =code-attribute> > < / asp:TextBox >
< asp:CustomValidator ID = CmpVal 文字 = * OnServerValidate = CustomValidator1_ServerValidate runat = server ValidateEmptyText = true ControlToValidate = txtAQty > < / asp:CustomValidator >
< / FooterTemplate >
< / asp:TemplateField >
< / Columns >
有两件事。
- 首先在CustomValidator1_ServerValidate方法中,如果验证失败,则设置args.IsValid = false;。这表明验证是否失败。您只是显示错误但没有使页面状态无效。
- 其次,添加额外支票Page.IsValid在DB中插入记录之前。它会检查页面上的所有验证是否都通过。
Hello Guys,
-There is 2 columns(i.e txtAQty(Available Quantity) and txtQtyOff(Quantity Needed)) and 2 different text boxes in a GridView Footer Row.
-When I insert the data using Footer Row it save the data in DataRow.
-Now I want to compare values of the two text boxes(i.e txtAQty(Available Quantity) and txtQtyOff(Quantity Needed))
-I implement CustomValidator which is not working properly.
-When I insert a record it is supposed to show the error message.
-But it is inserting the record as well and displaying the error message also..
-Please Help..
Thanks in Advance.
Behind The Code:
****************
protected void CustomValidator1_ServerValidate(object sender, ServerValidateEventArgs args)
{
CustomValidator cv = (CustomValidator)sender;
GridViewRow grv1 = (GridViewRow)cv.NamingContainer;
//dim gvr As GridViewRow = cv.NamingContainer
decimal txtofstock = 0;
decimal txtastock = 0;
decimal.TryParse(((TextBox)GridView1.FooterRow.FindControl("txtQtyOff")).Text, out txtofstock);
decimal.TryParse(((TextBox)GridView1.FooterRow.FindControl("txtAQty")).Text, out txtastock);
if (txtofstock > txtastock)
{
Response.Write("Error");
//lblError.Text = "Error ";
}
}
ASPX Page:
**********
<Columns>
<asp:TemplateField HeaderText="AvailableQuantity" HeaderStyle-ForeColor="Maroon" HeaderStyle-BackColor="LightGray">
<ItemTemplate>
<asp:Textbox ID = "txtAQty" runat ="server" Text='<%# Eval("AQty") %>' BorderWidth="0" Width="100%" ></asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID ="txtAQty" runat="server" AutoPostBack="true" ></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="QuantityOffered" HeaderStyle-ForeColor="Maroon" HeaderStyle-BackColor="LightGray" >
<ItemTemplate>
<asp:TextBox ID="txtQtyOff" runat="server" Text='<%# Eval("QtyOff") %>' BorderWidth="0" Width="100%"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtQtyOff" runat="server" AutoPostBack="true" ></asp:TextBox>
<asp:CustomValidator ID="CmpVal" Text = "*" OnServerValidate="CustomValidator1_ServerValidate" runat="server" ValidateEmptyText="true" ControlToValidate="txtAQty" ></asp:CustomValidator>
</FooterTemplate>
</asp:TemplateField>
</Columns>
There are two things.
- First in CustomValidator1_ServerValidate method, set theargs.IsValid = false;if validation fails. This indicates whether the validation is failed or not. You are just showing error but not making page status invalid.
- Second, add additional check forPage.IsValidbefore inserting record in DB. It checks whether all the validation on the page is passed or not.
这篇关于使用CustomValidator比较Griview中的两个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!