473,799 Members | 3,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Forcing "Not IsPostBack" code to run on postback if nessessary???

I have run into a situation where I need to run the !IsPostBack code under
one circumstance, even if it is a postback. Something that may complicate
matters more is that this is a double postback event.

I have a filemanager type application that allows me to dynamically create a
new folder on the web server. After the new folder is created, I need to
reread the directory and display all the current folders in this directory
to the user.

If I load all of the !IsPostBack code on every page load, everything works
fine but it is just so inefficient.

The code in the create folder event looks like this:
-------------
Directory.Creat eDirectory(NewF olderString);
StatusMessage.T ext="Folder " + NewFolderName.T ext + "has been created";
ViewState["ExecNonPostBac k"]="true";
Page.RegisterSt artupScript("Po stBack","<Scrip t
Language=\"Java script\">docume nt.forms[0].submit();</Script>"); //do double
post back
-------------

and the code in the page load event looks like this

-------------
string s = (string)ViewSta te["ExecNonPostBac k"];
if (!(Page.IsPostB ack)||(s=="true "))
{ ... display folders to the user}
--------------

In my attempt to use the state bag to determine if a code should be
executed, the view state variable set in the create folder event does not
carry to the page load event.

One thing to note is that the create folder event is in a user control that
is loaded into the page.

So, How do i force the page to run the IsPostBack code during a postback
ONLY when I want it to???

Thanks

Earl
Nov 18 '05 #1
2 3559
Hello Earl,

Earl Teigrob wrote:
I have run into a situation where I need to run the !IsPostBack code under
one circumstance, even if it is a postback. Something that may complicate
matters more is that this is a double postback event.
Not that I'm not going to give a solution for this, but why do you want
to do a double postback ? Seems an unnecessary dirty trick to me.
Because in the Create Folder event handler, nothing has been rendered
to the client yet, you can re-read the directory and re-populate the
control that is displaying the list from there. If you don't want
to have the refresh code in the user control, you can make the user
control emit an event, that you handle in the control or page
where you want to put the refresh code.

Now the answer to the original question, in case you don't
want to follow my advice ...
The code in the create folder event looks like this:
-------------
Directory.Creat eDirectory(NewF olderString);
StatusMessage.T ext="Folder " + NewFolderName.T ext + "has been created";
ViewState["ExecNonPostBac k"]="true";
Page.RegisterSt artupScript("Po stBack","<Scrip t
Language=\"Java script\">docume nt.forms[0].submit();</Script>"); //do
double post back
-------------
Here you're accessing the ViewState of the user control.
and the code in the page load event looks like this
-------------
string s = (string)ViewSta te["ExecNonPostBac k"];
if (!(Page.IsPostB ack)||(s=="true "))
{ ... display folders to the user}
--------------


And here you're accessing the ViewState of the page.
So you don't read from the same object that you write to,
that's why the "flag" trick doesn't work.

Create a property in your page like :

public bool ExecNonPostBack
{
get
{
if(ViewState["ExecNonPostBac k"] == null)
return(false);

return((bool)(V iewState["ExecNonPostBac k"]));
}
set
{
ViewState["ExecNonPostBac k"] = value;
}
}

And access that property from the user control and
in the Page_Load handler of the page.

That way both the page and the user control access
the same ViewState object to get/set the flag.

Best regards,

Eric
Nov 18 '05 #2
Earl

You are correct on all counts and I had to rethink what I was doing. I have
now created events as you suggented and data bind methods to call when that
event is fired. Works great and is so much cleaner. Thanks for your Help.

Also, I now understand how viewstate works better. I did not realize that
each control had it own state bag.

Thanks Again

Earl

"Eric Veltman" <eric@[RemoveThis]veltman.nu> wrote in message
news:vu******** ****@corp.super news.com...
Hello Earl,

Earl Teigrob wrote:
I have run into a situation where I need to run the !IsPostBack code under one circumstance, even if it is a postback. Something that may complicate matters more is that this is a double postback event.


Not that I'm not going to give a solution for this, but why do you want
to do a double postback ? Seems an unnecessary dirty trick to me.
Because in the Create Folder event handler, nothing has been rendered
to the client yet, you can re-read the directory and re-populate the
control that is displaying the list from there. If you don't want
to have the refresh code in the user control, you can make the user
control emit an event, that you handle in the control or page
where you want to put the refresh code.

Now the answer to the original question, in case you don't
want to follow my advice ...
The code in the create folder event looks like this:
-------------
Directory.Creat eDirectory(NewF olderString);
StatusMessage.T ext="Folder " + NewFolderName.T ext + "has been created";
ViewState["ExecNonPostBac k"]="true";
Page.RegisterSt artupScript("Po stBack","<Scrip t
Language=\"Java script\">docume nt.forms[0].submit();</Script>"); //do
double post back
-------------


Here you're accessing the ViewState of the user control.
and the code in the page load event looks like this
-------------
string s = (string)ViewSta te["ExecNonPostBac k"];
if (!(Page.IsPostB ack)||(s=="true "))
{ ... display folders to the user}
--------------


And here you're accessing the ViewState of the page.
So you don't read from the same object that you write to,
that's why the "flag" trick doesn't work.

Create a property in your page like :

public bool ExecNonPostBack
{
get
{
if(ViewState["ExecNonPostBac k"] == null)
return(false);

return((bool)(V iewState["ExecNonPostBac k"]));
}
set
{
ViewState["ExecNonPostBac k"] = value;
}
}

And access that property from the user control and
in the Page_Load handler of the page.

That way both the page and the user control access
the same ViewState object to get/set the flag.

Best regards,

Eric

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3116
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL" automatically after programs do "delete p"?
9
15859
by: Susan Bricker | last post by:
I am currently using the OnDirty event of a Form to detect whether any fields have been modified. I set a boolean variable. Then, if the Close button is clicked before the Save button, I can put up a message on the screen that will tell the user that "Data has been changed, do you wish to Save the record before Closing this window?" I think I've just discovered this only works if the record that is being displayed/modified it not a...
72
4241
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and c?
2
7671
by: Lenonardo | last post by:
Hi. I'm writing a VB.Net application to update multiple Excel Worksheets. I'm using late binding (i.e. all variables are objects + use createobject) I develop the application on an XP machine with Excel 2002 (version 10.0) installed - and the code works fine. I then test the code on a laptop running Excel 2000 (version 9.0) and it
8
2019
by: David Thielen | last post by:
Hi; ..NET 2.0 I have a situation where when the user selects an item in a drop down list, the code behind is called to update the values in another list on the page. This update can take 3 - 5 seconds. When it is complete, the same page is still displayed. What is the best way to tell the user the page is working? I don't like the idea of switching to a
6
2434
by: KiwiGenie | last post by:
Hi..I am trying to make a search form. I am fairly new to access and could well be looking at it completely wrong. I have an unbound form with textboxes in the header for entering different search criteria. I have a subform for displaying the results, which is bound to Query4. SQL for Query4 (taken from sql view in query): SELECT tblRecipes.RecipeName, tblRecipes.FoodCategory, Sum(Query3.IngredCost) AS SumOfIngredCost, Query3.RecipeID FROM...
4
13310
by: Chris | last post by:
Hi, i 'm experimenting with postback and i tried that with a button server control and an Html input button but with runat="server". The button server control causes a postback, but not the Html input button with runat="server". Can someone explain me why (because it's running on the server)? Thanks
4
2110
by: R.A.M. | last post by:
Hello, I am writing ASP.NET application in which I need to use User Profiles and Session mechanisms. Here I include part of my source code (Admin.cs): using System; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using System.Data.SqlTypes;
6
1764
by: grbgooglefan | last post by:
I am creating functions, the return result of which I am using to make decisions in combined expressions. In some expressions, I would like to inverse the return result of function. E.g. function contains(source,search) will return true if "search" string is found in source string. I want to make reverse of this by putting it as: if ( ! contains(s1,s2) ): return 1
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9543
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.