Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 17th, 2008, 02:04 AM
Friend of Wrox
Points: 1,749, Level: 16
Points: 1,749, Level: 16 Points: 1,749, Level: 16 Points: 1,749, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2007
Location: San Diego, CA, USA.
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default user control code

I'm building a contact form as a user control so I can reuse it throughout my site. My demo page is at...

http://beta.earthchronicle.com/EC/Ma...6LiveDemo.aspx

The page load runs, and the label text is set to "Ask Us!" When the button is clicked though, I do not receive an email, nor is the label text changed to "Sent." It was originally processing on the btnEmail_Click event. I thought this might not be working because in the .ascx file AutoEventWireup is set to true.

Code:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ContactForm.ascx.cs" Inherits="ContactForm" %>
The id of the control is being altered because it's part of a user control on a master page. So I tried changing the event to ctl00_compContactForm_btnEmail_Click, but that doesn't seem to have changed anything.

Here's the code I'm using in the ContactForm.ascx.cs file. I believe this is the correct place to insert C# for processing the form contained in ContactForm.ascx, right?

Any suggestions that I might test next?

Code:
public partial class ContactForm : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //testing to see if the page works
        this.lblSent.Text = "Ask Us!";
    }

    protected void btnEmail_Click(object sender, EventArgs e)
    {
        // Declaring necessary mail variables
        MailMessage objMail = new MailMessage(); // the email (technically mail message)
        NetworkCredential objCredentials = new NetworkCredential("username", "password"); // the authentication credentials
        SmtpClient objSend = new SmtpClient("SMTP", 25); // the SMTP account
        Uri objUrl = Request.Url; // the URL of the current webpage (this subroutine will run on every page of the website, so we don't know if we don't go grab it)

        // the email configuration information: sender, recipients, subject
        objMail.From = new MailAddress("Admin <from@earthchronicle.com>");
        objMail.To.Add(new MailAddress("to@earthchronicle.com"));
        objMail.To.Add(new MailAddress("support@earthchronicle.com"));
        objMail.Subject = "I have a question or comment.";
        objMail.IsBodyHtml = true;

        // the body of the email
        objMail.Body = "<p>Hi!</p>";
        objMail.Body += "<p>You have a message from an Earth Chronicle visitor. They responded to ";
        objMail.Body += "<a href='" + objUrl.AbsoluteUri + "'>" + objUrl.AbsoluteUri + "</a></p>";
        objMail.Body += "<p>Their email is: " + this.tbEmail.Text + "<br />";
        objMail.Body += "They write:" + this.tbComments.Text + "</p>";
        objMail.Body += "<p>TTFN,<br />the Server</p>";

        // SMTP configuration information and use the Send method
        objSend.UseDefaultCredentials = false;
        objSend.Credentials = objCredentials;
        objSend.Send(objMail);

        // notify the visitor that the procedure has successfully completed
        this.lblSent.Text = "Sent.";
    }
}
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
  #2 (permalink)  
Old September 17th, 2008, 03:34 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Do you have an onclick handler on the button the markup?|

AutoEventWire up only works for known objects like page; not a button. So, simply define onclick="YourMethod" and define a YourMethod in code behind...

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
  #3 (permalink)  
Old September 17th, 2008, 08:20 PM
Friend of Wrox
Points: 1,749, Level: 16
Points: 1,749, Level: 16 Points: 1,749, Level: 16 Points: 1,749, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2007
Location: San Diego, CA, USA.
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

NO! OK, that would be it. Thanks.

Is it better practice to have AutoEventWireup false and define all events by onclicks? That would seem to be more consistent. What are the arguments for using AutoEventWireup true?

Thanks Imar!

-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
  #4 (permalink)  
Old September 17th, 2008, 09:01 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

There's a possibility that explicitly defining the event handlers could have a slight performance gain. I imagine auto event wireup is implemented through reflection. If you turn it off, then the runtime doesn't need to reflect the class to find the handlers that should be wired up. So you would gain a bit from that, but frankly it would be negligible compared to other things you can do to optimize an application, such as query tuning or data caching.

-Peter
compiledthoughts.com
  #5 (permalink)  
Old September 17th, 2008, 10:17 PM
Friend of Wrox
Points: 1,749, Level: 16
Points: 1,749, Level: 16 Points: 1,749, Level: 16 Points: 1,749, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2007
Location: San Diego, CA, USA.
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

Thanks.

I'd assumed that Imar meant that only controls were picked up, not HTML tags, however my button was using runat="server" so I guess not. Is it because it's in a user control and has to be pulled in from outside the page in that sense? If AutoEvenWireup is true, what determines which objects are "synched" and which are not?

-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
  #6 (permalink)  
Old September 18th, 2008, 01:27 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

No, I didn't mean server controls versus client (HTML) controls: I mean System.Web.UI.Page versus controls in the System.Web.UI.Control / WebControls / HtmlControls namespaces: AutoEventWireUp only applies to the Page class itself, not to controls defined in that page.

So, it picks up Page_Load, Page_PreInit and so on. However, it does not pick up a Click event of a Button or a SelectedIndexChanged of a DropDownList.

For these controls in your page, you need the += syntax in C# or the Handles keyword in VB.NET to wire up the method. Alternatively, you can define the handler in markup (onclick="SomeMethod") and let .NET take care of wiring up the method.

For more info: http://odetocode.com/Blogs/scott/arc...2/16/2914.aspx

Hope this helps,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
  #7 (permalink)  
Old September 19th, 2008, 11:58 PM
Friend of Wrox
Points: 1,749, Level: 16
Points: 1,749, Level: 16 Points: 1,749, Level: 16 Points: 1,749, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2007
Location: San Diego, CA, USA.
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

It finally gives me something to read, sometimes Google answers all my questions, and sometimes I can't find the right keywords for beans. I knew that VWD express defaults to using the OnX= format in markup, but didn't understand very well why. Is that really best practice? It looks like handling it in the user control's code behind would be better for code separation, correct? If so, it sounds like I write my function in the code behind as normal, name it whatever, say "Fred". Now from what I've found, if I want to run Fred on Page load I'd set AutoEventWireup="false" in the .ascx and add to the code behind...

Code:
public ContactForm() 
   {
      Load += new EventHandler(Fred);
   }
But what about activating Fred from a control event? I got errors when I tried...

Code:
public ContactForm() 
   {
      btnEmail.Click += new EventHandler(Fred);
   }
and adjusting for the id changes just generated different errors...
ctl00_compContactForm_btnEmail.Click += new EventHandler(Fred);

http://beta.earthchronicle.com/ECBet...ngItAllUp.aspx


-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
  #8 (permalink)  
Old September 20th, 2008, 04:46 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

If btnEmail is defined in the markup section of the page, it should be accessible by the code behind. Try this:

1. Go to your ContactForm constructor.

2. Type:

  btnEmail.Click +=

3. Press Tab twice.

VWD inserts the click handler and the method to handle the click with a default NotImplementedException as the implementation.

You *never* need names like ctl00_compContactForm in your code behind. These names are the client IDs for the controls accessible to HTML / CSS / JacaScript. Server side, the control is simple named compContactForm. If you need to access controls in a repeating control or another container (e.g. GridView, DetailsView, Login ) you need to use FindControl to get a reference to the relevant control.

Hope this helps; if not, can you post the code for the control?

BTW I am not sure if using On... breaks good practices. The controls in the markup needs to be wired up to a method somehow. Using On... is easier as it's done automatically for you. Manually wiring up[ handler means, IMO, just more work.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
  #9 (permalink)  
Old September 24th, 2008, 12:28 AM
Friend of Wrox
Points: 1,749, Level: 16
Points: 1,749, Level: 16 Points: 1,749, Level: 16 Points: 1,749, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2007
Location: San Diego, CA, USA.
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

Thanks for all your help guys. We're making progress. After my last post, I tried to go simple and just run the load event using +=, and it blew up in my face. I played with it and now have that working so partial Yea. I can even get the page up, if I use the OnClick attribute for the button and reference my EmailComments function, good. That blows up when I actually push the button, but one thing at a time. I think it's just about me talking nicer to the mail server anyway.

Back to +=, I entered the event handler as you indicated (using double tab) and tried leaving the exception but that threw an error (not the right one), so I replaced it with the code from my EmailComments function and it generates the same error as the default exception. Here's the link so you can check it out, I also set <compilation debug="true" /> on in the web.config if that helps.

http://beta.earthchronicle.com/ECBet...ngItAllUp.aspx

Not sure if this confirms anything, but when I first uploaded this code, I'd forgotten to remove OnClick="EmailComments" from the .ascx file. It threw an error (I'd commented out that function in the .ascs.cs), before it got to the problem it's complaining about now.

As requested here's the code for the contact form control.

Code:
<%@ Control Language="C#" AutoEventWireup="false" CodeFile="ContactForm.ascx.cs" Inherits="ContactForm" %>



<div id="contactFormWrapper">
    <div id="contactForm" class="contactForm">

    <img id="contactImage" alt="Contact Us!" height="150" width="150" name="rolloverImage" tabindex="0" src="~/ECBeta/Images/ContactUsBox.jpg" runat="server" />

        <h2>Have a Question? Leave a comment!</h2>

        <h4>Add an update, make a suggestion, or report an error.</h4>

        <fieldset>


            <legend>Contact Us!</legend>

            <ol>
                <li>
                    <label for="ctl00_compContactForm_tbEmail">Email (optional)</label>
                    <asp:textbox id="tbEmail" runat="server" cssclass="tbEmail"></asp:textbox>
                </li>
                <li>
                    <label for="ctl00_compContactForm_tbComments">Questions and Comments</label>
                    <asp:textbox id="tbComments" runat="server" cssclass="tbComments" textmode="multiLine"></asp:textbox>
                </li>
                <li>
                    <asp:button id="btnEmail" runat="server" text="Email Us!" CssClass="btnEmail" />
                    <asp:Label ID="lblSent" runat="server"></asp:Label>            
                </li>
            </ol>

        </fieldset>

    </div>
</div>

<div id="contactFormSpacer" class="spacer"></div>
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
  #10 (permalink)  
Old September 24th, 2008, 01:49 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

That exception is just a placeholder in case you forget to implement the method. You should always remove it.

Are you sure this is all the code? Where do you call this? In an event like Page_Load or Page_PreInit? In that case, take a look at this:
Code:
<%@ Control Language="C#" AutoEventWireup="false"
You have this turned off, so the standard page events won't fire.

I can make this work with the attribute set to true and the following code in Code Behind:
Code:
protected void Page_Init(object sender, EventArgs e)
{
 btnEmail.Click += new EventHandler(btnEmail_Click);
}
But what I don't get is why you're trying to make this so difficult. Why not just follow this route:

1. Add a new user control to the site

2. Switch to Design View

3. Add a button

4. Double click the button and write your code in the event handler that VS created (and wired up) for you?

A lot easier, if you ask me...

Hope this helps,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add Windows User control in Web User Control agarwalvidhu C# 0 March 30th, 2006 01:17 AM
user control / code behind question whyulil General .NET 3 January 11th, 2005 11:17 AM
Loading user control dynamically from code-behind jacob ASP.NET 1.0 and 1.1 Basics 2 April 12th, 2004 06:06 PM
User Control / Code-behind ViagraFalls ASP.NET 1.0 and 1.1 Basics 3 November 1st, 2003 09:18 PM



All times are GMT -4. The time now is 09:00 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.