pascalmartin
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.FindCon trol("lblB ar"), Label).Text.ToString.ToUpp er = CType(GridOrderDet.FindCon trol("txbB ar"), TextBox).Text.ToString.ToU pper Then
Do something…
Else
Do something…
End If
End Sub
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
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Sub ValBarcode()
If CType(GridOrderDet.FindCon
Do something…
Else
Do something…
End If
End Sub
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
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" AutoPostback ="true" Text="" OnTextChanged="txbBar_TextChanged" MaxLength="13" />
</ItemTemplate>
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
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>
ASKER
Hi Dejjabu,
Tried to implement your solution but here is what I get:
Error: Sys.WebForms.PageRequestMa nagerServe rErrorExce ption: Unable to cast object of type 'System.Web.UI.WebControls .GridView' to type 'System.Web.UI.WebControls .GridViewR ow'.
I also forget to inform that my gridview is inside an update panel (ajax).
rgds
Tried to implement your solution but here is what I get:
Error: Sys.WebForms.PageRequestMa
I also forget to inform that my gridview is inside an update panel (ajax).
rgds
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Dejaanbu,
Great Job!
It works like a charm.
Cheers.
Great Job!
It works like a charm.
Cheers.
hope this helps.