problem with c# code

A

arun

Hi I am making a booking for a tour. The tours are allowed on some
days only( one on monday, other on tuesday like that).

For the booking object saving, I am using a wizrd control. In the
second step( next_button click of the wizard control ) I will decide
wether the date selected is valid or not and save it in a class level
boolean variable for use in the finish_button click of the wizard
control. if the boolean variable is true I will insert the booking
else leave it by displaying the error. If inserted I will goto a
gridview showing all bookings from where I can do individual editing of
bookings( for this i am passing the booking id as querystring and will
go to the booking creation page itself).

The problem is even if i get true, in next button click event of
wizard, in finish _button click of wizard it is showing false. So
the allowed bookings are also not getting inserted . Any idea how to
handle this situation?
 
T

Tim Mackey

hi arun,
try a different approach. if the date is invalid, disable the next and
finish buttons and display the error message. presumably the user must not
be allowed to continue with an invalid date. they should probably cancel
the wizard if they can't enter a correct date. then if the user gets past
step 2, you don't need to go back and re-validate the date, there is no
logical reason to persist the boolean variable you refer to. if you have
proper validation in place, the user will never get to step 3 unless step 2
is successful.
however, if you really want to preserve the value of the boolean variable,
the explanation for why it is false is because you have taken no steps to
preserve its value across postbacks. remember that every postback is
essentially a single http request to the server. just because you put a
value into a variable of your page object, it does not mean that it will be
there for any future postbacks of the page, you must remember that as soon
as your code finishes executing on the server, that page object is
destroyed, along with all the values of its variables. asp.net makes sure
to preserve the state of the page between roundtrips via the viewstate.
with the technique i've outlined below, you can use the viewstate mechanism
to preserve your own values between postbacks.

you can put your boolean variable in viewstate at the end of step 2, and
then load it from viewstate in step 3 of your wizard.

e.g. pseudocode:

private void wizard1_NextButtonClick(etc...)
{
bool DateValid;
if(tabNumber == 2)
{
DateValid = whatever; // based on form contents...
ViewState["DateValid"] = DateValid; // save to viewstate
}
else if(tabNumber == 3)
{
// load from viewstate
DateValid =
Convert.ToBoolean(ViewState["DateValid"].ToString()); // you should check
for null value before doing this
if(DateValid)
blah blah blah
}
}


good luck
tim
 
A

arun

Hi Tim

Please suggest me how I can disable the (first and) next button of the
wizard. This when combined with displaysidebar=false will help me...so
that user cant forward further

Thank you very much for the information

Arun
 
T

Tim Mackey

hi arun,
i looked into further and apparently you can't disable the button as i
suggested.
what you can do is cancel the NextButtonClick if the input does not pass
your validation test.

protected void Wizard1_NextButtonClick(object sender,
WizardNavigationEventArgs e)
{
if(String.IsNullOrEmpty(this.TextBox1.Text))
{
e.Cancel = true; // do not permit the user to proceed
this.lblError.Text = "Please enter some text and try again";
}
}

i hope this does it for you
thanks
tim
 
A

arun

Hi Tim,

Thanks for the help....
From one of the web sites I got the following code

protected void Wizard1_NextButtonClick(object sender,
WizardNavigationEventArgs e)
{ WizardStepType t =
Wizard1.WizardSteps[e.CurrentStepIndex].StepType;
Response.Write(t.ToString());
}

And from the object brwser I got the possible values for WizardStepType
are

Auto,
Start,
Step,
Finish,
Complete

But when I run the above code always I am getting 'Auto '. Why is it so?
 
A

arun

Hi Tim,

Thanks for the help....

We can disable the next and previous buttons by the folowing code(you
have to convert the wizard step to stepnavigation or other templates )

if (Wizard1.ActiveStepIndex == 1)
{

Button NextButton =
(Button)(Wizard1.FindControl("StepNavigationTemplateContainerID$StepNextButton"));
NextButton.Enabled = false;
}
From one of the web sites I got the following code


protected void Wizard1_NextButtonClick(object sender,
WizardNavigationEventArgs e)
{ WizardStepType t =
Wizard1.WizardSteps[e.CurrentStepIndex].StepType;
Response.Write(t.ToString());
}

And from the object brwser I got the possible values for WizardStepType

are


Auto,
Start,
Step,
Finish,
Complete


But when I run the above code always I am getting 'Auto '. Why is it
so?
 
T

Tim Mackey

hi arun,
i presume that it says 'Auto' because you haven't explicitly set the type of
any of the steps.
i also presume that auto means the first step has no back button, and that
the last step says 'finish' instead of next etc., without you having to
configure the step behaviour individually.

personally i would prefer to just cancel the NextButtonClick event, rather
than dig into the controls to find the ''next' button and then disable it
etc. but you may think it's worth the extra work.

tim


Hi Tim,

Thanks for the help....

We can disable the next and previous buttons by the folowing code(you
have to convert the wizard step to stepnavigation or other templates )

if (Wizard1.ActiveStepIndex == 1)
{

Button NextButton =
(Button)(Wizard1.FindControl("StepNavigationTemplateContainerID$StepNextButton"));
NextButton.Enabled = false;
}
From one of the web sites I got the following code


protected void Wizard1_NextButtonClick(object sender,
WizardNavigationEventArgs e)
{ WizardStepType t =
Wizard1.WizardSteps[e.CurrentStepIndex].StepType;
Response.Write(t.ToString());
}

And from the object brwser I got the possible values for WizardStepType

are


Auto,
Start,
Step,
Finish,
Complete


But when I run the above code always I am getting 'Auto '. Why is it
so?
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top