Archive for the '.NET 2.0' Category Page 4 of 5

24JunMake ATLAS support the Guid in javascript

I use guids as PK in my databases.

I like guids :) But atlas shows them as Object and that gives me some trouble using them in my atlas pages.

But to implement your own converter is not so difficult you just inherit from the JavaScriptConverter and override those methods.

And afterwards you have to register it in the converters section of the web.config

The web.config

   21 <microsoft.web>

   22         <converters>

   23             <add type=Microsoft.Web.Script.Serialization.Converters.DataSetConverter/>

   24             <add type=Microsoft.Web.Script.Serialization.Converters.DataRowConverter/>

   25             <add type=Microsoft.Web.Script.Serialization.Converters.DataTableConverter/>

   26             <add type=Flanders.Library.AtlasControls.GuidConverter />

   27         converters>

 

The Converter Class:

    1 using System;

    2 using Microsoft.Web.Script.Serialization;

    3 

    4 namespace Flanders.Library.AtlasControls

    5 {

    6     public class GuidConverter : JavaScriptConverter

    7     {

    8         public override string Serialize(object o)

    9         {

   10             Guid guid = Guid.Empty;

   11             if(o is Guid)

   12                 guid = (Guid)o;

   13             return “‘”+guid.ToString()+“‘”;

   14         }

   15         protected override string GetClientTypeName(Type serverType)

   16         {

   17             return “Guid”;

   18         }

   19         public override object Deserialize(string s, Type t)

   20         {

   21             throw new NotSupportedException();

   22         }

   23         protected override Type[] SupportedTypes

   24         {

   25             get

   26             {

   27                 return new Type[] { typeof(System.Guid) };

   28             }

   29         }

   30     }

   31 }

01JunEnable Intellisense for skin files

Vladimir Bychkov explains it nicely.

http://vladimir.bychkov.info/blog/PermaLink,guid,be76b3bf-c524-456c-9c8f-a2584924aa32.aspx

This will take a lot of the pain out of creating themes :)

27MayScript# : Nikhil does it again

Nikhil Kothari must be one of my favourite programmers on the whole world. Beside having learned a lot from his blog posts and book. He also develops these great little tools that really help my life as a developer.

This time he wrote a utility called Script# which let’s you code normal c# code, not everything is supported yet but in Nikhil I trust ;) and translates it to javascript without all the hassle of writing javascript.

I like the possibilities of javascript a lot but hate the whole scripting experience :)

Anyway here’s the link : http://www.nikhilk.net/ScriptSharpIntro.aspx

22MayATLAS continued : Get the freetextbox going.

I decided after working a bit more with ATLAS that the client-side needs a LOT more documentation. Some cooperation with other javascript libraries would also be nice. 
A properly working dataservice is lacking.

I’ve turned back to the server side controls.  The start of this whole mission was to keep the freetextbox control on a page and still don’t have full postbacks.  Upload pictures with progressbar was going to be a plus.

How to upload files from within an update panel is something that I will come back to in the future but it isn’t going to be pretty.

The things I’m about to write I will wrap in a control later (when I have time) for people to download

Anyways you wrap your freetextbox in an Iframe and it works. Don’t add a scriptmanager to your iframe page.

Put a hiddenfield on your main page and in the ClientSideTextChanged event of the freetextbox control you update the hiddenfield on the parent.  If you wrap it inside a control then I suggest you add a property to the control that holds the Text variable :)

That’s about it, easy isn’t it.

20MayIntellisense for Atlas Xml Script

I’ve come across a french guy’s blog who has created an intellisens xsd generator for the atlas framework.

He says it’s not perfect but I’ll take whatever I can get that saves me from writing full words. It’s not perfect in the sense that it doesn’t show you if something is allowed there or not.

The link to the article about the intellisense generator:
http://blogs.developpeur.org/cyril/archive/2006/04/19/Intellisense_pour_Atlas_c_est_possible_generation_schema_xsd_en_javascript.aspx

To install the xsd just extract this file to C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas

The next step is to separate the xml script from your aspx page so it doesn’t look so cluttered anymore. Also the intellisense will only work in the xml editor.  The same guy has written another post on how to do just that.

The link to his article : http://blogs.developpeur.org/cyril/archive/2006/04/29/Atlas_Charge_un_fichier_xml_script_externe_dans_une_application_ATLAS_grace_a_Ajax.aspx

The english explanation :
link to your atlas xml file like this : <link type=”text/xml-script” href=”DynamicLoad.atlas” />

And next add some javascript to the page :

Sys.Application.load.add(function(){
     var linkElements = document.getElementsByTagName(‘link’
);
     
     for (var i = 0; i < linkElements.length; i++){
           
linkElement = linkElements[i]; 
           
if (linkElement.type= ‘text/xml-script’ && linkElement.href){
                  
var request = new Sys.Net.WebRequest();
                  
request.set_url(linkElement.href);
                  
request.completed.add(function(e){
                       
Sys.MarkupParser.processDocumentScripts(Sys.Application.getMarkupContext(), e.get_xml().childNodes, null);
                 
});   
                 
request.invoke(); 

          
}
     }

});

20MayA .NET IM VOIP client

http://synced0.blogspot.com/2006/03/intro-to-nexxia-messenger.html

20MayATLAS cracked the nut

Ever since i knew about the ATLAS framework it intrigued me because it offers a lot of the features that made me avoid javascript in the past.  The issues that kept me from using javascript are well known to everybody who wrote 7 lines of javascript. Basically it comes down to browser differences in understanding javascript and the differences in the DOM implementation for the two.

Then there was Ajax.NET very nice but a lot of extra programming is involved to get minor ajaxy effects. and still the browser incompatibility.

Actually I wasn’t looking for an ajax framework I wanted  the whole deal. And I stumbled upon ATLAS like many others have of course.  I had been using ajax in intranet applications through the magixajax panel, which still has at least one feature that the update panel from atlas doesn’t have : the ability of a button to cause a regular postback from inside a panel.  It can be implemented in atlas by using a clientside control and have that cause a postback when it’s clicked.

Then Tim Haines told me about the Anthem library. A library of controls that add instant ajax abilities to numerous familiar controls.  The library works well but i found it hard to debug hence it slowed me down a lot. I am not using Anthem anymore.

The atlas framework was exactly what i wanted: extensible, cross-browser and shiny :)
The entry into the atlas world was definitely not the easiest one ever.  It started out with the updatepanels and the server side controls along with the AtlasControlToolkit. And from seeing how things worked in the control toolkit.
The updatepanel is a great tool for enhancing existing apps with instant ajax abilities, that much is certain. A downside I found with the updatepanel was the fact that the response you get back from the server is the full html version for your page and some added lines.  Not really a bandwith saver I would say. Accompanied with the fact that you can’t put a freetextbox inside an updatepanel nor a file upload control made it a close but no cigar kinda tool.  The data exchange is what bugged me the most because more data means slower pages etc…

The next step presented itself, get using webservices and do the processing client-side. Cool let’s take a look at the samples from the documentation. Aha a data service that sounds interesting. Ooops wait a minute xml script. So it took me a some time to get over the fact that it is xml script and to be convinced xml script does save you from writing tons of lines in javascript. Once you get used to the xml script it reads ok and more quickly than javascript.
The dataservice looked very cool.. it takes care of the processing of the datatables returned from my BLL. I tried to get it to work for a very long time but all I could get out of it was that it displays records (that part I had after 5 minutes) but i can’t get it to save my data back to the database (that part cost me 2 days at least). This is through bindings and xml script.  No updating kinda cripples my whole application :(  

A day later i got it working after digging deeper and doing a lot of googling and live searching. I still can’t use the dataservice except for loading data. but I can use regular webmethods to put stuff in the database and use the load method of the dataservice to get the fresh data bound to the listview and itemview controls

So now I can have my somewhat SOA oriented applications and not spend hours trying to resolve browser issues or write thousands lines of javascript to parse my results into the page.

Resources that I found to be very helpful :
http://atlas.asp.net/docs/Client/Browser/jsBrowser.aspx
http://atlas.asp.net/docs/default.aspx
http://atlas.asp.net/docs/Client/default.aspx
http://aspadvice.com/blogs/garbin/archive/2006/03/05/15591.aspx

Javascript transitions and good scripts can also be gotten from script.aculo.us
http://script.aculo.us/

Another problem with an ajax application is that the history button behavour gets broken. Or at least doesn’t work as it is supposed to work. There is a library out there called the Real Simple History Framework that makes that a thing from the past.
A detailed article can be found here :
http://www.onjava.com/pub/a/onjava/2005/10/26/ajax-handling-bookmarks-and-back-button.html?page=1

 

04MaySqlCommandBuilder .. The one I forgot to complete the puzzel

Basically:.NET 2.0 is too cool :) ))

After exploring the smo classes the last part of the afternoon was about creating migrating the data. I hadn’t used the ado api fully but remembered something about the commandbuilder. My conclusion : Worth taking a look at.

I do realise that it isn’t the worlds prettiest sql that gets generated but it gets the job done.  And in the case of my 125 tables that i need to migrate FAST it will save me about 1-2 weeks of writing import procedures etc. Generic datatable updates by writing one sql select command.

   18  public void ImportData(DataTable dt)

   19         {

   20             System.Text.StringBuilder sb = new StringBuilder();

   21             using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))

   22             {

   23                 SqlCommand cmd = new SqlCommand(string.Format(“Select * from {0}”, dt.TableName),conn);

   24                 SqlDataAdapter da = new SqlDataAdapter(cmd);

   25                 SqlCommandBuilder builder = new SqlCommandBuilder(da);

   26                 //da.UpdateBatchSize = 15;

   27                 da.Update(dt);

   28 

   29             }

   30         }

03MayWoohoo… Microsoft.SqlServer.Management.Smo

The problem I was facing is that I need to keep an accounting database in sync with the “real” application this company uses.

I don’t need all the tables but about 125 need to be imported with data and the whole shabang.

In my mind the best way to do that is to have something iterate the whole database, fill up a dataset and start creating the tables in sql if they don’t exist. Next step is to import all the data from the dataset into the new database.

Obviously this can be done if you just look at the dataset generator. It reads schema’s and creates .net objects from database tables.
But how do i create tables ? And how do i do it without having to write long sql strings ?

The answer is : Microsoft.SqlServer.Management.Smo
A managed library to manipulate sql 2005 :)

You can create any number of items with this library including databases, tables, functions, views, stored procedures ….
Hence woohoo.

To support just the simple types (no objects) I now have a class that does exactly what I want it to do import a database and it took me about 70 lines of code :)

All you need to know to get started can be found on the microsoft website or on david haydens website, that’s where I learned of the existence of this namespace.

http://davidhayden.com/blog/dave/archive/2006/01/27/2775.aspx

http://msdn2.microsoft.com/en-US/library/ms162203.aspx

I won’t be putting up any code with this post because it’s too easy.  If you do run into problems, i can provide you with a snippet :)

03MayAdding insert capabilities to the gridview

I didn’t have time to wrap it in a proper control yet. In this post I’ll just be putting the page implementation.

The gridview is cool and to add an insertrow to it can be done by the footer template. How to do it I explained a while ago : http://geekswithblogs.net/casualjim/articles/51360.aspx

If you want to get the controls from the footerrow you will need to address them with GridView.FooterRow.FindControl(“ControlName”);.

For the empty datatemplate it’s a little bit trickier but not that much. It still is a gridviewrow but it’s wrapped in another control. If you use a button control in your empty datatemplate you also can’t use the submit behaviour from then on everything should be familiar. :)

This is the part in the page :

<%

@ Page Language=”C#” Theme=”" AutoEventWireup=”true” CodeFile=”Test.aspx.cs” Inherits=”Test” %>

DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
   <
html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
      <
head id=”Head1″ runat=”server”>
         
<title>ASP.NET Insert data in Gridview title>
      head>
<
body>
   
<form id=”form1″ runat=”server”>
      
<asp:Label ID=”Label1″ runat=”server” Text=”Label”>asp:Label>
      
<asp:GridView ID=”GridView1″ ShowFooter=”true” runat=”server”    
         OnRowCommand
=”GridView1_RowCommand1″ AutoGenerateColumns=”false”>
      
<Columns>
         
<asp:TemplateField>
            
<ItemTemplate>
               
<asp:Button Text=”Edit” CommandName=”Edit” CausesValidation=”false” runat=”server” ID=”btEdit” /> 
               <asp:Button Text=”Delete” CommandName=”Delete” CausesValidation=”false” runat=”server” ID=”btDelete” />
            
ItemTemplate>
            
<EditItemTemplate>
               
<asp:Button Text=”Update” CommandName=”Update” CausesValidation=”true” runat=”server” ID=”btUpdate” /> 
               
<asp:Button Text=”Cancel” CommandName=”Cancel” CausesValidation=”false” runat=”server” ID=”btCancel” />
            
EditItemTemplate>
            
<FooterTemplate>
               
<asp:Button Text=”Insert” CommandName=”Insert” CausesValidation=”true” runat=”server” ID=”btInsert” /> 
               
<asp:Button Text=”Cancel” CommandName=”Cancel” CausesValidation=”false” runat=”server” ID=”btCancel” />
            
FooterTemplate>
         
asp:TemplateField>
      
   <asp:TemplateField >
            
<ItemTemplate>
               
<asp:Label ID=”lblValue” Text=’<%# Eval("Name") %> runat=”server”>asp:Label>
            
ItemTemplate>
            
<EditItemTemplate>
                  
<asp:TextBox ID=”tbUpdate” runat=”server” Text=’<% Bind("Name") %>‘>asp:TextBox>
            
EditItemTemplate>
            
<FooterTemplate>
                  
<asp:TextBox ID=”tbInsert” runat=”server” Text=”" >asp:TextBox>
            
FooterTemplate>
            asp:TemplateField>
         Columns>
         <EmptyDataTemplate>
               <asp:TextBox ID=”tbEmptyInsert” runat=”server”>asp:TextBox><br />
               <asp:Button ID=”btSend” Text=”Insert” runat=”server” CommandName=”EmptyInsert” UseSubmitBehavior=”False” />
            EmptyDataTemplate>
         asp:GridView>
      form>
   body>
html>

And the code behind :

   14 public partial class Test : System.Web.UI.Page

   15 {

   16     protected void Page_Load(object sender, EventArgs e)

   17     {

   18         if (!IsPostBack)

   19         {

   20             //Create dummy data

   21             DataTable dt = new DataTable();

   22             DataColumn dc = new DataColumn(“Name”);

   23             dt.Columns.Add(dc);

   24             DataRow dr = dt.NewRow();

   25             dr["Name"] = “Ivan”;

   26 

   27             //Uncomment the following line to have data in the grid :)

   28             //dt.Rows.Add(dr);

   29 

   30             //Bind the gridview

   31             GridView1.DataSource = dt;

   32             GridView1.DataBind();

   33         }

   34         //Recurses through the controls to show the naming of each individual control that is currently in the gridview

   35         RecurseControls(GridView1.Controls[0].Controls);

   36         Label1.Text += GridView1.Controls[0].Controls[0].GetType().Name +

   37     }

   38 

   39     void RecurseControls(ControlCollection ctls)

   40     {

   41         foreach (Control ctl in ctls)

   42         {

   43             if (!ctl.HasControls())

   44                 Label1.Text += ctl.ClientID + ” “ + ctl.GetType().Name +
;

   45             else

   46                 RecurseControls(ctl.Controls);

   47         }

   48     }

   49 

   50     protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)

   51     {

   52         if (e.CommandName == “EmptyInsert”)

   53         {

   54             //handle insert here

   55             TextBox tbEmptyInsert = GridView1.Controls[0].Controls[0].FindControl(“tbEmptyInsert”) as TextBox;

   56             Label1.Text = string.Format(“You would have inserted the name : {0} from the emptydatatemplate”,tbEmptyInsert.Text);

   57 

   58         }

   59         if (e.CommandName == “Insert”)

   60         {

   61             //handle insert here

   62             TextBox tbInsert = GridView1.FooterRow.FindControl(“tbInsert”) as TextBox;

   63             Label1.Text = string.Format(“You would have inserted the name :  {0} from the footerrow”, tbInsert.Text);

   64         }

   65     }

   66 

   67 }

Recent Flickrs

    Blogroll

    Recent Listening

    Scrobbler