How to prevent calling of function on every postback request - asp.net

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"

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.

Force a PostBack

I have an asp form in which on_button_click a thread a being executed following a finite number of loops. So I want after every single loop completion a postback should occur. So would you please help me out to find the way of doing post back by coding after single loop completion.
Thread tt = new Thread (mainProcess);
Button1_Click() {
tt.start();
}
void mainprocess()
{
while(true)
{
//do this
if(Condition)
break;
//do postback
}
}
From the moment you fire a thread on code behind, the thread did not have any control/connection with the page to make some how a postback or refresh.
You need to redesign your page some other way. One possible is to use ajax to make your call to the page and get the results when they are ready, or to make using ajax time to time call to code behind and see if the data are ready to gets them. Or make every 20 seconds a page refresh and again check if the loop has ended and get and show the data.

Execute function on specific key stroke in VB

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

Asp.net Wizard Control with Dynamic Steps gets stuck

I have a wizard control which must use dynamic steps in. I have the following code which loads the dynamic steps (this all works fine). I have 7 static steps.
protected override LoadViewState(object savedState)
{
base.LoadViewState(savedState);
int offset = 4;
foreach(string stepName in this.ViewState["Steps"])
{
WizardStep step = new WizardStep();
step.Title = stepName;
this.Wizard1.WizardSteps.AddAt(step, offset); // LINE 1
this.Wizard1.WizardSteps.Add(step); // LINE 2
offset++;
}
}
I have two issues, when I execute the code and use Line 1. When I get to a dynamic step it won't let you procede to the next one (using the Next button). This seems to be because this.IsValid is false (but I have no validation controls on the page). It just seems to get stuck on that current page.
When I run using Line 2, it adds the steps again fine. When I am on the first dynamic step and click Next I get the error. ActiveViewIndex is being set '7'. It must be smaller than the current view controls '7'. For dynamically added views, make sire they are added before or in Page_PreInit event.
The issue with the second error is I can't add the dynamic steps in Page_PreInit because I need access to the viewstate to know how many steps to draw.
I found the issue. Its since the steps must be added in the Page_PreInit event. Which does mean I can't use the Viewstate but I am using the Session instead now.

Resources