Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP: Passing a checkbox

Status
Not open for further replies.

dkaplan

Programmer
Jan 29, 2001
98
US
I am trying to pass a control as a session variable to a new page.
In the new page, the value of the control is added as a paramneter to an sql command.
This seems to work with textboxes, but not checkboxes.

in the passing page I have:

....
Session["MyTextBox"] = Request["MyTextBox"];
Session["MyCheckBox"] = Request["MyCheckBox"];
...

It is passed to a new page using:
Response.Redirect("NewPage.aspx", true);

In the new page I have

p = new SqlParameter("@MyTextBox", SqlDbType.VarChar, 10, "MyTextBox");
p.Value = Session["MyTextBox"];
cmd.Parameters.Add(p);

p = new SqlParameter("@MyCheckBox", SqlDbType.Bit, 1, "MyCheckBox");
p.Value = Session["Convert.ToBoolean(MyCheckBox)"];
cmd.Parameters.Add(p);

The TextBox works fine.

The checkbox does not trow an error, but I do not seem to be getting the boolean value in the parameters. The result acts as if the value is false, even when the passed checkbox is checked.

Any ideas?

 
You should always cast your session vars to the appropriate type of object first. Try something like this:
Code:
p.Value = DirectCast(Session("MyCheckBox"), CheckBox).Checked
 
why pass the entire control. you should get the value(s) you need from the control and pass them, but not the entire control.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Thank you both, Jbenson & jmeckly.

Both suggestions got me in the right direction.

This had been driving me nuts for days.

I finally ended up handling the value before it was passed.

if (this.MyCheckBox.Checked == true)

Session["MyCheckBox"] = 1;
else
Session["MyCheckBox"] = 0;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top