C#

  • How to access a variable from the web.config file using C#

    The web.config file can be a handy place to put site wide settings like the website name or contact admin email address. The steps below will let you use your web.config file to store and retrieve those details. This example will show you how to set the website name in the web.config, create a class to access that name and display the name in page title.

    First up we need to store the values in the web.config file itself so open up your web.config file in your project and locate the <appSettings /> tag. You want your appSettings element to look something like this:

    <appSettings>
        <add key="SiteName" value="My New Site" />
     </appSettings>

    So now you have the name for your site set in the web.config. But how do we actually get this information into our pages? Well the good news is that it's quite easy! I'm going to make a new class called 'sitesettings.cs' in my App_Code folder and inside in that put the following code:

    using System;

    using System.Collections.Generic;

    using System.Web;

    //this will allow you to pickup the web.config values

    using System.Web.Configuration;

    ///

    /// Gets the website settings stored in the web.config file

    ///

    public class WebsiteSettings

    {

    public WebsiteSettings()

    {

    }

    //get the web site title

    public static string SiteTitle

    {

    get

    {

    return WebConfigurationManager.AppSettings["SiteName"];

    }

    }

    }

    Once you have the code above saved as a new class you can access the values within your .cs pages with ease. For example, if you want to print out the website name as a page title you simple need to include the following line in your code behind page (this example is for an aspx page that uses a MasterPage):

    protected void Page_Load(object sender, EventArgs e)

    {

    //line below will read the site name from webconfig

    //and display "My New Site - home page" in your browser window title

    Master.Page.Title = WebsiteSettings.SiteTitle + " - home page";

    }

    Obviously this is only a quick example of what you can do but it can be a handy way to store some of your site settings like the site name, url links, admin email address, etc.

  • Integrate Realex Redirect Payment option using C#

    We do a lot of payment integrations these days for online stores, charity sites, etc. One provider we do a lot of payment integrations with is Realex Payments. They offer a number of payment solutions for both clients and developers to choose from. The main types of setup are called Realex Redirect or Realex Remote. Unless you absolutely have a thirst for pain I would recommend the Redirect option.

    The advantages of choosing this setup are that Realex will handle all the credit card processing on their side so you don't need to worry about verifying the credit cards entered and they handle the notifications for 3Dsecure/Verified By VISA. The only downside is that, to date, the Redirect option does not allow you to do re-occurring payments. If you need that then you will be better off using the Remote option but be prepared to create all the 3Dsecure code yourself! 3D secure is not turned on by default either so make sure you ask for this option if required (I would highly recommend it to avoid costly charge backs!).

    The point of this article is to talk you through how to setup the Realex Redirect option within a C# application. From doing a few quick searches online there doesn't seem to be many examples out there and Realex themselves only provide a classic ASP example.

    C#
  • Solgar.ie goes live with Dragnet Systems New Online Store Software

    Solgar.ie launches

    Dragnet Systems Limited is proud to launch its new flagship online store software. Our software has a number of benefits to businesses:

    • No nonsense, simple to use interfaces for your customers and your site admin team.
    • Excellent reporting tools showing you what products were sold in any timeframe you choose, view all sales for the last 30 days, view all sales for the last year, etc.
    • Reminder emails automatically sent to customers. Useful technique to get your customers to return to your site.
    • Promotions tools giving you the flexibility to offer your customers discounts.
    • Auto image resizing for product images uploaded. you don't need to worry about resizing your images, our software will manage it all.
    • SEO friendly page links. Site fully optimized for Google, MSN, Yahoo, etc.
    • Email Marketing. Your customers can easily sign up, and opt out of email marketing on the site. You have total control over the email's sent to your customers. Includes filters to effectively targeting the right people.
    • Site optimized for speed. Sites load insanely fast and fully tested with over 100,000 products and orders in the system - it's fully robust!
    • And much much more!

    If you think you might be interested in taking our store software for a spin simply contact Dragnet Systems today and see how this software could help your online business succeed.

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

    This ends my shameless plug ;) Sorry about that, I'm just very excited about this new product!

  • How to fix an animated GIF not working in an ASP:Panel when using Internet Explorer

    If you want to use an animated gif within an ASP:Panel when using the ModalPopupExtender you might have noticed that there is an issue displaying this correctly in Internet Explorer. Everything will work as normal in IE except that if you look closely you should notice that the animated gif only runs through once. It doesn't continuously refresh the animation in Internet Explorer. Usually Modal Popups are used to display a friendly message to your users that the site is busy carrying out their request and the animation on a 'loading' graphic is a great way to give the impression that the site is site actually doing something.

    I will include 2 examples that show how to get an animated gif working in an asp:panel - one in a page using MasterPages and another in a regular aspx page. If you didn't know how to get modalboxes working in asp.net you should pick up some tips from this article too (hopefully!). By the end of this example you will have a page with a ModalBox that is called when a submit button is clicked on. I will assume that you have ajax.net setup on your page using the ModalPopupExtender control and that you have the asp:ScriptManager at the top of your page.

    C#
  • Use JQuery to add a simple watermark to your asp.net textbox

    The script below will let you use jQuery to place a simple watermark in your .net textbox. A good example would be a search box on your site where you want the word "search" in your textbox. When the user clicks into the textbox the word "search" will disappear.

    To get this up and running you just need to make sure you have jQuery (obviously) and then you add the following javascript within your <head> tag:

      The function should be easy to follow. Basically we are just checking the value of the textbox that has the class searchField on our page. If that textbox contains the word Search we are going to remove the word when the user is inside that textbox. The last part of the function is a check to ensure we don't wipe out the search term if the textbox contains a value that was entered by the user.

    So now that we have our javascript we just need to make our .net textbox. You will have seen in the above code that I am looking for a class called searchField so I just need to ensure my textbox control has this css class on it.

      <asp:TextBox ID="searchField" runat="server" CssClass="searchField" Text="Search" />

    And that's all there is to it!

  • Find a texbox control or label control on a master page from an inner page

    I noticed that this one seems to catch a good few people out - including myself when I was starting out with Master Pages. If you have an aspx page that sits in your Master Page you might have a need of populating a label or a textbox (or other control) with some value. To do this you can simply use the following code to tell .net to find your control on your master page. In the example below I am looking for a Literal control but you can easily change this to TextBox or Label or whatever you want:

    Literal ControlIDToFind = (Literal)this.Master.FindControl("ControlIDToFind");

    Just change the ControlIDToFind to the name of your ID you want to grab and then you can use it just like you would a normal .net control on your child page. So using the example above, if I wanted to output text into my ControlIDToFind literal which is in my MasterPage I would use the following code in my Child Page:

    ControlIDToFind .Text = "some text about how great you are!";

     It's only a small 2 line thing but you'd be surprised how often this catches people out. Below is a small list of items you can find using the above tips, hope it helps!

    //find a dropdown in your masterpage and get it's selected value

    DropDownList CountryList= (DropDownList)this.Master.FindControl("CountryList");

    string SelectedCountryList = CountryList.SelectedValue;

    //find a textbox in your masterpage and get it's value

    TextBox EmailAddress = (TextBox)this.Master.FindControl("EmailAddress ");

    string EmailAddress = EmailAddress.Text;

    //find a label in your masterpage and get it's value

    Label EmailAddress = (Label)this.Master.FindControl("EmailAddress");

    string EmailAddress = EmailAddress.Text;

    C#
  • Hide a div element and it's contents using c#

    This can be a nice little trick to do if you want to hide options on a user after an update or after so many products are selected, etc. Simply wrap the area you want to hide in a div element and put a runat="server" value into it's tag. Example:

     

    <div id="GridDiv" runat="server">

    gridview would go in here....

    </div>

     

    You can now easily set the div to hidden using a bit of CSS that can be set using C# whenever your condition is met. For example:

    GridDiv.Attributes["style"] = "display:none;";

    //OR you could just type

    GridDiv.visible = false;

    That's all there is to it but you can see how powerful this can be as it allows you to dynamically set the css class or styles you might want in your applications.

    C#
  • Dynamically set the Body Onload attribute when using MasterPages

    If you use MasterPages in your .Net website there will be a time when you want to load some javascript on a certain page and have it called OnLoad within the Body tag. A good example is when you use Google Maps.

    When setting up your Google Map you will need to place javascript in the header of your page and you also need to set the Body OnLoad and UnLoad attributes. You could place the javascript in the MasterPage and be done with it but that is such a waste as usually you only want the Map to show on a contact page or similar. So how do you set the onBody attribute from within a child page?

    C#
  • how to read data from an Excelsheet using C#

    I had to use the script below to read in values from an Excel sheet using C# recently. The script itself is generic enough but it has one benefit - you don't need to know the name of the sheet you want to import. This can be very beneficial if you have people supplying you with Excel sheets as everyone has their own way of saving sheet names.

    The code in this example will take your Excel sheet and read in the first 5 columns. It will also loop through any other sheet and read in the first 5 columns in those too.

    C#
  • How to know which button is clicked on when using a repeater C#

    If you're developing websites, sooner or later you will be asked to produce a list of data where each row will need to have a button on it that performs a function. If you need to do this in a Repeater and are wondering how to achieve this then read on.

    For this example I will assume you know what a repeater is and are familiar with how to binding data to it from a database or similar.

    In your page you would have a repeater. Below is a sample that I'm going to use for this example:

    <asp:Repeater ID="MyRepeater" OnItemCommand="WhatToDo_ItemCommand" runat="server">
    <ItemTemplate>
    <ul>
        <li><%# Eval("ItemName")%></li>
        <li><%# Eval("ItemDetails")%></li>
        <li><asp:LinkButton ID="DeleteItem" CommandArgument='<%# Eval("ItemID") %>' CommandName="DeleteThisItem" runat="server">delete this item</asp:LinkButton></li>
        <li><asp:LinkButton ID="UpdateItem" CommandArgument='<%# Eval("ItemID") %>' CommandName="UpdateThisItem" runat="server">update this item</asp:LinkButton></li>
    </ul>
    </ItemTemplate>
    </asp:Repeater>

    Nothing to out of the ordinary here. There are a few things to note. In your <asp:repeater> tag you must include the class you want to call when the button is clicked. This is the OnItemCommand="WhatToDo_ItemCommand" bit. You will see in a few mins where this is used. Next up is the code that puts the ID value into the buttons you're going to use. CommandArgument holds the value for your command, in this case it's going to hold our database row ID and CommandName is needed so that our code will know which button made the call.

    Next up is the code behind page:

    C#

Get In Touch

Follow me online at TwitterFacebook or Flickr.

Latest Tweets