MikeMCSD
asked on
LinkButton in DataGrid checking a texbox for null
I have a LinkButton in a DataGrid, that when clicked,
I want to check to see if a textbox in the DataGrid is empty (empty string).
<asp:DataGrid id="grid" . . .
<asp:TemplateColumn><ItemT emplate>
<asp:TextBox ID="txtEng" Runat=server></asp:TextBox >
<asp:LinkButton CommandName="Add" Runat=server ID="lnkAdd">Add Engraving
</asp:LinkButton>
Private Sub grid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls. DataGridCo mmandEvent Args) Handles grid.ItemCommand
If e.CommandName = "AddEngrav" Then
Dim engrave As String = CType(e.Item.FindControl(" txtEng"), TextBox).Text
Try
ShoppingCart.UpdateCartEng (productId , engrave)
...............
I want to check to see if a textbox in the DataGrid is empty (empty string).
<asp:DataGrid id="grid" . . .
<asp:TemplateColumn><ItemT
<asp:TextBox ID="txtEng" Runat=server></asp:TextBox
<asp:LinkButton CommandName="Add" Runat=server ID="lnkAdd">Add Engraving
</asp:LinkButton>
Private Sub grid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.
If e.CommandName = "AddEngrav" Then
Dim engrave As String = CType(e.Item.FindControl("
Try
ShoppingCart.UpdateCartEng
...............
It is much better to have another class which receives the DataGridItem and provide strongly typed values
ASKER
oh . . . I forgot the most important thing!
I want to check to see if a textbox in the DataGrid is empty (empty string). . .
>>>Then I want a pop-up box to appear with a message.<<<
sorry about that.
I want to check to see if a textbox in the DataGrid is empty (empty string). . .
>>>Then I want a pop-up box to appear with a message.<<<
sorry about that.
Public Class ControlValues
Private ReadOnly txtEng As TextBox
Public Sub New(ByVal e As DataGridItem)
txtEng = CType(e.FindControl("txtEn g"),TextBo x)
End Sub
Public ReadOnly Property Engrave() As String
Get
Return GetValue(txtEng)
End Get
End Property
Private Function GetValue(ByVal ctl As TextBox) As String
Dim value As String = String.Empty
If Not ctl Is Nothing Then
value = ctl.Text.Trim()
End If
Return value
End Function
End Class
thus,
Private Sub grid_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles grid.ItemCommand
Dim controls As New ControlValues(e.Item)
Select Case e.CommandName
Case "AddEngrav"
If controls.Engrave.Length > 0 Then
...
End If
End Select
End Sub
Private ReadOnly txtEng As TextBox
Public Sub New(ByVal e As DataGridItem)
txtEng = CType(e.FindControl("txtEn
End Sub
Public ReadOnly Property Engrave() As String
Get
Return GetValue(txtEng)
End Get
End Property
Private Function GetValue(ByVal ctl As TextBox) As String
Dim value As String = String.Empty
If Not ctl Is Nothing Then
value = ctl.Text.Trim()
End If
Return value
End Function
End Class
thus,
Private Sub grid_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles grid.ItemCommand
Dim controls As New ControlValues(e.Item)
Select Case e.CommandName
Case "AddEngrav"
If controls.Engrave.Length > 0 Then
...
End If
End Select
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks b1, worked . . but spaces get through. Is there anywhere I can throw
a Trim in there?
Thanks for the info on Classes/Properties. I've tried in the past but just can't get
the concept of "Public ReadOnly Property" in code.
I have this in my code (from a book). I see what it does, but still don't understand
the concept.
Private Shared ReadOnly Property connectionString() As String
Get
Return ConfigurationSettings.AppS ettings("C onnectionS tring")
End Get
End Property
a Trim in there?
Thanks for the info on Classes/Properties. I've tried in the past but just can't get
the concept of "Public ReadOnly Property" in code.
I have this in my code (from a book). I see what it does, but still don't understand
the concept.
Private Shared ReadOnly Property connectionString() As String
Get
Return ConfigurationSettings.AppS
End Get
End Property
1. Dim engrave As String = DirectCast(e.Item.FindCont rol("txtEn g"), TextBox).Text.Trim() will cause the leading and trailing spaces to be removed. But it will not remove any spaces within the content.
2. To remove all spaces,
Dim engrave As String = DirectCast(e.Item.FindCont rol("txtEn g"), TextBox).Text.Replace(" ","")
2. To remove all spaces,
Dim engrave As String = DirectCast(e.Item.FindCont
Public Class ControlValues
Private ReadOnly txtEng As TextBox
Public Sub New(ByVal e As DataGridItem)
txtEng = CType(e.FindControl("txtEn g"),TextBo x)
End Sub
Public ReadOnly Property Engrave() As String
Get
Return GetValue(txtEng).Replace(" ",,"") 'remove all spaces
End Get
End Property
Private Function GetValue(ByVal ctl As TextBox) As String
Dim value As String = String.Empty
If Not ctl Is Nothing Then
value = ctl.Text.Trim()
End If
Return value
End Function
End Class
Private ReadOnly txtEng As TextBox
Public Sub New(ByVal e As DataGridItem)
txtEng = CType(e.FindControl("txtEn
End Sub
Public ReadOnly Property Engrave() As String
Get
Return GetValue(txtEng).Replace("
End Get
End Property
Private Function GetValue(ByVal ctl As TextBox) As String
Dim value As String = String.Empty
If Not ctl Is Nothing Then
value = ctl.Text.Trim()
End If
Return value
End Function
End Class
ASKER
thanks again
no problems =)
Select Case e.CommandName
Case "AddEngrav"
Dim engrave As String = DirectCast(e.Item.FindCont
If engrave.Length > 0 Then
...
End If
End Select
End Sub