Solved

Compare Validate two values in Gridview

Posted on 2014-09-11
6
709 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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

17 Experts available now in Live!

Get 1:1 Help Now