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

E

Earl Teigrob

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.CreateDirectory(NewFolderString);
StatusMessage.Text="Folder " + NewFolderName.Text + "has been created";
ViewState["ExecNonPostBack"]="true";
Page.RegisterStartupScript("PostBack","<Script
Language=\"Javascript\">document.forms[0].submit();</Script>"); //do double
post back
-------------

and the code in the page load event looks like this

-------------
string s = (string)ViewState["ExecNonPostBack"];
if (!(Page.IsPostBack)||(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
 
E

Eric Veltman

Hello Earl,

Earl said:
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.CreateDirectory(NewFolderString);
StatusMessage.Text="Folder " + NewFolderName.Text + "has been created";
ViewState["ExecNonPostBack"]="true";
Page.RegisterStartupScript("PostBack","<Script
Language=\"Javascript\">document.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)ViewState["ExecNonPostBack"];
if (!(Page.IsPostBack)||(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["ExecNonPostBack"] == null)
return(false);

return((bool)(ViewState["ExecNonPostBack"]));
}
set
{
ViewState["ExecNonPostBack"] = 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
 
E

Earl Teigrob

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 said:
Hello Earl,

Earl said:
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.CreateDirectory(NewFolderString);
StatusMessage.Text="Folder " + NewFolderName.Text + "has been created";
ViewState["ExecNonPostBack"]="true";
Page.RegisterStartupScript("PostBack","<Script
Language=\"Javascript\">document.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)ViewState["ExecNonPostBack"];
if (!(Page.IsPostBack)||(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["ExecNonPostBack"] == null)
return(false);

return((bool)(ViewState["ExecNonPostBack"]));
}
set
{
ViewState["ExecNonPostBack"] = 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top