PDA

View Full Version : checkboxes on form - The name 'brochure' does not exist in the current context


tempest4000
11-25-2009, 02:21 PM
I have been asked to amend a form written asp.net even though Im a PHP developer and have never used asp.net before!

I have added two new checkboxes to the form on the HTML page as below:

<input type="checkbox" id="brochure" runat="server" name="brochure" value="brochure" style="width:15px;height:15px;margin:0 10px 0 0 " />Please tick to request our brochure</p>
<input type="checkbox" id="furtherInfo" name="furtherInfo" runat="server" value="further information" style="width:15px;height:15px;margin:0 10px 0 0 " />Please tick if you do not wish to receive further information from us</p>


On the backend .cs page which does all the processing I have:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Net.Mail;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Yucca.Pentire.Dal;
using Yucca.Pentire.Utils;

public partial class contact : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{
Master.UpdateBodyTag("contactus");
Master.UpdateMetaDetails("contact");

PhoneTracking.ImageSwap();
phonenumber.Text = PhoneTracking.HeaderPhone;

submitbutton.Click += new ImageClickEventHandler(submitbutton_Click);

}

void submitbutton_Click(object sender, ImageClickEventArgs e)
{

if (ValdiateAllFields())
{

MailMessage msg = new MailMessage();

SmtpClient smtp = Smtp.CreateSMTP(1);

//CheckBox brochure = (CheckBox) PreviousPage.FindControl("brochure");

string strBody;

strBody = "<strong>Name: </strong>" + name.Value + "<br />";
strBody += "<strong>Email: </strong>" + email.Value + "<br />";
strBody += "<strong>Telephone: </strong>" + telephone.Value + "<br />";
strBody += "<strong>Address: </strong>" + address1.Value + "<br />";
strBody += "<strong>Address: </strong>" + address2.Value + "<br />";
strBody += "<strong>Address: </strong>" + address3.Value + "<br />";
strBody += "<strong>Address: </strong>" + towncity.Value + "<br />";
strBody += "<strong>Address: </strong>" + county.Value + "<br />";
strBody += "<strong>Address: </strong>" + postcode.Value + "<br /><br />";

strBody += "<strong>Comments: </strong>" + comments.Value + "<br /><br />";

//strBody += "<strong>Brochure: </strong>" + brochure.Value + "<br /><br />";
// strBody += "<strong>Further Info: </strong>" + furtherInfo.Value + "<br /><br />";


//msg.From = new MailAddress("bookings@pentirehaven.co.uk");
//msg.To.Add(new MailAddress("bookings@pentirehaven.co.uk"));
msg.To.Add(new MailAddress("leannetempest@yahoo.co.uk"));
msg.Subject = "Enquiry";
msg.Body = strBody;
msg.IsBodyHtml = true;

smtp.Send(msg);

headersentmessage.Visible = true;
Response.Redirect("~/contact_amended.aspx");

}
}

This works unless I uncomment out the lines of code relating to the two new checkboxes and I get the error message:
CS0103: The name 'brochure' does not exist in the current context


Can anyone help? Many thanks...

Freon22
11-25-2009, 03:40 PM
A checkbox just returns a boolean value also you are coding it with html not asp. So your check box should look like this.

<asp:CheckBox ID="brochure" runat="server" />

Then on your next page in the code behind .cs you can check to see if it has been checked or not.

CheckBox mytest = (CheckBox)PreviousPage.FindControl("brochure");
if (mytest.Checked)
{
Label1.Text = "Its checked";
}
else
{
Label1.Text = "Its Not checked";
}

tempest4000
11-25-2009, 04:03 PM
I had the form checkbox as HTML as all the other form text input fields are and they work.

I now have an issue with the code you supplied

CheckBox chk1 = (CheckBox)PreviousPage.FindControl("brochure");

I get error:

Details: System.NullReferenceException: Object reference not set to an instance of an object.

:confused:

Freon22
11-25-2009, 04:57 PM
Are you using a form from one page to send the information to another page to send out an email? If so then is that page a aspx page?

Or are you just using the code behind of the form page to send out the email? If so then you would not use (CheckBox)PreviousPage.FindControl("brochure"); because there is no previous page.

You may need to explain more or show more code.

Edit: wow I have been checking and you must be using an old ver of asp.net if so then why can't you just remove the (CheckBox)PreviousPage.FindControl("brochure"); since you don't need it and pick up the values of the checkboxies like you did in the rest of the code.

<input type="checkbox" id="brochure" runat="server" name="brochure" value="brochure" style="width:15px;height:15px;margin:0 10px 0 0 " />Please tick to request our brochure</p>
<input type="checkbox" id="furtherInfo" name="furtherInfo" runat="server" value="further information" style="width:15px;height:15px;margin:0 10px 0 0 " />Please tick if you do not wish to receive further information from us</p>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Net.Mail;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Yucca.Pentire.Dal;
using Yucca.Pentire.Utils;

public partial class contact : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{
Master.UpdateBodyTag("contactus");
Master.UpdateMetaDetails("contact");

PhoneTracking.ImageSwap();
phonenumber.Text = PhoneTracking.HeaderPhone;

submitbutton.Click += new ImageClickEventHandler(submitbutton_Click);

}

void submitbutton_Click(object sender, ImageClickEventArgs e)
{

if (ValdiateAllFields())
{

MailMessage msg = new MailMessage();

SmtpClient smtp = Smtp.CreateSMTP(1);

*******************************************************
//CheckBox brochure = (CheckBox) PreviousPage.FindControl("brochure"); Remove this
*********************************************************
string strBody;

strBody = "<strong>Name: </strong>" + name.Value + "<br />";
strBody += "<strong>Email: </strong>" + email.Value + "<br />";
strBody += "<strong>Telephone: </strong>" + telephone.Value + "<br />";
strBody += "<strong>Address: </strong>" + address1.Value + "<br />";
strBody += "<strong>Address: </strong>" + address2.Value + "<br />";
strBody += "<strong>Address: </strong>" + address3.Value + "<br />";
strBody += "<strong>Address: </strong>" + towncity.Value + "<br />";
strBody += "<strong>Address: </strong>" + county.Value + "<br />";
strBody += "<strong>Address: </strong>" + postcode.Value + "<br /><br />";

strBody += "<strong>Comments: </strong>" + comments.Value + "<br /><br />";
****************************************************************************
strBody += "<strong>Brochure: </strong>" + brochure.Value + "<br /><br />"; uncomment these two
strBody += "<strong>Further Info: </strong>" + furtherInfo.Value + "<br /><br />";
******************************************************************************

msg.From = new MailAddress("bookings@pentirehaven.co.uk");
msg.To.Add(new MailAddress("bookings@pentirehaven.co.uk"));
msg.To.Add(new MailAddress("leannetempest@yahoo.co.uk"));
msg.Subject = "Enquiry";
msg.Body = strBody;
msg.IsBodyHtml = true;

smtp.Send(msg);

headersentmessage.Visible = true;
Response.Redirect("~/contact_amended.aspx");

}
}

The only problem I see with this is brochure.Value will always give the value brochure even if its not checked. So you may need to add something like this

string myBrochure = "";
if (brochure.Checked)
{
myBrochure = brochure.Value;
}

Now instead of using brochure.Value in our email script use myBrouchure, this way if its not checked it will be an empty string.