I am just a beginner in ASP.NET. Now I have 2 pages: master and child page(content page). On the child page, I create a html form which will be used to submit user's input to the server. I use the asp button to submit those but it just validates user's input by running my javascript. The label at the bottom is used to check if it does postback or not. That always shows "False" even the page refreshes after I click on the submit button. I don't know what I am doing wrong here. Also, I want to show all the information of user in a textbox after they are submitted to the server. Hope you can help me out with clear explanation. Thank you very much.
1/Master Page:
2/Child Page (content page):
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder" Runat="Server">
<div class="row">
<div class="box">
<div class="col-lg-12">
<hr>
<h2 class="intro-text text-center">Contact <strong>Mon Ami Cafe Restaurant</strong>
</h2>
<hr>
</div>
<div class="col-md-8" id="map-canvas">
<!-- Embedded Google Map using an iframe - to select your location find it on Google maps and paste the link as the iframe src. If you want to use the Google Maps API instead then have at it! -->
<!--iframe width="100%" height="400px" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=14291+S+Euclid+St,+Garden+Grove,+CA+92843&aq=&sll=33.754949,-117.938489&sspn=0.010437,0.021136&ie=UTF8&hq=&hnear=14291+S+Euclid+St,+Garden+Grove,+California+92843&t=m&z=14&ll=33.754949,-117.938489&output=embed"></!--iframe><br /><small>View Larger Map</small-->
</div>
<div class="col-md-4">
<h5>Phone:</h5>
<p><strong>*******</strong>
</p>
<h5>Email:</h5>
<p><strong>*******</strong>
</p>
<h5>Address:</h5>
<p><strong>*******</strong>
</p>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="row">
<div class="box">
<div class="col-lg-12">
<hr>
<h2 class="intro-text text-center">Contact
</h2>
<hr>
<form name="ContactForm" method="post">
<div class="row" id="ContactForm">
<div class="form-group col-lg-4">
<label>Name</label>
<input type="text" id="NAME" class="form-control">
</div>
<div class="form-group col-lg-4">
<label>Email Address</label>
<input type="text" id="EMAIL" class="form-control">
</div>
<div class="form-group col-lg-4">
<label>Phone Number</label>
<input type="text" id="PHONE" class="form-control">
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<label>Message</label>
<textarea id="MSG" class="form-control" rows="6"></textarea>
</div>
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<asp:Button ID="btnSubmit" runat="server" OnClientClick="return validateForm();" Text="Submit"/>
</div>
</div>
</form>
</div>
</div>
</div>
<!--The output information will be here -->
<p><asp:Label id="lbl1" runat="server" /></p>
</asp:Content>
Here is my script #Chia... :
<script>
function validateName() {
var x = document.getElementById("NAME").value;
if (x == null || x == "") {
alert("Name must be filled out");
return false;
}
else
return true;
}
function validateEmail() {
var y = document.getElementById("EMAIL").value;
var atpos = y.indexOf("#");
var dotpos = y.lastIndexOf(".");
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= y.length) {
alert("Not a valid e-mail address");
return false;
}
else
return true;
}
function validatePhone() {
var formatForm = /^[1-9]\d{9}$/;
var z = document.getElementById("PHONE").value;
if (z.length == 0) {
alert("Phone must be filled out");
return false;
}
else if (z.length < 10 || z.length > 10 || !(z.match(formatForm))) {
alert("Not a valid phone number");
return false;
}
else
return true;
}
function validateMess() {
var t = document.getElementById("MSG").value;
if (t == null || t == "") {
alert("Pleave leave your message");
return false;
}
else
return true;
}
function validateForm() {
if (validateName()) {
if (validateEmail()) {
if (validatePhone()) {
if (validateMess()) {
//alert("Submitted Sucessfully");
return true;
}
}
}
}
return false;
}
</script>
You need to add an onclick attribute to your asp button which calls an event in your code behind class. For example
<asp:Button ID="btnSubmit" runat="server" OnClientClick="return validateForm();" OnClick="btnSubmit_Click" Text="Submit"/>
Which would trigger the event
protected void btnSubmit_Click(object sender, System.EventArgs e)
{
// Do stuff here after postback
}
Because you have wired up your onclientclick attribute and ValidateForm is returning a bool value, then this will correctly only allow the onclick event to fire if the validateForm function returns true.
Related
I have built a web app with GAS that send the data on submit to a google spreadsheet.
Sometimes it happens that if I don't update the google spreadsheet manually, the data just entered in the form doesn't appear in the spreadsheet.
Why? Something is missing in the code I need to add?
This is my code.gs:
function doGet(request) {
return HtmlService.createTemplateFromFile('Index')
.evaluate();
}
/* #Include JavaScript and CSS Files */
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
/* #Process Form */
function processForm(formObject) {
var url = "xxx";
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("Data");
ws.appendRow([formObject.azienda,
formObject.test,
formObject.field1,
formObject.field2,
formObject.field3]);
}
function sceltaatecoedatiseguenti() {
var sheet = SpreadsheetApp.openById("xxx").getSheetByName("Sheet3");
var lastRow = sheet.getLastRow();
var myRange = sheet.getRange("A2:D" + lastRow); // Modified
var data = myRange.getValues();
var optionsHTML = "";
for (var i = 0; i < data.length; i+=1) {
optionsHTML += `<option data-values="${data[i][1]};${data[i][2]};${data[i][3]}">${data[i][0]}</option>`; // Modified
};
return optionsHTML;
}
This is my javascript.html:
<script>
// Prevent forms from submitting.
function preventFormSubmit() {
var forms = document.querySelectorAll('form');
for (var i = 0; i < forms.length; i++) {
forms[i].addEventListener('submit', function(event) {
event.preventDefault();
});
}
}
window.addEventListener('load', preventFormSubmit);
function handleFormSubmit(formObject) {
google.script.run.processForm(formObject);
document.getElementById("myForm").reset();
}
// Funzione scelta ateco e dati seguenti -prima parte-
function setValues(select) {
const [v1, v2, v3] = select.options[select.selectedIndex].dataset.values.split(";");
document.getElementById("field1").value = v1;
document.getElementById("field2").value = v2;
document.getElementById("field3").value = v3;
}
// Funzione scelta ateco e dati seguenti -seconda parte-
const select = document.getElementById("test");
setValues(select);
select.addEventListener("change", () => setValues(select));
</script>
HTML:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-12">
<form id="myForm" onsubmit="handleFormSubmit(this)">
<h1>Master Leads</h1>
<div class="form-row">
<div class="form-group col-md-12">
<label for="azienda">Azienda</label>
<input type="text" class="form-control" id="azienda" name="azienda" required="">
</div>
<div class="form-group col-md-12">
<span class="badge badge-primary">Codici Ateco</span>
</div>
<div class="form-group col-md-2">
<label for="test">Ateco 1</label>
<select class="custom-select" name="test" id="test">
<?!= sceltaatecoedatiseguenti(); ?>
</select>
</div>
<div class="form-group col-md-10">
<label for="field1">Sottocategoria</label>
<input type="text" class="form-control" id="field1" name="field1" readonly>
</div>
<div class="form-group col-md-8">
<label for="field2">Divisione</label>
<input type="text" class="form-control" id="field2" name="field2" readonly>
</div>
<div class="form-group col-md-4">
<label for="field3">Sezione</label>
<input type="text" class="form-control" id="field3" name="field3" readonly>
</div>
</div>
<button type="submit" class="btn btn-primary btn-block">Inserisci in Master Leads</button>
</form>
<div id="output"></div>
</div>
</div>
</div>
</body>
<?!= include('JavaScript'); ?>
</html>
Conclusions after testing with your spreadsheet:
I could reproduce the issue with your spreadsheet
I could reproduce the issue when making a copy of your spreadsheet
The issue does not occur when creating a brand new spreadsheet
Thus, the issue must be related to some setting in your spreadsheet.
You can either create a new spreadsheet from scratch or use the following workaround:
Go to File - > Spreadsheet Settings and change Calculation to On change and every minute.
This seems to solve your problem.
I want to take the id, name image and content of tinymce editor which i want to show on another page using controller. I am using array through which i want to take id, name, image and content but i don't know how to retrive it on another controller.
<div class="col-sm-4">
<div class="form-group">
<label class="settinglabel">Name</label>
<input type="text" maxlength="20" class="form-control" ng-model="data.name">
</div>
</div>
<div class="col-sm-3" style="margin-bottom: 4px;">
<div class="BoardCardWithCustomProperties BoardCardWithCustomProperties--hover" draggable="true">
<div class="BoardCardWithCustomProperties-contents">
<img class="BoardCardCoverImage" ng-src={{data.thumbnail}}>
<div class="BoardCardWithCustomProperties-nameAndDropdownButton">
</div>
</div>
</div>
</div>
<div id="design-9">
<div class="col-sm-12" style="margin-top:10px; margin-bottom:5px">
<textarea rows="20" cols="100" ui-tinymce="tinymceOptions" ng-model="tinymceModel" />
<input name="image" type="file" id="upload" class="hidden" onchange="">
</div>
</div>
myApp.controller('SampleFormatCntrl', ['$scope', '$http', 'orderProcessService', '$timeout', 'authService', '$routeParams', 'localStorageService', '$q', 'CommonhttpService', function ($scope, $http, orderProcessService, $timeout, authService, $routeParams, localStorageService, $q, CommonhttpService) {
var id = $routeParams.id;
$scope.data = localStorageService.get("formatdata");
$scope.action = $routeParams.action;
if ($scope.action == 'Edit' || $scope.action == 'Copy') {
$scope.action = "Update"
}
else {
$scope.action = "Save"
}
$scope.tinyArray = [];
$scope.screenshot = function () {
$scope.tinyArray.push($routeScope.tinyArray);
}
<div ng-repeat="elem in data">
<button id="{{elem.id}}" ng-click="showItem($index, elem)">
{{elem.name}}
</button>
</div>
Controller:
$scope.showItem = function(elementIndex, dataItem){
console.log(elementIndex); // 0
console.log(dataItem.id + ':' + dataItem.name); // 1:Abhishek
}
I am trying to implement the following behavior.
I have a form and I want to require filling at least one of the check boxes or text field.
I am trying to do this with the following code, but I don't know what am I doing wrong. Thanks in advance.
https://jsfiddle.net/AlexLavriv/mc8fj4f9/
// Code goes here
var app = angular.module('App', []).controller("Ctrl", Ctrl);
function Ctrl($scope) {
$scope.formData = {};
$scope.formData.selectedFruits = {};
$scope.fruits = [{'name':'Apple', 'id':1}, {'name':'Orange', 'id':2}, {'name':'Banana', 'id':3}, {'name':'Mango', 'id':4},];
$scope.someSelected = function (object) {
console.log(object);
for (var i in object)
{
if (object[i]){
return true;
}
}
return false;
}
$scope.submitForm = function() {
console.log($scope.formData.selectedFruits);
}
}
<div ng-controller="Ctrl" >
<form class="Scroller-Container" name="multipleCheckbox" novalidate >
<div ng-app>
<div ng-controller="Ctrl">
<div>
What would you like?
<div ng-repeat="(key, val) in fruits">
<input type="checkbox" ng-model="formData.selectedFruits[val.name]">
{{val.name}}
</div>
<p class="error" ng-show="submitted && multipleCheckbox.$error.required">Select check box or input text</p>
</div>
<pre>{{formData.selectedFruits}}</pre>
<input type="text" ng-required="!someSelected(formData.selectedFruits)" />
<input type="submit" id="submit" value="Submit" ng-click="submitted=true" />
</div>
</div>
</form>
</div>
You can't use ng-required with a function
You can find another solution here: https://jsfiddle.net/mc8fj4f9/2/
var app = angular.module('App', []).controller("Ctrl", Ctrl);
function Ctrl($scope) {
$scope.formData = {};
$scope.formData.selectedFruits = {};
$scope.fruits = [{'name':'Apple', 'id':1}, {'name':'Orange', 'id':2}, {'name':'Banana', 'id':3}, {'name':'Mango', 'id':4},];
$scope.submited=false;
$scope.someSelected = function (object) {
console.log(object);
for (var i in object)
{
if (object[i]){
$scope.checkboxSelected =true;
return true;
}
}
$scope.checkboxSelected =false;
return false;
}
$scope.submitForm = function() {
console.log("submit "+ $scope.formData.selectedFruits);
$scope.submitted=true;
$scope.someSelected($scope.formData.selectedFruits);
}
}
<div ng-controller="Ctrl" >
<form class="Scroller-Container" name="multipleCheckbox" novalidate>
<div ng-app>
<div ng-controller="Ctrl">
<div>
What would you like?
<div ng-repeat="(key, val) in fruits">
<input type="checkbox" ng-model="formData.selectedFruits[val.name]" ng-required = "!inputVal">
{{val.name}}
</div>
<p class="error" ng-show="submitted && !checkboxSelected && !inputVal">Select check box or input text</p>
</div>
<pre>{{formData.selectedFruits}}</pre>
<input type="text" ng-required="!checkboxSelected" ng-model="inputVal"/>
<input type="submit" id="submit" value="Submit" ng-click="submitForm()" />
</div>
</div>
</form>
</div>
I am working on SPA (Single Page Application) for Online Team Collaboration service(OTC) ,and I include HTML pages by ng-include,in some included page there is a popover ,this one contains a possibility for creating a public group chat,and in order to create one ,the user must submit, now my question is : how can i display a "successfully created" message in the same popover instead of the main div for creating the group in the popover?
The external page (the page that include other pages):
<div ng-show="ctrChanneldiv" ng-contoller="ctrChannelCon" style="float: right;" class="col-md-3">
<div ng-include="'CreateChannel.html'" ></div>
</div>
The Controller ctrChannelCon:
appvar.controller('ctrChannelCon',['$scope','$http','$rootScope',function($scope, $http, $rootScope){
$scope.createBtn = function() {
$http.post("http://localhost:8080/ExampleServletv3/crtchannelservlet",this.crtchannel)
.success(function(response) {
setTimeout(function () {
$scope.$apply(function(){
//******* Here Display "Successfully Created" In the Popover *******//
});
});
});
};
}]);
In CreateChannel.html :
<div>
<div class="row">
<a href="#" class="popper" data-toggle="popover"
data-placement="bottom"><span class="glyphicon glyphicon-plus"></span>Create Channel</a>
<div class="popper-content hide">
<div class="form-group">
<!-- ng-controller="createChannelCon" -->
<div class="form-group">
<label>Channel name:</label>
</div>
<div class="form-group">
<input ng-model="crtchannel.Name" type="text" placeholder="enter channel's name" maxlength="30"
class="form-control input-md" required />
</div>
<div class="form-group">
<label>Description:</label>
</div>
<div class="form-group">
<textarea cols="15" ng-model="crtchannel.Description" type="text"
placeholder="enter channel's description" maxlength="500"
class="form-control input-md" required></textarea>
</div>
<div>
<input ng-model="crtchannel.Type" type="radio" name="chtype"
value="private" required /> Private<br> <input
ng-model="crtchannel.Type" type="radio" name="chtype"
value="public" /> Public<br>
</div>
<div class="form-group">
<button ng-click="createBtn()" class="btn btn primary">Apply</button>
</div>
</div>
</div>
<script>
$('.popper').popover({
container : 'body',
html : true,
content : function() {
return $(this).next('.popper-content').html();
}
});
</script>
</div>
</div>
Any suggestions?
Thanks
You may use ngClass directive to counter this problem. ngClass directive allows conditional application of classes. Create a div for successfully created in the popup and set up ng-class directive with a variable in the scope and assign it true and false as per your requirement.
JS
appvar.controller('ctrChannelCon',['$scope','$http','$rootScope',function($scope, $http, $rootScope){
$scope.createBtn = function() {
$http.post("http://localhost:8080/ExampleServletv3/crtchannelservlet",this.crtchannel)
.success(function(response) {
setTimeout(function () {
$scope.$apply(function(){
//******* Change value of a model *******//
$scope.hidden = false;
});
});
});
};
}]);
HTML
<span ng-class="{hide: hidden}">Successfully Created</span>
CSS
.hide { display: none; }
Set a flag in your controller on success and use this flag to show or hide the success message:
...
<div class="form-group">
<button ng-click="createBtn()" class="btn btn primary">Apply</button>
</div>
<div ng-show="isSuccess">Successfully Created</div>
Inside the controller:
$scope.isSuccess = false;
$scope.createBtn = function() {
$http.post("http://localhost:8080/ExampleServletv3/crtchannelservlet",this.crtchannel)
.success(function(response) {
setTimeout(function () {
$scope.$apply(function(){
//******* Here Display "Successfully Created" In the Popover *******//
$scope.isSuccess = true;
});
});
});
};
I have the below popup where the user can encode item information.
My requirement is that, when Foreign Currency and Conversion Rate are both have values, it should multiply Foreign Currency * Conversion Rate to get the Amount. And when both Foreign Currency and Conversion Rate are 0, then Amount field should accept user input.
Currently, I have the below HTML.
<div class="form-group" show-errors>
<label for="foreignCurrency" class="control-label col-md-3 text-muted">Foreign Currency</label>
<div class="col-md-9">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-dollar fa-lg"></i> </span>
<input type="number" id="foreignCurrency" name="foreignCurrency" class="form-control" placeholder="Foreign Currency" ng-model="vm.newItem.newItemEnt.ForeignCurrency" value="{{vm.newItem.newItemEnt.ForeignCurrency || 0}}" min="0" />
</div>
<p class="help-block" ng-if="perksFrm.foreignCurrency.$error.min">The minimum foreign currency value is 0</p>
</div>
<div class="form-group" show-errors>
<label for="convRate" class="control-label col-md-3 text-muted">Conversion Rate</label>
<div class="col-md-9">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-money"></i></span>
<input type="number" id="convRate" name="convRate" class="form-control" placeholder="Conversion Rate" ng-model="vm.newItem.newItemEnt.ConversionRate" ng-required="vm.newItem.newItemEnt.ForeignCurrency" value="{{vm.newItem.newItemEnt.ConversionRate || 0}}" min="0" />
</div>
<p class="help-block" ng-if="perksFrm.convRate.$error.required">The conversion rate is required</p>
<p class="help-block" ng-if="perksFrm.convRate.$error.min">The minimum conversion rate is 0</p>
</div>
<div class="form-group" show-errors>
<label for="amount" class="control-label col-md-3 text-muted">Amount</label>
<div class="col-md-9">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-money"></i></span>
<input type="number" id="amount" name="amount" class="form-control" placeholder="Amount" ng-model="vm.newItem.newItemEnt.Amount" required />
</div>
<p class="help-block" ng-if="perksFrm.amount.$error.required">The first name is required</p>
</div>
In Amount html, I can do it like this {{vm.newItem.newItemEnt.ForeignCurrency * vm.newItem.newItemEnt.ConversionRate}}. But, what if they have a 0 value and my requirement is to accept the user input from Amount textbox.
Any advise to achieve my requirements?
TIA
According to your requirment, I tried to provide you answer.
Please find Code for this, also JS fiddle demo.
HTML
<style>
.error{
border-color:red;
}
</style>
<div ng-app="myApp" ng-controller="myCtrl">
<div class="row">
<div class="col-lg-2">Foreign Currency</div>
<div class="col-lg-2"><input type="number" ng-model="FCurrency" /></div>
</div>
<div class="row">
<div class="col-lg-2">Rate</div>
<div class="col-lg-2"><input type="number" ng-model="Rate" /></div>
</div>
<div class="row">
<div class="col-lg-2">Amount</div>
<div class="col-lg-2"><input ng-class="{error : RateAmount <= 0}" ng-disabled="isAmountDisable"
type="number" ng-model="RateAmount" /></div>
</div>
</div>
JS
var myApp = angular.module("myApp", []);
myApp.controller('myCtrl', function ($scope) {
$scope.RateAmount = 0;
$scope.isAmountDisable = false;
function setRateAmount() {
if ($scope.FCurrency > 0 && $scope.Rate > 0) {
$scope.RateAmount = ($scope.FCurrency * $scope.Rate);
$scope.isAmountDisable = true;
}
else {
$scope.RateAmount = 0;
$scope.isAmountDisable = false;
}
}
$scope.$watch('FCurrency', function (newval, oldval) {
setRateAmount();
});
$scope.$watch('Rate', function (newval, oldval) {
setRateAmount();
});
});
JS Fiddle Demo
Did you tried to use ng-change on foreignCurrency and convRate input.
when those input change you can calcul your amount value and disable it.
if they are both 0, you can enable it.
Something like this :
function change() {
var foreignCurrency = vm.newItem.newItemEnt.ForeignCurrency;
var conversionRate = vm.newItem.newItemEnt.ConversionRate;
if (foreignCurrency === 0 && conversionRate === 0) {
vm.enableAmout = true;
} else {
vm.enableAmout = false;
vm.vm.newItem.newItemEnt.Amount = foreignCurrency * conversionRate;
}
}