1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Sorting

Discussion in 'C#' started by kristof1605, Jun 19, 2012.

  1. #1
    hi
    SEMrush
    i'm new in asp now i've got a problem how can i sort on name ?
    i don't find to do that :(
    this is my code asp and asp.cs

    ASP

    <%@ Page Title="Unknown" Language="C#" MasterPageFile="../Default.master" AutoEventWireup="true" CodeFile="IngeschrevenSpelersPerLocatie_Stap2.aspx.cs" Inherits="Admin_IngeschrevenSpelersPerLocatie_Stap2" %><%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
      <asp:HiddenField ID="hfLocatieDatumId" runat="server" Value="" />
      <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">   
        <script type="text/javascript">   
          function ShowDialog()   
          {
            OpenDlg("admin/WebGUI/SpelerInschrijvingDlg.aspx?Id=-1&LDId=" + document.getElementById('ContentPlaceHolder1_hfLocatieDatumId').value);
          }
          </script>  
      </telerik:RadCodeBlock>
      <asp:UpdatePanel runat="server" ID="updatePanel1">
        <ContentTemplate>
          <table cellpadding="0" cellspacing="0">
            <tr>
              <td>
                <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="ldsItems" AllowPaging="False" AllowSorting="True" AutoGenerateColumns="False" GridLines="None" PageSize="10" ShowFooter="true">
                  <MasterTableView DataSourceID="ldsItems" CommandItemDisplay="Top">
                    <RowIndicatorColumn>
                      <HeaderStyle Width="20px"></HeaderStyle>
                    </RowIndicatorColumn>
                    <ExpandCollapseColumn>
                      <HeaderStyle Width="20px"></HeaderStyle>
                    </ExpandCollapseColumn>
                    <Columns>
                    <telerik:GridTemplateColumn HeaderText="Naam">
                        <ItemTemplate>
                          <asp:Label ForeColor='<%# Eval("WinnaarColor") %>' Font-Bold='<%# Eval("IsWinnaar") %>' ID="RadLabelNaam" runat="server"  Text='<%# FindMe.GetNameFromUser(Eval("UserId")) %>'></asp:Label>
                        </ItemTemplate>
                      </telerik:GridTemplateColumn>
                      <telerik:GridTemplateColumn HeaderText="Nickname">
                        <ItemTemplate>
                          <asp:Label ForeColor='<%# Eval("WinnaarColor") %>' Font-Bold='<%# Eval("IsWinnaar") %>' ID="RadLabelNickname" runat="server" Text='<%# FindMe.GetNicknameFromUser(Eval("UserId")) %>'></asp:Label>
                        </ItemTemplate>
                      </telerik:GridTemplateColumn>
                      <%--<telerik:GridTemplateColumn HeaderText="GPwin">
                        <ItemTemplate>
                          <asp:Label ForeColor='<%# Eval("WinnaarColor") %>' Font-Bold='<%# Eval("IsWinnaar") %>' ID="RadLabelBwin" runat="server" Text='<%# FindMe.GetBwinFromUser(Eval("UserId")) %>'></asp:Label>
                        </ItemTemplate>
                      </telerik:GridTemplateColumn>
                      <telerik:GridTemplateColumn HeaderText="Datum ingeschreven">
                        <HeaderStyle Width="200" />
                        <ItemStyle Width="200" />
                        <ItemTemplate>
                          <asp:Label ForeColor='<%# Eval("WinnaarColor") %>' Font-Bold='<%# Eval("IsWinnaar") %>' ID="RadLabelEmail" runat="server" Text='<%# Eval("DatumIngeschreven") %>'></asp:Label>
                        </ItemTemplate>
                      </telerik:GridTemplateColumn>--%>
                      <telerik:GridTemplateColumn HeaderText="Afwezig">
                        <HeaderStyle Width="100" />
                        <ItemStyle Width="100" />
                        <ItemTemplate>
                          <asp:HiddenField ID="hfSpelerInschrijvingId" runat="server" Value='<%# Eval("Id") %>' />
                          <asp:HiddenField ID="hfUserId" runat="server" Value='<%# Eval("UserId") %>' />
                          <asp:CheckBox ID="cbAfwezig" AutoPostBack="true" runat="server" Checked='<%# Eval("Afwezig")==null ? false : Convert.ToBoolean(Eval("Afwezig")) %>' oncheckedchanged="cbAfwezig_CheckedChanged" Visible="true" />
                        </ItemTemplate>
                      </telerik:GridTemplateColumn>
                      <telerik:GridTemplateColumn Aggregate="Count" FooterText="Aantal: " FooterStyle-HorizontalAlign="Left" HeaderText="Aanwezig">
                        <HeaderStyle Width="75" />
                        <ItemStyle Width="75" />
                        <ItemTemplate>
                          <asp:CheckBox ID="cbAanwezig" AutoPostBack="true" runat="server" Checked='<%# Eval("Afwezig")==null ? false : !Convert.ToBoolean(Eval("Afwezig")) %>' oncheckedchanged="cbAanwezig_CheckedChanged" Visible="true" />
                        </ItemTemplate>
                      </telerik:GridTemplateColumn>
                      <telerik:GridTemplateColumn HeaderText="Ticket">
                        <HeaderStyle Width="75" />
                        <ItemStyle Width="75" />
                        <ItemTemplate>
                          <asp:CheckBox ID="cbTicket" AutoPostBack="true" runat="server" Checked='<%# Eval("AlTicket") %>' oncheckedchanged="cbTicket_CheckedChanged" Visible="false" />
                        </ItemTemplate>
                      </telerik:GridTemplateColumn>
                      <%--<telerik:GridTemplateColumn>
                        <ItemStyle HorizontalAlign="Left" />
                        <ItemTemplate>
                          <asp:LinkButton ID="LinkButton1" runat="server" OnClientClick='<%# Eval("FldIdOfUser","OpenDlg(\"admin/WebGUI/NieuwsbrievenDlg.aspx?TypeId={0}&Type=Speler\"); return false;")%>'>E-mail versturen</asp:LinkButton>
                        </ItemTemplate>
                      </telerik:GridTemplateColumn>--%>
                    </Columns>
    				<CommandItemTemplate>
    					<div style="padding: 5px 5px;">
    					  <asp:LinkButton ID="lbAddNew" runat="server" OnClientClick="ShowDialog(); return false;">
    						<asp:Image AlternateText="Toevoegen" ID="Image1" ImageUrl="~/admin/images/AddRecord.gif" runat="server" style="border:0px;vertical-align:middle;" />&nbsp;Toevoegen Speler
    					  </asp:LinkButton>
    					</div>
    				</CommandItemTemplate>
                  </MasterTableView>
                  <ClientSettings>
                    <Selecting AllowRowSelect="True" />
                  </ClientSettings>
                </telerik:RadGrid>
              </td>
            </tr>
            <asp:LinqDataSource ID="ldsItems" runat="server" ContextTypeName="BKFreerollDataContext" onselecting="ldsItems_Selecting" />
            <asp:HiddenField ID="hfSelectedId" runat="server" Value="" />
          </table>
        </ContentTemplate>
      </asp:UpdatePanel>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server">
    </asp:Content>
    Code (markup):
    ASP.CS
    using System;using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
    using System.Web.Security;
    using System.Configuration;
    
    
    public partial class Admin_IngeschrevenSpelersPerLocatie_Stap2 : System.Web.UI.Page
    {
      // Variables
      private BKFreerollDataContext bkf = new BKFreerollDataContext();
      private int iLocatieDatumId = -1;
    
    
      // Events
      protected void Page_Load(object sender, EventArgs e)
      {
        // QueryString
        iLocatieDatumId = Convert.ToInt32(Request["Id"]);
    
    
        // Check
        if (iLocatieDatumId > 0)
        {
          // Set field
          hfLocatieDatumId.Value = iLocatieDatumId.ToString();
    
    
          // Database
          var locatiedatum = (from ld in bkf.LocatieDatums
                              where ld.Id.Equals(iLocatieDatumId)
                              select ld).SingleOrDefault();
    
    
          // Set header title
          Header.Title = locatiedatum.Locatie.LocatieCategorie.Titel_nl + " - " + locatiedatum.Locatie.Titel + " | " + String.Format("{0:dd-MM-yyyy}", locatiedatum.Datum) + " (" + locatiedatum.Aantal_plekken + " plekken) | " + ConfigurationManager.AppSettings["Website_Titel"];
        }
      }
      protected void cbAfwezig_CheckedChanged(object sender, EventArgs e)
      {
        try
        {
          // Get clicked row
          GridDataItem clickedItem = (GridDataItem)(sender as CheckBox).NamingContainer;
          
          // Get row field information
          HiddenField hfSpelerInschrijvingId = (HiddenField)clickedItem.FindControl("hfSpelerInschrijvingId");
          HiddenField hfUserId = (HiddenField)clickedItem.FindControl("hfUserId");
          CheckBox cbAfwezig = (CheckBox)clickedItem.FindControl("cbAfwezig");
    
    
          // Check if spelerinschrijvigid has been found
          if (hfSpelerInschrijvingId != null)
          {
            string strId = hfSpelerInschrijvingId.Value;
            int iId = 0;
            Int32.TryParse(strId, out iId);
            if (iId > 0)
            {
              // Get spelerinschrijving item
              SpelerInschrijving item = (from sp in bkf.SpelerInschrijvings
                                         where sp.Id.Equals(Convert.ToInt32(hfSpelerInschrijvingId.Value))
                                         select sp).SingleOrDefault();
    
    
              // Error handling
              if (item == null) { throw new ArgumentException("Object is niet gevonden!"); }
    
    
              // Ban functionality - Uncheck
              if (!cbAfwezig.Checked)
              {
                IsAbsentThatCreatedBan(Convert.ToInt32(hfSpelerInschrijvingId.Value), hfUserId.Value);
              }
    
    
              // Change afwezig field
              item.Afwezig = cbAfwezig.Checked;
    
    
              // Save afwezig field
              bkf.SubmitChanges();
    
    
              // Ban functionality - Check
              if (cbAfwezig.Checked)
              {
                IsSecondAbsentThenBan(Convert.ToInt32(hfSpelerInschrijvingId.Value), hfUserId.Value);
              }
    
    
              // Refresh
              //RadGrid1.DataBind();
            }
          }
        }
        catch(Exception ex)
        {
          LogExceptions.Log("admin/WebGUI/IngeschrevenSpelersPerLocatie_Stap2.aspx.cs", Request.RawUrl, "cbAfwezig_CheckedChanged",
                            LogExceptions.GetFullExceptionMessage(ex), HttpContext.Current.Request.UserHostAddress);
        }
      }
      protected void cbAanwezig_CheckedChanged(object sender, EventArgs e)
      {
        try
        {
          // Get clicked row
          GridDataItem clickedItem = (GridDataItem)(sender as CheckBox).NamingContainer;
    
    
          // Get row field information
          HiddenField hfSpelerInschrijvingId = (HiddenField)clickedItem.FindControl("hfSpelerInschrijvingId");
          HiddenField hfUserId = (HiddenField)clickedItem.FindControl("hfUserId");
          CheckBox cbAanwezig = (CheckBox)clickedItem.FindControl("cbAanwezig");
    
    
          // Check if spelerinschrijvigid has been found
          if (hfSpelerInschrijvingId != null)
          {
            string strId = hfSpelerInschrijvingId.Value;
            int iId = 0;
            Int32.TryParse(strId, out iId);
            if (iId > 0)
            {
              // Get spelerinschrijving item
              SpelerInschrijving item = (from sp in bkf.SpelerInschrijvings
                                         where sp.Id.Equals(Convert.ToInt32(hfSpelerInschrijvingId.Value))
                                         select sp).SingleOrDefault();
    
    
              // Error handling
              if (item == null) { throw new ArgumentException("Object is niet gevonden!"); }
    
    
              // Ban functionality - Uncheck
              if (cbAanwezig.Checked)
              {
                IsAbsentThatCreatedBan(Convert.ToInt32(hfSpelerInschrijvingId.Value), hfUserId.Value);
              }
    
    
              // Change afwezig field
              item.Afwezig = !cbAanwezig.Checked;
    
    
              // Save afwezig field
              bkf.SubmitChanges();
    
    
              // Refresh
              //RadGrid1.DataBind();
            }
          }
        }
        catch (Exception ex)
        {
          LogExceptions.Log("admin/WebGUI/IngeschrevenSpelersPerLocatie_Stap2.aspx.cs", Request.RawUrl, "cbAanwezig_CheckedChanged",
                            LogExceptions.GetFullExceptionMessage(ex), HttpContext.Current.Request.UserHostAddress);
        }
      }
      protected void cbTicket_CheckedChanged(object sender, EventArgs e)
      {
        try
        {
          // Get clicked row
          GridDataItem clickedItem = (GridDataItem)(sender as CheckBox).NamingContainer;
    
    
          // Get row field information
          HiddenField hfSpelerInschrijvingId = (HiddenField)clickedItem.FindControl("hfSpelerInschrijvingId");
          HiddenField hfUserId = (HiddenField)clickedItem.FindControl("hfUserId");
          CheckBox cbTicket = (CheckBox)clickedItem.FindControl("cbTicket");
    
    
          // Check if spelerinschrijvigid has been found
          if (hfSpelerInschrijvingId != null)
          {
            string strId = hfSpelerInschrijvingId.Value;
            int iId = 0;
            Int32.TryParse(strId, out iId);
            if (iId > 0)
            {
              // Get spelerinschrijving item
              var speler = (from s in bkf.Spelers
                            where s.UserId.Equals(new Guid(hfUserId.Value))
                            select s).SingleOrDefault();
              if (speler != null)
              {
                speler.TicketReceived = cbTicket.Checked;
                bkf.SubmitChanges();
    
    
                // Refresh
                //RadGrid1.DataBind();
              }
            }
          }
        }
        catch (Exception ex)
        {
          LogExceptions.Log("admin/WebGUI/IngeschrevenSpelersPerLocatie_Stap2.aspx.cs", Request.RawUrl, "cbTicket_CheckedChanged",
                            LogExceptions.GetFullExceptionMessage(ex), HttpContext.Current.Request.UserHostAddress);
        }
      }
      protected void ldsItems_Selecting(object sender, LinqDataSourceSelectEventArgs e)
      {
        e.Result = (from si in bkf.SpelerInschrijvings
                    where si.LocatieDatumId.Equals(LocatieDatumId)
                    orderby si.DatumIngeschreven
                    select new
                    {
                      FldIdOfUser = FindMe.GetIdFromUser(si.UserId),
                      si.Id,
                      si.UserId,
                      si.DatumIngeschreven,
                      si.Afwezig,
                      WinnaarColor = FindMe.GetSpelerWinnaarColor(si.UserId),
                      IsWinnaar = FindMe.IsSpelerWinnaarVanTicket(si.UserId),
                      AlTicket = FindMe.HeeftSpelerAlTicket(si.UserId)
                    });
      }
    
    
      // Functions
      private void IsSecondAbsentThenBan(int SpelerInschrijvingId, object UserId)
      {
        try
        {
          // Get speler (ban date)
          var resSpeler = (from s in bkf.Spelers
                           where s.UserId.Equals(UserId)
                           select s).SingleOrDefault();
    
    
          // Initialize a LINQ var
          var resSpelerAfwezig = (from sp in bkf.SpelerInschrijvings
                                  select sp);
    
    
          // Check if this is the 2nd time, give the Speler a Ban Date, 7 days from his last Inschrijving
          if (resSpeler.BanDate.HasValue)
          {
            resSpelerAfwezig = (from sp in bkf.SpelerInschrijvings
                                where sp.Afwezig.Equals(true)
                                && sp.UserId.Equals(UserId)
                                && sp.LocatieDatum.Datum > resSpeler.BanDate.Value
                                select sp);
          }
          else
          {
            resSpelerAfwezig = (from sp in bkf.SpelerInschrijvings
                                where sp.Afwezig.Equals(true)
                                && sp.UserId.Equals(UserId)
                                select sp);
          }
    
    
          // If it's an even number (only 2 actually) set the Ban date
          if (resSpelerAfwezig.Count() % 2 == 0)
          {
            // Check if this is the 2nd time, give the Speler a Ban Date, 7 days past the last Inschrijving
            var resSpelerInschrijving = (from sp in bkf.SpelerInschrijvings
                                         where sp.UserId.Equals(UserId)
                                         orderby sp.LocatieDatum.Datum descending
                                         select sp
                                        ).FirstOrDefault();
    
    
            // Calculate ban untill
            DateTime dtLaatsteInschrijving = resSpelerInschrijving.LocatieDatum.Datum.GetValueOrDefault();
            DateTime dtBanUntill = dtLaatsteInschrijving.AddDays(Convert.ToInt32(ConfigurationManager.AppSettings["Ban_Days"]));
    
    
            // Save Ban Date
            Speler speler = (from s in bkf.Spelers
                             where s.UserId.Equals(UserId)
                             select s).SingleOrDefault();
            // Error handling
            if (speler == null) { throw new ArgumentException("Object is niet gevonden!"); }
            // Change Ban Date field
            speler.BanDate = dtBanUntill;
            // Save speler
            bkf.SubmitChanges();
          }
        }
        catch (Exception ex)
        {
          LogExceptions.Log("admin/WebGUI/IngeschrevenSpelersPerLocatie_Stap2.aspx.cs", Request.RawUrl, "IsSecondAbsentThenBan",
                            ex.Message, HttpContext.Current.Request.UserHostAddress);
        }
      }
      private void IsAbsentThatCreatedBan(int SpelerInschrijvingId, object UserId)
      {
        try
        {
          // Get speler (ban date)
          var resSpeler = (from s in bkf.Spelers
                           where s.UserId.Equals(UserId)
                           select s).SingleOrDefault();
    
    
          // Check if the speler has a ban date
          if (!resSpeler.BanDate.HasValue)
          {
            // Speler doesn't have a ban date, so no worries
            return;
          }
          else
          {
            // Check if this absent date is the 1st or 2nd absent date of the ban-date
            var resSpelerAfwezig = (from sp in bkf.SpelerInschrijvings
                                    where sp.Afwezig.Equals(true)
                                    && sp.UserId.Equals(UserId)
                                    && sp.LocatieDatum.Datum < resSpeler.BanDate.Value
                                    orderby sp.LocatieDatum.Datum descending
                                    select sp);
            // Loop through 
            int iCounter = 0;
            foreach (var spelerinschrijving in resSpelerAfwezig)
            {
              // When counter is smaller then 2
              if (iCounter < 2)
              {
                // Check if this inschrijving is the one that is clicked
                if (spelerinschrijving.Id.Equals(SpelerInschrijvingId))
                {
                  // Reset the ban date to nothing
                  Speler speler = (from s in bkf.Spelers
                                   where s.UserId.Equals(UserId)
                                   select s).SingleOrDefault();
                  // Error handling
                  if (speler == null) { throw new ArgumentException("Object is niet gevonden!"); }
                  // Change Ban Date field
                  speler.BanDate = null;
                  // Save speler
                  bkf.SubmitChanges();
                }
              }
    
    
              // Counter
              iCounter++;
            }
          }
        }
        catch (Exception ex)
        {
          LogExceptions.Log("admin/WebGUI/IngeschrevenSpelersPerLocatie_Stap2.aspx.cs", Request.RawUrl, "IsAbsentThatCreatedBan",
                            ex.Message, HttpContext.Current.Request.UserHostAddress);
        }
      }
    
    
      // Snippets
      public int? LocatieDatumId
      {
        get
        {
          try
          {
            return Convert.ToInt32(hfLocatieDatumId.Value);
          }
          catch (Exception ex)
          {
            LogExceptions.Log("admin/WebGUI/IngeschrevenSpelersPerLocatie_Stap2.aspx.cs", Request.RawUrl, "LocatieDatumId",
                            ex.Message, HttpContext.Current.Request.UserHostAddress);
    
    
            return null;
          }
        }
        set
        {
          hfLocatieDatumId.Value = value.ToString();
        }
      }
    }
    Code (markup):
    thx
     
    kristof1605, Jun 19, 2012 IP
    SEMrush
  2. TasteOfPower

    TasteOfPower Peon

    Messages:
    572
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Oh My God!

    You can do sorting so many ways! It can some straight out of the database, you can do it in your business object, you can do it on the client side.

    But thats to much code to be looking thru for free. Sorry, man. Lolz.
     
    TasteOfPower, Jul 2, 2012 IP
  3. veganin

    veganin Greenhorn

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Which bit of data you want to sort? Do you want to shorten the code sample
     
    veganin, Jul 21, 2012 IP