Solved

Compare Validate two values in Gridview

Posted on 2014-09-11
6
729 Views
Last Modified: 2014-09-14
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
0
Comment
Question by:pascalmartin
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:jitendra patil
ID: 40318704
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.
0
 

Author Comment

by:pascalmartin
ID: 40319674
There is no method for SelectedIndexChanged event in Texbox control??
0
 
LVL 14

Expert Comment

by:dejaanbu
ID: 40321411
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

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:pascalmartin
ID: 40321733
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
0
 
LVL 14

Accepted Solution

by:
dejaanbu earned 500 total points
ID: 40322417
try
Dim currentRowAs GridViewRow = TryCast(DirectCast(sender, TextBox).Parent.Parent, GridViewRow)

Open in new window

0
 

Author Comment

by:pascalmartin
ID: 40322436
Hi Dejaanbu,
Great Job!
It works like a charm.
Cheers.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now