Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    checkboxes on form - The name 'brochure' does not exist in the current context

    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:

    Code:
    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...

  • #2
    Regular Coder Freon22's Avatar
    Join Date
    May 2005
    Location
    USA
    Posts
    287
    Thanks
    3
    Thanked 5 Times in 5 Posts
    A checkbox just returns a boolean value also you are coding it with html not asp. So your check box should look like this.
    Code:
    <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.
    Code:
    CheckBox mytest = (CheckBox)PreviousPage.FindControl("brochure");
            if (mytest.Checked)
            {
                Label1.Text = "Its checked";
            }
            else
            {
                Label1.Text = "Its Not checked";
            }
    I was wondering why frisbees got bigger as they got closer, then it hit me.

  • #3
    New Coder
    Join Date
    Feb 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    Code:
    CheckBox chk1 = (CheckBox)PreviousPage.FindControl("brochure");
    I get error:

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

  • #4
    Regular Coder Freon22's Avatar
    Join Date
    May 2005
    Location
    USA
    Posts
    287
    Thanks
    3
    Thanked 5 Times in 5 Posts
    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.
    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>
    Code:
    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
    Code:
    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.
    Last edited by Freon22; 11-25-2009 at 07:40 PM.
    I was wondering why frisbees got bigger as they got closer, then it hit me.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •