Link to home
Start Free TrialLog in
Avatar of pascalmartin
pascalmartinFlag for Hong Kong

asked on

Compare Validate two values in Gridview

Hi,
I have a gridview with a barcode value in a label control, I want to insert the same barcode value in an empty textbox control and compare the two values. The OnTextChanged method of the textbox should fire a sub and take some action.

My question is how to code this fire this OnTextChanged methid from the gridview textbox? See my code below.

<asp:GridView ID="GridOrderDet" runat="server" DataKeyNames="PO" AutoGenerateColumns="false">
<Columns>        
<asp:BoundField DataField="PO" Visible="false" />

<asp:TemplateField HeaderText="Barcode">
<ItemTemplate><asp:Label ID="lblBar" runat="server" Text='<%# Eval("Barcode") %>' /></ItemTemplate>
</asp:TemplateField>


<asp:TemplateField>  
<ItemTemplate><asp:TextBox ID="txbBar" runat="server" Text="" OnTextChanged="ValBarcode" MaxLength="13" />
</ItemTemplate>
</asp:TemplateField>          
</Columns>  
</asp:GridView>  

Sub ValBarcode()
If CType(GridOrderDet.FindControl("lblBar"), Label).Text.ToString.ToUpper = CType(GridOrderDet.FindControl("txbBar"), TextBox).Text.ToString.ToUpper Then
  Do something…
Else
  Do something…
End If
End Sub
Avatar of jitendra patil
jitendra patil
Flag of India image

just set the AutoPostback property of the Textbox control to "True", so this will tell to the control to fire an autopostback event and search for the textbox event txbBar_SelectedIndexchanged, in this event you can write your code what you wrote in the sub function. or just call the function in this event.

hope this helps.
Avatar of pascalmartin

ASKER

There is no method for SelectedIndexChanged event in Texbox control??
There should be a Textchanged event for the textbox. set Autopostback to true and then code should be
<ItemTemplate><asp:TextBox ID="txbBar" runat="server" AutoPostback ="true" Text="" OnTextChanged="txbBar_TextChanged" MaxLength="13" />
</ItemTemplate>

Open in new window

 protected sub txbBar_TextChanged(object sender, EventArgs e)
Dim currentRow As GridViewRow = DirectCast(DirectCast(sender, TextBox).Parent.Parent.Parent.Parent, GridViewRow)
Dim txt As TextBox = DirectCast(currentRow.FindControl("txbBar"), TextBox)
Dim lbl As Label= DirectCast(currentRow.FindControl("lblBar"), Label)
If txt.Text.ToString.ToUpper = lbl.Text.ToString.ToUpper Then
  Do something…
Else
  Do something…
End If

End sub

Open in new window


BTW, you are doing this now in server code, if this is only for validation purpose, you can use compare validator for the same. something like below:
<ItemTemplate><asp:TextBox ID="txbBar" runat="server" Text=""  MaxLength="13" />
<asp:CompareValidator ID="cv" ValueToCompare='<%# Eval("Barcode") %>' ControlToValidate="txtBar" Display="Dynamic" ErrorMessage="the error" runat="server" Operator="Equal"></asp:CompareValidator>
</ItemTemplate>

Open in new window

Hi Dejjabu,

Tried to implement your solution but here is what I get:
Error: Sys.WebForms.PageRequestManagerServerErrorException: Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.GridViewRow'.

I also forget to inform that my gridview is inside an update panel (ajax).

rgds
ASKER CERTIFIED SOLUTION
Avatar of Deja Anbu
Deja Anbu
Flag of Oman image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Dejaanbu,
Great Job!
It works like a charm.
Cheers.