Beanie posted on December 8, 2009 08:47

I needed to display a list, countries in this case, over two columns. It is relatively simple using record set paging, in its simplest form you count the total rows divide by the number of columns you want then set the page size to the result of that calculation. Then instead of button to scroll through the record set we have a repeater for each page – simples ;-).

ASPX

   1:  <div id="CountryList1" style="float:left; width:40%">
   2:      <asp:Repeater ID="rptCountryList1" runat="server">
   3:          <ItemTemplate>
   4:              <li>
   5:                  <a href="Description.aspx?country=<%# DataBinder.Eval(Container.DataItem, "fldCountryID") %>">
   6:                      <%# DataBinder.Eval(Container.DataItem, "fldCountry") %>
   7:                  </a>
   8:              </li>
   9:          </ItemTemplate>
  10:      </asp:Repeater>
  11:  </div>
  12:   
  13:  <div id="CountryList2" style="float:left; width:40%">
  14:      <asp:Repeater ID="rptCountryList2" runat="server">
  15:          <ItemTemplate>
  16:              <li>
  17:                  <a href="Description.aspx?country=<%# DataBinder.Eval(Container.DataItem, "fldCountryID") %>">
  18:                      <%# DataBinder.Eval(Container.DataItem, "fldCountry") %>
  19:                  </a>
  20:              </li>
  21:          </ItemTemplate>
  22:      </asp:Repeater>
  23:  </div>

 

Code behind

   1:  using System;
   2:  using System.Web;
   3:  using System.Web.Security;
   4:  using System.Web.UI;
   5:  using System.Web.UI.WebControls;
   6:  using System.Web.UI.WebControls.WebParts;
   7:  using System.Web.UI.HtmlControls;
   8:  using System.Data.SqlClient;
   9:  using System.Data;
  10:  using System.Configuration;
  11:   
  12:  public partial class page_name : System.Web.UI.Page
  13:  {
  14:      SqlConnection scon=new  SqlConnection(ConfigurationManager.AppSettings["strConn"]);
  15:      SqlDataAdapter sDA;
  16:      DataSet dsCountryList1;
  17:      DataSet dsCountryList2;
  18:   
  19:      const int intPages = 2;
  20:      int intRows;
  21:      int rowSum;
  22:   
  23:      private void BindData()
  24:      {
  25:          rptCountryList1.DataSource = dsCountryList1;
  26:          rptCountryList1.DataBind();
  27:          rptCountryList2.DataSource = dsCountryList2;
  28:          rptCountryList2.DataBind();
  29:      }
  30:   
  31:      private void readpage(int n)
  32:      {
  33:          SqlCommand cmd = new SqlCommand("SelectCountriesByLetter", scon);
  34:          cmd.CommandType = CommandType.StoredProcedure;
  35:          cmd.Parameters.Add(new SqlParameter("@Product", 10));
  36:          cmd.Parameters.Add(new SqlParameter("@Letter", "%"));
  37:          sDA = new SqlDataAdapter(cmd);
  38:          dsCountryList1 = new DataSet();
  39:          dsCountryList1.Clear();
  40:          sDA.Fill(dsCountryList1, 0, intRows, "tblCountry");
  41:          dsCountryList2 = new DataSet();
  42:          dsCountryList2.Clear();
  43:          sDA.Fill(dsCountryList2,  intRows, intRows, "tblCountry");
  44:      }
  45:   
  46:      protected void Page_Load(object sender, EventArgs e)
  47:      {
  48:          if (!Page.IsPostBack)
  49:          {
  50:              SqlCommand cmd = new SqlCommand("SelectCountriesByLetter", scon);
  51:              cmd.CommandType = CommandType.StoredProcedure;
  52:              cmd.Parameters.Add(new SqlParameter("@Product", 10));
  53:              cmd.Parameters.Add(new SqlParameter("@Letter", "%"));
  54:              sDA = new SqlDataAdapter(cmd); 
  55:              dsCountryList1 = new DataSet();
  56:              try
  57:              {
  58:                  sDA.Fill(dsCountryList1, "tblCountry");
  59:                  rowSum = dsCountryList1.Tables[0].Rows.Count;
  60:              }
  61:              catch (Exception ex)
  62:              {
  63:                  rowSum = 0;
  64:                  return;
  65:              }
  66:              intRows = rowSum / intPages;
  67:              readpage(1);
  68:              BindData();     
  69:          }
  70:      }
  71:  } 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Beanie posted on November 25, 2009 15:31

Ever needed to display more than one value in a bulleted list or dropdown, I did and here is how I ended up getting round it.

Using a normal bulleted list, we leave of the data source and on pageload run loadBulletedLists passing in the list and any variables to filter the stored stocedure used to populate it.

loadBulletedLists takes the recordset and inserts the results into the list, if there are two columns in a row is adds both seperated by a comma, if not it adds the one column.

aspx page

<asp:BulletedList ID="blBulletedList" runat="server" EnableViewState="True"></asp:BulletedList>
aspx.cs page
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:  using System.Web.UI;
   6:  using System.Web.UI.WebControls;
   7:  using System.Data;
   8:  using System.Data.SqlClient;
   9:  using System.Configuration;
  10:  public partial class bridge_Default3 : System.Web.UI.Page
  11:  {
  12:      protected void Page_Load(object sender, EventArgs e)
  13:      {
  14:          if (!IsPostBack)
  15:          {
  16:              loadBulletedLists(blBulletedList,0);
  17:          }
  18:      }
  19:      protected void loadBulletedLists(object sender, object vParameter)
  20:      {
  21:          BulletedList blList = ((BulletedList)sender);
  22:          blList.Items.Clear();
  23:   
  24:          SqlConnection conn = null;
  25:          conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
  26:          SqlDataReader rdr = null;
  27:          
  28:          conn.Open();
  29:          SqlCommand cmd = new SqlCommand("selectBulletedListItems", conn);
  30:          cmd.CommandType = CommandType.StoredProcedure;
  31:          cmd.Parameters.Add(new SqlParameter("@Parameter", vParameter));
  32:   
  33:          string strColumn1;
  34:          string strColumn2;
  35:          rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  36:          string strTitle = "";
  37:   
  38:          while (rdr.Read())
  39:          {
  40:              strColumn1 = rdr["fldColumn1"].ToString();
  41:              strColumn2 = rdr["fldColumn2"].ToString();
  42:              if (strColumn1 != "")
  43:              {
  44:                  strTitle = strColumn1;
  45:              }
  46:              if (strColumn2 != "")
  47:              {
  48:                  if (strTitle != "")
  49:                  {
  50:                      strTitle += ", " + strColumn2;
  51:                  }
  52:                  else
  53:                  {
  54:                      strTitle = strColumn2;
  55:                  }
  56:              }
  57:              blList.Items.Add(new ListItem(strTitle));
  58:          }
  59:      }
  60:  }

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Beanie posted on October 27, 2009 18:49
   1:  protected void cboCountry_SelectedIndexChanged(object sender, EventArgs e)
   2:  {        
   3:      cboState.DataBind();
   4:      if (cboState.Items.Count != 0)
   5:      {
   6:          cboState.Visible = true;
   7:          ListItem li = new ListItem("No State", "0");
   8:          cboState.Items.Add(li);
   9:          cboState.SelectedIndex = cboState.Items.IndexOf(cboState.Items.FindByValue("0"));
  10:      }
  11:      else
  12:      {
  13:          cboState.Visible = false;
  14:      }
  15:  }

Posted in: Development , Development - ASP  Tags: , , ,

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Beanie posted on September 17, 2009 13:42

With contract negotiations and rumours happening on a daily basis I thought it might be a good idea to set out which teams have sorted out their rider line up for 2010.

Fiat Yamaha

  • Valentino Rossi
  • Jorge Lorenzo

Repsol Honda Team

  • Daniel Pedrosa
  • Andrea Doviziso

Rizla Suzuki MotoGP

  • Loris Capirossi
  • Alvaro Bautista

Ducati Marlboro Team

  • Nicky Hayden
  • TBC

Monster Yamaha Tech 3

  • Colin Edwards
  • Ben Spies

Pramac Racing (Ducati)

  • TBC
  • TBC

San Carlo Honda Gresini

  • Marco Simoncelli
  • Marco Melandri

LCR Honda MotoGP

  • Randy De Puniet (tbc)
  • Hiroshi Aoyama (Rumour) – Extra Bike

Scot Racing Team MotoGP (Honda)

  • TBC

 

Aspar (Ducati) – New team

  • Héctor Barberá

There are also rumours that Honda may be putting another four bikes on the grid, this may include the one Lucio Cecchinello would like to add to his team.

Riders without a confirmed ride for 2010:

  • James Toseland
  • Chris Vermulen (current seat taken)
  • Alex de Angles (current seat taken)
  • Toni Elias (current seat taken)
  • Casey Stoner
  • Randy De Puniet (LCR keen to keep him)
  • Gabor Talmasi
  • Niccolo Canepa
  • Mika Kallio

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Beanie posted on September 9, 2009 08:56

Ever need to have a dropdown which allows you to have a SelectedIndex that's not in the list?

This is how to write a control to allow you to do this.

   1:  using System;
   2:  using System.Collections;
   3:  using System.Data;
   4:  using System.Web.UI.WebControls;
   5:  using System.Web.UI;
   6:  using System.ComponentModel;
   7:  namespace Controls {
   8:  public class ForgivingDropDownList : DropDownList
   9:      {
  10:          [Category("Behavior"), DefaultValue(true)]
  11:          public bool AllowInvalidSelectedValue
  12:          {
  13:              get { return ViewState["allowInvalid"] != null ? (bool)ViewState["allowInvalid"] : true; }
  14:              set { ViewState["allowInvalid"] = value; }
  15:          }
  16:   
  17:          public override string SelectedValue
  18:          {
  19:              get
  20:              {
  21:                  return base.SelectedValue;
  22:              }
  23:              set
  24:              {
  25:                  if (!AllowInvalidSelectedValue)
  26:                  {
  27:                      base.SelectedValue = value;
  28:                      return;
  29:                  }
  30:                  if (this.Items.Count != 0)
  31:                  {
  32:                      if ((value == null) || (base.DesignMode && (value.Length == 0)))
  33:                      {
  34:                          this.ClearSelection();
  35:                          return;
  36:                      }
  37:                      ListItem item = this.Items.FindByValue(value);
  38:                      if (item == null)
  39:                      {
  40:                          base.SelectedValue = null;
  41:                          return;
  42:                      }
  43:                      base.SelectedValue = value;
  44:                  }
  45:              }
  46:          }
  47:      }
  48:  }

Register it in web.config

   1:  <pages>
   2:         <controls>
   3:          <add tagPrefix="custom"  namespace="Controls" />
   4:      </controls>
   5:  </pages>

Using the dropdown

   1:  <Custom:ForgivingDropDownList  AppendDataBoundItems="true" ID="cbo" runat="server"  DataSourceID="dse" DataValueField="fldID" DataTextField="fld" SelectedValue='<%# bind("fldID") %>' >
   2:  </Custom:ForgivingDropDownList>
   3:                     

Simples ;-)


Posted in: Development  Tags: , , ,

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Beanie posted on September 6, 2009 16:22
Here we take the information from a contact form and email it to the site owner.

 

   1:  protected void btnSend_Click(object sender, EventArgs e)
   2:  {
   3:      if (Page.IsValid)
   4:      {
   5:          string fileName = Server.MapPath("ContactForm.txt");
   6:          string mailBody = string.Empty;
   7:          if (Cache["ContactFormMailBody"] == null)
   8:          {
   9:              mailBody = System.IO.File.ReadAllText(fileName);
  10:              Cache.Insert("ContactFormMailBody", mailBody, new CacheDependency(fileName));
  11:          }
  12:          else
  13:          {
  14:              mailBody = Cache["ContactFormMailBody"].ToString() + "\r\n(File from the cache)";
  15:          }
  16:   
  17:          mailBody = mailBody.Replace("##Name##", txtName.Text);
  18:          mailBody = mailBody.Replace("##Email##", txtEmailAddress.Text);
  19:          mailBody = mailBody.Replace("##HomePhone##", txtPhoneHome.Text);
  20:          mailBody = mailBody.Replace("##BusinessPhone##", txtPhoneBusiness.Text);
  21:          mailBody = mailBody.Replace("##Comments##", txtComments.Text);
  22:   
  23:          MailMessage myMessage = new MailMessage();
  24:          myMessage.Subject = "Response from web site";
  25:          myMessage.Body = mailBody;
  26:   
  27:          myMessage.From = new MailAddress("email@doamin.ext");
  28:          myMessage.To.Add(new MailAddress("web@doamin.ext"));
  29:          lblMessage.Text = "Your message has been sent to us";
  30:          SmtpClient mySmtpClient = new SmtpClient();
  31:          try
  32:          {
  33:              mySmtpClient.Send(myMessage);            
  34:          }
  35:          catch (Exception )
  36:          {
  37:              lblMessage.Text = "An error occurred while sending your e-mail. Please try again.";
  38:          }
  39:   
  40:          lblMessage.Visible = true;
  41:          FormTable.Visible = false;
  42:          System.Threading.Thread.Sleep(5000);
  43:      }
  44:  }
  45:   

The template

    Hi there,
A user has left the following feedback at the site:
Name:               ##Name##
E-mail address:     ##Email##
Home phone:         ##HomePhone##
Business phone:     ##BusinessPhone##
Comments:           ##Comments##

 


Posted in: Development , Development - ASP  Tags: , ,

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Beanie posted on September 6, 2009 16:16

 How to run through all the files within a folder and list them in code.

List files to bulleted list

In this case we find all the files within the current folder except csharp.aspx and populate a bulleted list with them as hyperlinks. The C Sharp index file uses this code to create the sample list.

   1:  using System.IO; 
   2:  using System.Text.RegularExpressions;
   3:   
   4:  protected void Page_Load(object sender, EventArgs e)
   5:  {
   6:      DirectoryInfo di = new DirectoryInfo(MapPath(""));
   7:      FileInfo[] rgFiles = di.GetFiles("*.aspx");        
   8:      string strFile = "";
   9:      foreach (FileInfo fi in rgFiles)
  10:      {
  11:          if (fi.Name.ToLower() != "csharp.aspx")
  12:          {
  13:          strFile = Regex.Replace(fi.Name.ToLower(), ".aspx", "");
  14:          blCsharp.Items.Add(new ListItem(strFile,fi.Name.ToLower()));            
  15:          }
  16:      }
  17:  }

 

List files in dropdown list

In this case we list the number contained within a filename starting with a certain country name. The images/chart fodler contains many charts for different countries each with a numerical identifier As we only need the numberical identifier to store in a database we strip out the rest of the filename and list the charts as [chart #]

   1:  using System.IO;
   2:  using System.Text.RegularExpressions;
   3:   
   4:  protected void Page_Load(object sender, EventArgs e)
   5:  {
   6:      string strCountry = "country name";
   7:      string strChart = "";
   8:      DirectoryInfo di = new DirectoryInfo(MapPath("~/images/charts"));
   9:      FileInfo[] rgFiles = di.GetFiles(strCountry + "*.gif");
  10:      var slSortedList = new SortedList();
  11:   
  12:      foreach (FileInfo fi in rgFiles)
  13:      {
  14:          strChart = Regex.Replace(fi.Name.ToLower(), ".gif", "");
  15:          strChart = Regex.Replace(strChart, strCountry, "");
  16:          slSortedList.Add(strChart,"Chart " + strChart );
  17:      } 
  18:       cboChart.DataSource = slSortedList;
  19:       cboChart.DataTextField = "Value";
  20:       cboChart.DataValueField = "Key";
  21:       cboChart.DataBind();
  22:  }

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Beanie posted on September 6, 2009 16:14

A handy built in function of MS SQL that changes the owner of a table or object, simply fill in the table or object name and the name of the new user and execute it.

sp_changeobjectowner  'object' ,  'owner'    

Posted in: Development  Tags:

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Beanie posted on August 27, 2009 10:51

With contract negotiations and rumours happening on a daily basis I thought it might be a good idea to set out which teams have sorted out their rider line up for 2010.

Fiat Yamaha

  • Valentino Rossi
  • Jorge Lorenzo

Repsol Honda Team

  • Daniel Pedrosa
  • Andrea Doviziso

Rizla Suzuki MotoGP

  • Loris Capirossi
  • Alvaro Bautista

Ducati Marlboro Team

  • Nicky Hayden (TBC)
  • TBC

Monster Yamaha Tech 3

  • Colin Edwards / Ben Spies (Rumour)
  • TBC

Pramac Racing (Ducati)

  • TBC
  • TBC

San Carlo Honda Gresini

  • Marco Simoncelli
  • Marco Melandri

LCR Honda MotoGP

  • Randy De Puniet (Rumour)
  • (Rumour) – Extra Bike

Scot Racing Team MotoGP (Honda)

  • TBC

 

Aspar (Ducati) – New team

  • Héctor Barberá

There are also rumours that Honda may be putting another four bikes on the grid, this may include the one Lucio Cecchinello would like to add to his team.

Riders without a confirmed ride for 2010:

  • James Toseland
  • Nicky Hayden (option with Ducati
  • Chris Vermulen (current seat taken)
  • Alex de Angles (current seat taken)
  • Toni Elias (current seat taken)
  • Casey Stoner
  • Randy De Puniet (LCR keen to keep him)
  • Gabor Talmasi
  • Niccolo Canepa
  • Colin Edwards (Staying at Tech 3 / Ben Spies taking his ride (Rumour) )
  • Mika Kallio

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Beanie posted on July 30, 2009 14:26

The last British round of the MotoGP Championship to be held the Donington circuit was held over the weekend of the 24-26 July 2009. For a farewell the rider put on a show for us, the weather probably helped them make it more interesting than they may have liked.

The weather did play a major part in the weekend with no two sessions having the same conditions. Race day was forecast to rain sometime in the afternoon, the rain started in the morning only gently so morning warm up was wet. By the time of the races it was dry and the 125GP started with a dry track, 15 laps in that all changed and the rain came and the rider started falling before the race was red flagged.

The 125 race hadn’t completed enough laps for a result to be declared so a 5 lap sprint was required to get a result, this time in the wet. The conditions caught more riders out including Marc Marquez from the lead and Bradley Smith from 5th. This left Julian Simon to take the win and Scott Reading to take his best result of the year in 3rd.


The 250GP race was far lest eventful and was won by Hiroshi Aoyama with a sizeable lead.


The MotoGP race saw an action packed 50 minutes with three of the leaders falling in the trick conditions, wrong tyre choices saw the Ducati’s of Casey Stoner and Nicky Hayden being lapped before half race distance. Jorge Lorenzo crashed out whilst leading, Toni Elias crashed out having lead the first few laps and Valentino Rossi also crashed out of the lead, although the Doctor did manage to remount and finish 5th after a duel with James Toseland in the last corners of the last lap.

James Toseland made amends for last years disappointment with 6th place, and without a few scary moments could well have finished higher. Rider of the day has to go to Colin Edwards who got off to a slow start and was down in 15th place on the first lap but made it all the way up to 2nd on the last lap and had there been one or two more laps could of made it all the way to the top step. The honour of the win for the last Donington MotoGP was to got to Andrea Dovizioso for his maiden MotoGp win and his first podium of the year.


Photos from Donington MotoGP 2009


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Page List

Search Blog

Tag Cloud

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 Beanie