Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Compare Validate two values in Gridview

Posted on 2014-09-11
6
Medium Priority
?
870 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 13

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

704 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