Execute function on specific key stroke in VB - asp.net

I'm developing a web application that displays items from a work queue to a user. When an item is selected I have the app lock that item out so no other user can select it. By hitting the back button in the app it unlocks the item.
I want to be able to unlock the item if the user hits the backspace key. I know what code I need to unlock it. I just need to know how to make the code execute on backspace key stroke.
The code that I need to execute will be server side code.
Thanks in advance.

<script>
document.onkeydown = function (e)
{
if (window.event && window.event.keyCode == 8) {
__doPostBack('__Page', 'MyCustomArgument');
}
}
</script>
If you need to execute code on server, you have to change your question accordingly
EDIT:
You could set a Hiddenfield's value to f.e. "unlockItem" and do a document.forms[0].submit() and checkthe hidden-value on serverside or better:
Use the clientside __doPostBack function generated from ASP.Net to submit page(for example on selectedIndexChanged of a DropDownList). You could even generate it from Codebehind if you want the cleanest way.
I changed the above code, but i think your next question could be how you should know which item was selected, won't it?
Then you have to clarify what items we are talking about.
On serverside you get the passed arguments with:
If Page.IsPostBack Then
Dim eventArg As String = Request("__EVENTARGUMENT")
End If
End If

Related

How to achieve callback functionality on saving a record when in auto save mode in google appmaker?

I have a model which is in autosave mode. When the user clicks on a button below code is executed.
I want the status to change and get saved and then it should execute the refresh function as the refresh functionality is dependent on the status value. But with the below code refresh function is getting executed before the new status is getting saved.
widget.datasource.item.status='inside';
refreshPanelWithColor();
What I really want to do is this to have the callback functionality but I can't use the saveChanges as it's only for manual save mode.
widget.datasource.item.status='inside';
widget.datasource.saveChanges(function() {
refreshPanelWithColor();
});
How to achieve callback functionality here without switching to manual save mode?
As how Markus Explains in his comment, this functionality is not available at the moment. You can of course use another solution which is using server scripting and reloading the datasource item. In order to achieve that, your client script should look similar to this:
var recordKey = widget.datasource.item._key;
var status = "inside";
google.script.run.withSuccessHandler(function(){
widget.datasource.item._reload(function(){
refreshPanelWithColor();
});
}).withFailureHandler(function(err){
console.err(err.toString());
}).updateDesiredRecord(recordKey, status);
And of course, you need to implement your server script being called from the client. It should look something like this:
function updateDesiredRecord(recordKey, status){
var record = app.models.MYMODEL.getRecord(recordKey);
record.status = status;
app.saveRecords([record]);
}
I'm not sure what your refreshPanelWithColor() function does but I hope you get an idea of what this solution intends to.
You just have to bind your function to datasource on after save event.

Table Not Updating With Manual Save Mode

So I have a table that shows the entries. Users click on a button to open a fragment page to edit the data.
app.datasources.SystemOrders.selectKey(widget.datasource.item._key);
app.showDialog(app.pageFragments.SystemOrders_Edit);
This part works fine.
I have changed my datasource to Manual Save Mode to be able to utilize the "email notification for changes" functions that are used in the Project Tracker sample. So that a user can make changes, hit a Save (Close) Button and an email goes out showing the changes.
The problem is that when the user closes the fragment, the table does not update (they have the same datasource). When I was in automatic save mode, I was able to utilize the following to force the table to reload so it reflected any changes:
var datasource = app.datasources.SystemOrders_HideComplete;
datasource.load();
app.closeDialog();
So I figured I just needed to add the widget.datasource.saveChanges(); option for the Close Button.
widget.datasource.saveChanges();
var datasource = app.datasources.SystemOrders_HideComplete;
datasource.load();
app.closeDialog();
Unfortunately, when I use the above I get the following error and the table seems like it gets stuck trying to reload (the spinner keeps spinning).
"Cannot query records on models with changes."
I'm assuming this is maybe because the datasource hasn't finished saving the new change, before trying to reload the datasouce?
How can I have the Save (Close) Button:
Save the changes
Close the dialog
Refresh the table so it reflects the changes?
Thank you for your help.
You can try to refresh datasource in save callback(assuming that you are actually sharing datasource between page fragment and page):
widget.datasource.saveChanges(function() {
widget.datasource.load();
app.closeDialog();
});
That error will happen if your datasource is trying to refresh while you have unsaved changes.
I know your code block calls a client-side saveChanges before loading the datasource again, but you may need to check to make sure that the datasource is not being reloaded elsewhere at the same time.
If that hasn't fixed it, try passing the values to a server-side script and save the changes after assigning the properties, like this:
Client:
function handleSuccess() {
// ensures that records are saved, now reload
app.models.YourModel.YourDatasource.load();
}
google.script.run.withSuccessHandler(handleSuccess).addItem(field_value1, field_value2, field_value3);
Server:
function addItem(field_value1, field_value2, field_value3) {
// create new items
var newItem = app.models.YourModel.newRecord();
// assign values
newItem.field1 = field_value1;
newItem.field2 = field_value2;
newItem.field3 = field_value3;
// save
app.saveRecords([newItem]);
}
I've found this method to be more reliable than manipulating changes from the client script.

How to prevent calling of function on every postback request

I have bind complete menu on postback now every post back request function
call and bind menu again i want to call it only first time please suggest
here below is my code
if (!Page.IsPostBack)
{
objCommon = new Common();
Common.UpdateLoginSession();
if (hiddenMenuFlag.Value == "S")//used hidden field but not working as is
//does not retain value on post back please suggest
{
BindMenu("0");//here is function for binding menu
hiddenMenuFlag.Value="";
}
}
use
if (!IsPostBack)
{
--------------------------;
--------------------------;
}
All functions or code inside this condition will run only for the first time, when page is requested. It won't execute on reload.
If you want to run a code only once; when the user request the page then you can use some session as suggested above.
If you want to run a code only for the first time when application runs, then you can use Application state to control your code
You could create a session variable and then check that variable to ensure your code will execute only once.
You create session variable like this:
Session["myVar"] = "myText";
And then you could check it value like below:
((string)Session["myVar"]) == "myText"

Asp.net webforms autosave

I understand this may be an elementary question, but I'm new to Asp.net webforms, so please bear with me.
I have a lengthy form on a page that I would like to autosave when users type in a field, or make a selection. The problem is, all I've been able to find online is autosaves that work on a timer. I'd prefer that it saves as the user makes their edits. Also I would like just the individual form element being edited to be sent to the server to avoid sending the entire page back each time.
I've read that I should use a webservice to accomplish this, but since I want to autosave individual items and not the whole form on a timer, how would I set up a webservice to accomplish this? I'm new to webservices I'd like to know what to read up on. Any links are appreciated.
Also, how is the autosave functionality effected when using asp.net validation controls? I've looked around but can't tell if the entire page needs to be valid to make a trip to the server, or if just a single valid item can be sent itself.
Thanks for any help!
If you set AutoPostBack=True on the field, and you add an OnChange event for it (this will vary depending on the type of field the user is interacting with), you can execute a save. Don't call Page.Validate in the methods where you're doing these updates. Call it when you hit the Submit button.
This could cause a LOT of round trips to the server, and it's a lot of code to write and debug.
The Timer approach is one call to one method on a repetitive basis. If you can I'd recommend going with a timer, but sometimes that's not an option.
Generally speaking this is what you'll want to setup on the client-side. Ideally, you will end up with lots of tiny requests which do not require much power on the back-end. This however depends on lots of variables including the database engine you're using.
$(document).ready(function () {
$("input").blur(OnFieldChanged);
});
function OnFieldChanged()
{
var $this = $(this);
var isValid = ValidateField($this);
if (isValid)
{
SaveField($this);
}
}
function SaveField($field)
{
if ($this.val() === $this.prop("oldVal")) return;
var data = {
id: $("idfield").val()
};
data[$field.attr("id")] = $field.val();
$.post({..}).done(function() {
NotifySaved($this);
$this.prop("oldVal", $this.val());
});
}
function ValidateField($field)
{
// Validate the field with your method of choice, manually call Microsoft's client-side validate function or switch to jquery validate.
return true;
}

Google apps script ui enable button only when all values in grid are int

I have a Google Apps Script application written using the UI service (not the HTML service), and the app is embedded in a Google Site.
I have a grid of 15 values (3x5). I am able to use a clientHandler to validate that the the values in each textBox are integers.
I want to ensure that all 15 of the values are correctly set before enabling the Submit button.
What is the best way to do this?
Obviously, just toggling the the button .setEnabled property onChange is no good, as if one widget disables the button, but the next is a valid integer, it would re-enable the button.
I thought about using a serverHandler, but figured that could be slow and unreliable. I'd like to keep it client side if I can. It feels like it should be possible, but I just can't work it out. What am I missing?
All advice welcomed. Thanks.
This will enable submit once all fields are integers, but it'll need more added to it to handle cases where the values could get changed back to non-integer after first passing the validation.
function doGet() {
var app = UiApp.createApplication();
var field1 = app.createTextBox();
var field2 = app.createTextBox();
var field3 = app.createTextBox();
var submit = app.createButton('SUBMIT').setEnabled(false);
var handler = app.createClientHandler().validateInteger(field1)
.validateInteger(field2).validateInteger(field3)
.forTargets(submit).setEnabled(true);
field1.addValueChangeHandler(handler);
field2.addValueChangeHandler(handler);
field3.addValueChangeHandler(handler);
app.add(field1).add(field2).add(field3).add(submit);
return app;
}
Deployed app.
Corey's UiApp posts are helpful in this area.

Resources