Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 894
  • Last Modified:

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
0
pascalmartin
Asked:
pascalmartin
  • 3
  • 2
1 Solution
 
Jitendra PatilSr.Software EngineerCommented:
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
 
pascalmartinAuthor Commented:
There is no method for SelectedIndexChanged event in Texbox control??
0
 
dejaanbuCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
pascalmartinAuthor Commented:
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
 
dejaanbuCommented:
try
Dim currentRowAs GridViewRow = TryCast(DirectCast(sender, TextBox).Parent.Parent, GridViewRow)

Open in new window

0
 
pascalmartinAuthor Commented:
Hi Dejaanbu,
Great Job!
It works like a charm.
Cheers.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now