Question GridView EditItemTemplate RequiredFieldValidator Problem?

raysefo

Well-known member
Joined
Jul 21, 2010
Messages
207
Programming Experience
Beginner
Hi,



I have a gridview like below. The problem is when I changed the value in the TextBox3 and press enter, nothings happen but if I m editing lets say row number 4, editiable row changes to row number 1. But when I remove Required Field Validator, TextBox3_Changed event works perfect! How can I fix it?



Thanks in advance.



Best Regards.

...

<asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Quantity") %>' OnTextChanged="TextBox3_TextChanged"
AutoPostBack="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox3"
Display="Dynamic" ErrorMessage="You must enter Quantity">
</asp:RequiredFieldValidator>
<asp:ValidatorCalloutExtender runat="Server" ID="ValidatorCalloutExtender1" TargetControlID="RequiredFieldValidator1"
Width="200px" HighlightCssClass="highlight">
</asp:ValidatorCalloutExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

And the vb code for TextBox3_TextChanged is:
Protected Sub TextBox3_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim tb As TextBox = CType(sender, TextBox)
Dim index As Integer = GridView1.EditIndex
Dim row As GridViewRow = GridView1.Rows(index)

Dim val1 As Integer = Nothing
If Integer.TryParse(tb.Text, val1) Then
Dim val2 As Integer = Nothing
Dim ddl As DropDownList = CType(row.Cells(5).FindControl("DropDownList2"), DropDownList)
Dim lbl As Label = CType(row.FindControl("Label5"), Label)
Dim temp As Integer = Integer.Parse(ddl.SelectedItem.Text)
If Integer.TryParse(temp, val2) Then
row.Cells(7).Text = (val1 * val2)
lbl.Text = val1 * val2
Session("Total") = lbl.Text
Else
' The input in cell 7 is not a number, do something to correct that
End If
Else
' The input in TextBox is not a number, do something to correct that
End If
End Sub
 
Back
Top