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

java script/Ajax, VB Code Behind....

I have a VB.Net page that users place orders for equipment.

The code beind checks each row to see if a qty has been entered for an item. The problem I am having is if they hit submit with no qty's entered a Blank order is sent. So....

I need to check to make sure something was ordered.  The checking of each row happens on the button click event.

Here is the code from my Click event... Can anyone tell me  how to check to see if nothing was ordered and alert the user....

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Set session variables for the header section of the form.
        Session("Company") = txtCompany.Text
        Session("CustomerNo") = txtCustomerNo.Text
        Session("FName") = txtfName.Text
        Session("LName") = txtlName.Text
        Session("Email") = txtEmail.Text
        Session("RequestDate") = txtRequestDate.Text
        'Session("RequiredDate") = txtRequiredDate.Text
        Session("Store") = txtStore.Text
        Session("ShipMethod") = ddlShipMethod.SelectedValue
        Session("CreatedBy") = lblCreatedby.Text
        Session("UserIP") = lblUserIP.Text
        Session("Status") = lblStatus.Text
        Session("ApprovedBy") = lblApprovedBy.Text
        Session("ApprovedDate") = lblApprovedDate.Text
        Session("AddInfo") = txtMemo.Text
        Session("OKTOGO") = ""



        'Create the order in the Orders Table...
        CreateOrder()
        lblOrder.Text = Session("MyOrder")

        'Setup Email Message
        Dim sb As New System.Text.StringBuilder()
        sb.Append("<table>")
        sb.AppendFormat("<tr><td><b>{0}</b></td><td><b>{1},</b></td></tr>", "Thank You for your order", Session("FName"))
        sb.AppendFormat("<tr><td><b>{0}</b></td><td><b>{1}</b></td>", "Your Order# is: ", Session("MyOrder"))
        sb.AppendFormat("<tr><td><b>{0}</b></td><td><b>{1}</b></td>", "Store #: ", Session("Store"))
        sb.AppendFormat("<tr><td><b>{0}</b></td><td><b>{1}</b></td>", "Request Date: ", Session("RequestDate"))
        sb.AppendFormat("<tr><td><b>{0}</b></td><td><b>{1}</b></td>", "", "")
        sb.AppendFormat("<tr><td><b>{0}</b></td><td><b>{1}</b></td>", "Ship Method: ", Session("ShipMethod"))

        sb.Append("</table>")
        sb.AppendFormat("<hr>")
        sb.Append("<table border = 1 cellpadding= 10>")
        sb.AppendFormat("<tr><td><b>{0}</b></td><td><b>{1}</b></td><td><b>{2}</b></td><td><b>{3}</b></td><td><b>{4}</b></td></tr>", "Item", "Description", "Qty", "Spare", "Return Tag")

        'Check each row to see if the item was requested.
        '****** Row 1 *************************************
        If txtQty1.Text > 0 Then
            'The Item was requested So... Set the session variables for the item, update the Email Message, write the record to the database.
            Session("Item") = lblItem1.Text
            Session("Descp") = lblDescp1.Text
            Session("Qty") = txtQty1.Text
            Session("CB") = cbCB1.Checked
            Session("Spare") = cpS1.Checked
            Session("Code") = ""
            Session("SN") = ""
            Session("OKTOGO") = "1"

            sb.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td></tr>", Session("Item"), Session("Descp"), Session("Qty"), Session("Spare"), Session("CB"))

            BuildSQL()

            Session("Item") = ""
            Session("Descp") = ""
            Session("Qty") = ""
            Session("CB") = ""

        End If
0
Wildone63
Asked:
Wildone63
  • 9
  • 7
1 Solution
 
Rajar AhmedConsultantCommented:
You can proceed like below process, Validation for each textbox in  server side / client side
if txtCompany.Text <>"" then 
Response.Write("Company")
elseif txtlname.Text <>"" then 
Response.Write("lastname")
'and so on.....
else 'Successfull entry all details entered 

'Implement Successful logic
End if

Open in new window

Client side /JAvascript Validation

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return validateDetails();" />
<script>
function validateDetails(){
if(document.getElementById("txtCompany").value ==""){
alert("Please Enter company");
return false;
}
else if(document.getElementById("txtlName").value ==""){
alert("Please Lastname");
return false;
}
//and so on..
return true;
}
</script>

Open in new window

0
 
Wildone63Author Commented:
I think you are missing my question....

The Company/last name etc... is always pre-filled in from the web site based on the users login.

My Problem is if they do not select a qty for any of the items and hit submit... The order still is sent but is empty because the user did not enter a qty.

So if there is no qty ordered, I want to alert the user that they must enter a qty for at least one item.
0
 
Rajar AhmedConsultantCommented:
Are you validating txtQty1 TEXTBOX CONTROL ? If so try like this

//Server side
 If IsNumeric(txtQty1.Text) AndAlso txtQty1.Text > 0 Then
   'Successfull Condition   
Else
   Response.Write("Please Enter valid quantity")
 End If

Open in new window


i dint find any qty named controls on your code , so its confusing .
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
Wildone63Author Commented:
Well not exactly. Your post above does not work, but I need to check ALL boxes before I present an alert. There are 96 txtQTY text boxes. If any of them has a qty then the order is ok to process and no alert is needed.
0
 
Rajar AhmedConsultantCommented:
well u can try like below ,
  For Each key As String In Request.Form.AllKeys
            If key.Contains("txtQty") Then
                If IsNumeric(Request.Form(key)) AndAlso Request.Form(key) > 0 Then
                    'Successfull Condition  
                    Response.Write("success")
                Else
                    Response.Write("Invalid")
                End If
            End If
        Next

Open in new window

0
 
Wildone63Author Commented:
Ok Still not getting a popup window that alerts the user that they did not order anything.

I also do not follow your logic, I placed your code in my code behind and it does not do anything. I do not get an error, and if I do not enter a qty, the code behind just continues...
0
 
Rajar AhmedConsultantCommented:
Logic,
 1. While clicking button, the form will be posted , So we can get all the form element on postback using Request.Form approach
  2.key.Contains("txtQty")
      If you follow id of quantity textboxes such a way like txtQty1,txtQty2.....and so on you can get all the value entered on the quantity textboxes .

If any of them has a qty then the order is ok to process and no alert is needed.
The below code will validate the above quote and shows a popup message if it fails.
Function checkAllQtyEntered() As Boolean
        Dim isAllEntered As Boolean = True
        For Each key As String In Request.Form.AllKeys
            If key.Contains("txtQty") Then
                'Response.Write(Request.Form(key))
                If Not (IsNumeric(Request.Form(key)) AndAlso Request.Form(key) > 0) Then
                    ClientScript.RegisterStartupScript([GetType](), "AlertFailed", "alert('Please Enter quantity');", True)
                    isAllEntered = False
                    Exit For
                End If
            End If
        Next
        Return isAllEntered
    End Function
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        If checkAllQtyEntered() Then

            'Set session variables for the header section of the form.
            Session("Company") = txtCompany.Text
            Session("CustomerNo") = txtCustomerNo.Text
           .......................
.......................
        End If
    End Sub

Open in new window


Meeran 03
0
 
Wildone63Author Commented:
OK I understand now what you are saying, and I really like the idea but.... It didnt work.

I put your function on my .vb page. and added a call to the button click event for the function.

I did not enter any qty and it just went right past your function.

All of my qty boxes are txtQTY1 txtQTY2 etc...

Almost like it is just ignoring the call to your function...
0
 
Wildone63Author Commented:
Here is where it is missing...

It goes to the function but the line

If key.Contains("txtQty") Then

Never is true....
0
 
Rajar AhmedConsultantCommented:
Proper CASE is required on the key
if your textboxes are like  txtQTY1 txtQTY2 then ,
'If key.Contains("txtQty") Then
Replace to

If key.Contains("txtQTY") Then
Meeran03
0
 
Wildone63Author Commented:
I thought that may be the case and I checked them all, They are all

txtQty1 etc... I was able to put in a break and a msgbox(key) and it showed them all to me, it did eventually get to the txtQty1 but again did nothing....
0
 
Wildone63Author Commented:
I was able to capture every key, and the all are in the correct case.  But the alert never fires.
0
 
Rajar AhmedConsultantCommented:
Just a sample which is working here
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="txtReqFormQty.aspx.vb" Inherits="Vb_txtReqFormQty" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       ID txtQTY1 :<asp:TextBox ID="txtQTY1" runat="server"></asp:TextBox><br />
        txtQTY2<asp:TextBox ID="txtQTY2" runat="server"></asp:TextBox><br />
        txtQTY96<asp:TextBox ID="txtQTY96" runat="server"></asp:TextBox><br />
        txtQTY500<asp:TextBox ID="txtQTY500" runat="server"></asp:TextBox><br />
    <asp:Button ID="Button1" runat="server" Text="Validate" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>
Code behind 

Partial Class Vb_txtReqFormQty
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        If checkAllQtyEntered() Then
            ClientScript.RegisterStartupScript([GetType](), "AlertSuccess", "alert('Success');", True)
        End If
    End Sub
    Function checkAllQtyEntered() As Boolean
        Dim isAllEntered As Boolean = True
        For Each key As String In Request.Form.AllKeys
            If key.Contains("txtQTY") Then
                'Response.Write(Request.Form(key))
                If Not (IsNumeric(Request.Form(key)) AndAlso Request.Form(key) > 0) Then
                    ClientScript.RegisterStartupScript([GetType](), "AlertFailed", "alert('Please Enter quantity');", True)
                    isAllEntered = False
                    Exit For
                End If
            End If
        Next
        Return isAllEntered
    End Function
End Class

Open in new window

This is view source which holds qty key format on each textbox . Please check yours also .
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title></head>
<body>
    <form method="post" action="txtReqFormQty.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTAxNTQxNDc2MWRkrVipLEAI1ifLJ+xdil12marDwEUaq9hSk8tJ8iu8RUI=" />
</div>

<div class="aspNetHidden">

	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAZuDSkBMY6novtBuqsQBLB9UPHWiRIlX//MpkYBzFh0GSVIcA23MraY6KwzsW9WdimIT80/h4TqrfeBQZg+f2+2xAOYxiT7wu2LqaHl9ZJVRs34O/GfAV4V4n0wgFZHr3f+eKk/DfL1IGQWOZID2obMPs8yCLJy+qUG6MotxICR4Q==" />
</div>
    <div>
        txtQTY1 :<input name="txtQTY1" type="text" value="232" id="txtQTY1" /><br />
        txtQTY2<input name="txtQTY2" type="text" value="233" id="txtQTY2" /><br />
        txtQTY96<input name="txtQTY96" type="text" value="323" id="txtQTY96" /><br />
        txtQTY500<input name="txtQTY500" type="text" value="323" id="txtQTY500" /><br />
    <input type="submit" name="Button1" value="Invisible button" id="Button1" />
    </div>
    

<script type="text/javascript">
//<![CDATA[
alert('Success');//]]>
</script>
</form>
</body>
</html>

Open in new window


Meeran03
0
 
Wildone63Author Commented:
First Thank You  for all of your help Meeran03. It is truly appreciated.

But it still does not work for me.

I took your sample and it almost works, except if you do enter a qty you still get the alert to enter a qty.

I used your sample exactly without any of my code. You must put a qty in every box to get the success message.

Also my text boxes are in CollapsiblePanelExtender
and all text boxes are defaulted to 0
0
 
Rajar AhmedConsultantCommented:
You must put a qty in every box to get the success message.
Yes , that is what i did .
Dat works in a scenario , where you need to enter all the qty textboxes with greater than zero otherwise you will get error message . I thought you asked for that only .


This works for atleast for one valid qty is entered . Hope you are asking if i understood correctly . If not , just create a checklist/bullet point and mention what is missing in this below example. So that i can help you .

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       ID txtQTY1 :<asp:TextBox ID="txtQTY1" runat="server" Text="0"></asp:TextBox><br />
        txtQTY2<asp:TextBox ID="txtQTY2" runat="server" Text="0"></asp:TextBox><br />
        txtQTY96<asp:TextBox ID="txtQTY96" runat="server" Text="0"></asp:TextBox><br />
        txtQTY500<asp:TextBox ID="txtQTY500" runat="server" Text="0"></asp:TextBox><br />
    <asp:Button ID="Button1" runat="server" Text="Validate"  OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>
Code behind
Partial Class Vb_txtReqFormQty
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        If checkAnyQtyEntered() Then
            ClientScript.RegisterStartupScript([GetType](), "AlertSuccess", "alert('Success');", True)
        End If
    End Sub
    'All Entered Validation - Starts
    Function checkAllQtyEntered() As Boolean
        Dim isAllEntered As Boolean = True
        For Each key As String In Request.Form.AllKeys
            If key.Contains("txtQTY") Then
                If Not (IsNumeric(Request.Form(key)) AndAlso Request.Form(key) > 0) Then
                    ClientScript.RegisterStartupScript([GetType](), "AlertFailed", "alert('Please Enter quantity');", True)
                    isAllEntered = False
                    Exit For
                End If
            End If
        Next
        Return isAllEntered
    End Function
    'All Entered Validation - Ends
    'Any Valid Entered Validation - Starts
    Function checkAnyQtyEntered() As Boolean
        Dim isAnyValidEntered As Boolean = False
        Dim errMess As String = "Please Enter quantity"
        For Each key As String In Request.Form.AllKeys
            If key.Contains("txtQTY") Then
                If (Not String.IsNullOrEmpty(Request.Form(key)) AndAlso Not IsNumeric(Request.Form(key))) Then 'This will check if Non numeric is entered
                    isAnyValidEntered = False
                    errMess = "Please Enter Valid Quantity"
                    Exit For
                ElseIf (IsNumeric(Request.Form(key)) AndAlso Request.Form(key) > 0) Then
                    isAnyValidEntered = True
                End If
            End If
        Next
        If Not isAnyValidEntered Then
            ClientScript.RegisterStartupScript([GetType](), "AlertFailed", "alert('" & errMess & "');", True)
        End If
        Return isAnyValidEntered
    End Function
    'Any Valid Entered Validation - Ends


End Class

Open in new window


Meeran03
0
 
Wildone63Author Commented:
meeran03,

Thank You Very Much! This is exactly what I needed. I could not have gotten it without your help!

Thank You!!!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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