Link to home
Start Free TrialLog in
Avatar of MikeMCSD
MikeMCSDFlag for United States of America

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

<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.DataGridCommandEventArgs) Handles grid.ItemCommand
      If e.CommandName = "AddEngrav" Then
         Dim engrave As String = CType(e.Item.FindControl("txtEng"), TextBox).Text
         Try
            ShoppingCart.UpdateCartEng(productId, engrave)
            ...............
Avatar of b1xml2
b1xml2
Flag of Australia image

Private Sub grid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles grid.ItemCommand
      Select Case e.CommandName
      Case "AddEngrav"
             Dim engrave As String = DirectCast(e.Item.FindControl("txtEng"), TextBox).Text.Trim()
             If engrave.Length > 0 Then
                  ...
             End If
            
      End Select
End Sub
It is much better to have another class which receives the DataGridItem and provide strongly typed values
Avatar of MikeMCSD

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.
Public Class ControlValues
      Private ReadOnly txtEng As TextBox
      
      Public Sub New(ByVal e As DataGridItem)
            txtEng = CType(e.FindControl("txtEng"),TextBox)
      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
Avatar of b1xml2
b1xml2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.AppSettings("ConnectionString")
      End Get
End Property
1. Dim engrave As String = DirectCast(e.Item.FindControl("txtEng"), 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.FindControl("txtEng"), TextBox).Text.Replace(" ","")
Public Class ControlValues
     Private ReadOnly txtEng As TextBox
     
     Public Sub New(ByVal e As DataGridItem)
          txtEng = CType(e.FindControl("txtEng"),TextBox)
     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
thanks again
no problems =)