Hello friends.. I have a doubt.In a page ,I have a gridview with 2 coloums.One boundcoloum(ID) and one Textbox template coloum(to enter amount at run time) and a save button. While entering amount in the Gridview Texbox i should provide a validation that no two textboxes should have the same Amount.The amount should not be repeted. Pls help me in getting a soloution for this..
Thanks
Madhu
Hi:
We can use CustomValidator control to do this. For example:
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="CustomValidator"
onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
<asp:GridView ID="GridView3" AutoGenerateColumns="false" runat="server" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" DataSourceID="SqlDataSource1">
<footerstyle backcolor="#F7DFB5" forecolor="#8C4510" />
<rowstyle backcolor="#FFF7E7" forecolor="#8C4510" />
<pagerstyle forecolor="#8C4510" horizontalalign="Center" />
<selectedrowstyle backcolor="#738A9C" font-bold="True" forecolor="White" />
<headerstyle backcolor="#A55129" font-bold="True" forecolor="White" />
<Columns><asp:TemplateField><ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField><ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Save" />
</ItemTemplate></asp:TemplateField></Columns>
</asp:GridView>
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
List<string> s = new List<string>();
foreach (GridViewRow gvr in this.GridView3.Rows) {
TextBox t = gvr.FindControl("TextBox1") as TextBox;
if (t != null&&t.Text.Trim()!="") {
if (s.Contains(t.Text))
{
args.IsValid = false;
return;
}
else
{
s.Add(t.Text);
}
}
}
args.IsValid = true;
}
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/validation/default.aspx#custom
Regards
This might be another way to check..
When you move from one row to other, selectedindex changed would fire.. in that you can loop through values in the source table. if there is a duplicate, you can show error message.
No comments:
Post a Comment