call today Call Today 320.281.0605

Feedback tab widget in ASP.NET

February 3, 2010    Author: Joel

I recently had a client that wanted a Feedback tab that you find on a lot of sites now a days.  Something similar to Get Satisfaction or uservoice.

feedback

I couldn’t find anything out there that I liked so here is what I came up with. The approach I am about to describe will place the Feedback tab on every page that is using your Master Page.

I am using the ASP.NET AJAX Control Toolkit for the modal.

First we need to set the modal up.

  1. Make sure you have the AjaxControlToolkit assembly in your project references.
  2. Add the page directive for the Control Kit at the top of your Master Page:
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
    
  3. Add the ScriptManager to your Master Page:
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    
  4. Place the Feedback tab on your site by adding it as an ASP.NET ImageButton and adding a cssclass to it:
    <asp:ImageButton ID="lbFeedback" CssClass="tab" runat="server" ImageUrl="images/feedback.gif" />
    
  5. Add this styling to your css stylesheet:
    .tab{position: fixed; right: 0; top: 250px;}
    

    This will position the tab on the right of the browser window.

  6. Now add the Feedback form inside a panel that will appear in the modal to the bottom of your Master Page just before the </form>.
    <asp:Panel ID="pnlModal" runat="server" CssClass="modal">
            <p>Thank you for taking the time to leave us
                feedback. Good or bad we want to know how your experience was at our
                establishment.</p>
            <p><label>Name:</label><br /><asp:TextBox ID="txtName" runat="server" /></p>
            <p><label>Email:</label><br /><asp:TextBox ID="txtEmail" runat="server" /></p>
            <p><label>Message:</label><br /><asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Columns="40" Rows="10" /></p>
            <asp:Button ID="btnSubmit" runat="server" Text="Submit"
                onclick="btnSubmit_Click" />
            <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
        </asp:Panel>
    
  7. Add the Modal Popup Extender control to handle the modal:
    <ajaxToolkit:ModalPopupExtender ID="mpe" runat="server" TargetControlID="lbFeedback" PopupControlID="pnlModal" BackgroundCssClass="modalbg" CancelControlID="btnCancel"></ajaxToolkit:ModalPopupExtender>
    
  8. Lets add a Thank you message that is only visible after the feedback form is submitted. I add this at the top of the Master Page:
    <div id="thankyou" runat="server" visible="false" class="thankyoumessage">Thank you for leaving feedback</div>
    

We now have a non-functioning Feedback form appearing inside a modal when the Feedback tab is clicked.  A cancel button is there if they decide not too leave feedback after all.

By adding the ASP.NET Button control in the modal it will do a PostBack back to the page that can be handled in the Master Page’s code-behind.  In the button handler I add the logic to simply email the submitted form to a desired email.  See my previous post on how to do that.

thankyou

At the end of the Submit Button handler I have a Page Redirect that sends the visitor back to the page they are on.  Two reasons for this:

  1. In the redirect I pass a QueryString param to the page letting it know that Feedback has been submitted.  In the PageLoad of the Master Page I check for this param and if it is present display a Thank You at the top of the page.
  2. Secondly I do a redirect so the visitor won’t do a refresh to get ride of the Thank You message, thus re-sending the feedback info again.
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack && !String.IsNullOrEmpty(Request.QueryString["fb"]))
        {
            // the feedback form has been filled out, display a thank you message.
            thankyou.Visible = true;
        }
    }

    /// <summary>
    /// Handle the feedback form submit
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        // add the logic to handle the submited form fields

        string currentPage = Request.Path;
        Response.Redirect(currentPage + "?fb=1");
    }

To sharpen it up a bit some final touches in the style sheet for the modal and the thank you message:

.thankyoumessage{background-color:#cc0000; color:#fff; padding:20px; font-size:3em; text-align:center}
.modal{background-color:#fff; border:solid 3px gray; padding:8px; width:350px; height:450px;}
.modalbg{filter: Alpha(Opacity=70); -moz-opacity:0.7; opacity: 0.7; width: 100%; height: 100%; background-color: #000000; position: absolute; z-index: 500; top: 0px; left: 0px;}

I’ve put together a simple example website with all this working for download here.

There certainly is more that can be added upon this such as making the thank you message only visible for 3 seconds.  But, I think this is a good foundation.

Share

Sending email on Rackspace Cloud using asp.net

January 20, 2010    Author: Joel

I have hosted with the Rackspace Cloud now for about two years.  I really couldn’t be happier with them and would recommend them to anyone.  The one thing I have seemed to have issues with is sending emails from my web applications. After talking with support and doing some testing I have a solution that should last.

Here is an example of a basic contact form.

First set the smtp information in the web.config

<system.net>
 <mailSettings>
 <smtp>
 <network host="mail.emailsrvr.com" userName="info@mydomain.com" password="mypassword" port="25"/>
 </smtp>
 </mailSettings>
 </system.net>

I put this at the very bottom of the web.config just before the closing tag.

Note that the userName and password must be of a valid email address of the domain you are sending from. Make sure to set this up through their control panel.

I also add in the appSettings of the web.config the email addresses of whom to send it to and the subject line. Just best practices.

<appSettings>
 <add key="mailTo" value="joel@mydomain.com"/>
 <add key="mailSubject" value="Contact Form inquiry"/>
 </appSettings>

Now my example is of an asp.net web application in c#. I am going to assume everyone knows how to setup the page, lets go to the code-behind.

You will need to add these three lines to your using block.

using System.Web.Configuration;
using System.Text;
using System.Net.Mail;
The submit button handler:
 /// <summary>
 /// Handles the contact form submit
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void imtbtnSubmit_Click(object sender, ImageClickEventArgs e)
 {
 StringBuilder body = new StringBuilder();
 body.AppendFormat("Company Name: {0}{1}", txtCompany.Text, Environment.NewLine);
 body.AppendFormat("Contact Name: {0}{1}", txtName.Text, Environment.NewLine);
 body.AppendFormat("Phone: {0}{1}", txtPhone.Text, Environment.NewLine);
 body.AppendFormat("Email: {0}{1}", txtEmail.Text, Environment.NewLine);
 body.AppendLine();
 body.AppendLine("Message");
 body.AppendLine(txtMessage.Text);

 MailMessage message = new MailMessage(txtEmail.Text, WebConfigurationManager.AppSettings["mailTo"], WebConfigurationManager.AppSettings["mailSubject"], body.ToString());

 SmtpClient client = new SmtpClient();
 client.Send(message);

 pnlForm.Visible = false;
 lblMessage.Visible = true;
 }

Pretty simple stuff, the import part is system.net in the web.config, making sure you have the correct host and port  and a valid email.

You would think they would have this in their knowledge base, but as of this writing they don’t. I hope this helps someone.

Share

Keep mulitple ListViews on the same page.

December 15, 2009    Author: Joel

I just had a project that required me to have a list of articles both in the the left nav and main content area.

In the left nav is was just a list of the titles.  The titles were links to anchors in the main page since the actual articles were a bit lengthy.

The client only wanted 5 articles to be displayed per page.  So paging was needed.  I used two separate ListViews to display the news items.  Along with that each ListView had a  DataPager to handle the paging.

The trick was if a user clicked on page 2 in the left nav how would we update the main content ListView to move to page 2 as well?

To accomplish this I accessed the PagePropertiesChanging handler and updated both DataPager PageProperties.

So if a user clicked in the left nav the code-behind is:

protected void lvLeftNav_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    dpNews.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    dpLeftNav.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    BindNews();
 }

As you see I update both DataPagers.

Here is the code-behind for the main news content code-behind:

protected void lvNews_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
    dpNews.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    dpLeftNav.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    BindNews();
}

I should mention that the BindNews() method handles binding both the Left Nav and Main News to the same News List<News>.

Share

Search Results – Webpage has expired on back button

December 8, 2009    Author: Joel

search

I’ve been asked this a few times now so I thought I would blog my solution to point others to this next time I am asked.

Often times people will create a simple search on their site. The most obvious way to do this is with a simple textbox and button.  You then handle the search when the button is clicked in the code-behind.

It may look something like this:

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(txtSearch.Text.Trim()))
        {
            BindSearch(txtSearch.Text.Trim());
        }
    }

Pretty straight forward.  The problem here is you’re doing a postback to the same page.  You won’t have a history of the previous searches in your browser. After doing a search and you want to see what you had for results from the previous search and simply hit back in your browser you’ll see this

expired

So what I have always done to avoid this is to simply do a Response.Redirect to the page instead of just calling the BindSearch() method and put the search term in the query string.

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(txtSearch.Text.Trim()))
        {
            Response.Redirect("/Search.aspx?search=" + txtSearch.Text.Trim());
        }
    }

This is a pretty simple solution, does anyone else have a different way to handle this?

Share

Controls not being registered in designer.cs

August 3, 2009    Author: Joel

I’ve encountered this a few times now.

When dealing with a webform with an extraordinary amount of controls, ie. textboxes, dropdownlists, panels, etc. I’ve noticed that after x amount of controls have been added they are no longer registered in the designer.cs file. 

I notice this by first the control not showing up in intellisense when trying to use it in code-behind.  If I manually type it out I will still get a build error. 

So, at this point I have to manually register any new controls in the designer.cs file.  At the top of the file it says,

//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.

However it seems that they are never lost and now everything works fine.  I am not sure if this is normal behavior and this is what you are suppose to do, but so far it has worked for me.

Has anyone else seen this, does anyone know the exact amount of controls that you can add to a page before they are no longer registered in the designer.cs?

Share
Agent Cody Banks 2 Destination London full lenght movie download Afghan Knights download movie Inside the Smiths download movie Beverly Hills Chihuahua download movie Permanent Vacation download movie Indiana Jones and the Kingdom of the Crystal Skull download movie Afghan Knights download movie Inside the Smiths download movie Beverly Hills Chihuahua download movie Permanent Vacation download movie Indiana Jones and the Kingdom of the Crystal Skull download movie