• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

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)
            ...............
0
MikeMCSD
Asked:
MikeMCSD
  • 7
  • 3
1 Solution
 
b1xml2Commented:
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
0
 
b1xml2Commented:
It is much better to have another class which receives the DataGridItem and provide strongly typed values
0
 
MikeMCSDAuthor Commented:
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.
0
Industry Leaders: 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!

 
b1xml2Commented:
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
0
 
b1xml2Commented:
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
       Page.RegisterClientScriptBlock("message","<script language='javascript'>window.onload = function () { alert('Engrave Should Have Some Values'); };</script>")
     Else
       ....
     End If
     End Select
End Sub


The key point being this:
Page.RegisterClientScriptBlock("message","<script language='javascript'>window.onload = function () { alert('Engrave Should Have Some Values'); };</script>")
0
 
MikeMCSDAuthor Commented:
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
0
 
b1xml2Commented:
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(" ","")
0
 
b1xml2Commented:
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
0
 
MikeMCSDAuthor Commented:
thanks again
0
 
b1xml2Commented:
no problems =)
0

Featured Post

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.

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