Thursday, February 26, 2009

Silverlight: Web service handling

Full story here.

Silverlight: if your application doesn't run on the local server...

If you are having problems loading a Silverlight 2.0 application, and you have exhausted all options, you may need to register the XAP file extension in IIS. It is a simple fix. 

Open your IIS Manager, double-click on the MIME Types icon, and then on the Add... link on the right. 
Register the MIME type for .xap to application/x-silverlight-app.

Silverlight: cross domain policy

Click here for the full article.

Wednesday, February 18, 2009

ASP: Cookies - adding new values

For example:

   HttpCookie cookie = Response.Cookies["someId"];
   cookie.Values.Add("key_X", "value_X");


   // or, just:
   cookie.Values["key_Y"] = "value_Y";

ASP: Accessing GET and POST parameters from C#

To access only POST parameters:
   Request.Form["parmeter_name"];

To access only GET parameters:
   Request.QueryString["parameter_name"];

To access GET or POST parameters:
   Request["parameter_name"];

C#: Deserialising XML files

If, for example, you have a books.xml file like this:
<books xsi="....">
  <bookinfo>
    <isbn>0596009305</isbn>
    <author>Bill Bosan</author>
    <title>All About Bulbs</title>
    <price>5.99</price>
    <description>Just when you thought...</description>
  </bookinfo>
  ....
</books>
and you wanted to read it into a list of objects in your .CS file.

First, you need to create a BookInfo class. For example:

public class BookInfo {
private string _id;
private string _author;
private string _title;
private decimal _price;
private string _description;


public string Isbn {
get { return _id; }
set { _id = value; }
}
public string Author {

...
}
}

Then, in your main code you would do something like this:

private void ReadXml(string filename) {
List _catalog;
string fullPath = HttpContext.Current.Server.MapPath(filename);
if (File.Exists(fullPath) == false)
return;
XmlSerializer serializer = new XmlSerializer(typeof(List));
using (FileStream stream = File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
_catalog = (List)serializer.Deserialize(stream);
}

}

Voila!

Friday, February 13, 2009

C#: Compressing, uncompressing, encoding...

To compress data and use Unicode character encoding:

void WriteCompressedDataToStream(string data, Stream destStm)
{
using (DeflateStream zipStm = new DeflateStream(destStm, CompressionMode.Compress))
using (StreamWriter destWriter = new StreamWriter(zipStm, Encoding.Unicode))
destWriter.Write(data);
}

To decompress the above:

string ReadCompressedDataFromStream(Stream srcStm)
{
using (DeflateStream zipStm = new DeflateStream(srcStm, CompressionMode.Decompress))
using (StreamReader srcRdr = new StreamReader(zipStm))
return srcRdr.ReadToEnd();
}

C#: Reading and Writing Full File Content (text or binary)

To read all text:

string fileName = "c:\dir\somefile.txt";
string text = File.ReadAllText(fileName);

To write all text:

string fileName = "c:\dir\somefile.txt";
string text = "Here's some text...\r\nAnd some more in the next line...";
File.WriteAllText(fileName, text);


To read all bytes:

byte[] buffer = new byte[BIG_ENOUGH_SIZE];
string fileName = "somefile.bin";
// opens a binary file, reads bytes, then closes
buffer = File.ReadAllBytes(fileName);

To write all bytes:

// creates a new file (or overwrites, writes bytes, then closes
File.WriteAllBytes(filename, buffer);

Friday, July 11, 2008

ASP: Inserting ASP tags from code behind files

Put a PlaceHolder object in your ASPX file like this:
<asp:PlaceHolder id="ph1" runat="server"/>

Then, from your code file insert dynamically the ASP tags like this:
TextBox tb = new TextBox();
tb.Text = "...some text...";
ph1.Controls.Add(tb);


This will add the <asp:textBox> control to the ASP file.

You can also add HTML tags using the HtmlXXX classes like this:
HtmlImage img = new HtmlImage();
img.Src = "./img123.jpg";
ph1.Controls.Add(img);


or like this:
HtmlGenericControl gc = new HtmlGenericControl("a");
gc.Attributes.Add("href", "http://www.hp.com");
gc.InnerText = "Hewlet Packard";
ph1.Controls.Add(gc);


Sometimes, you will need to add a HTML tag literally. For example, you may wish to add the <br/>. However, the following code:
HtmlGenericControl gc = new HtmlGenericControl("br");
would generate:
<br> </br>

So - instead, you should use the LiteralControl class like this:
ph1.Controls.Add(new LiteralControl("<br/>"));

Wednesday, July 09, 2008

ASP: Repeater control

The Repeater control is a data-bound list control that allows custom layout by repeating a specified template for each item displayed in the list.
Typcially, Repeater control will have the following declaration:


<asp:Repeater ID="Repeater1" runat="server">
  <HeaderTemplate>
    ...
  </HeaderTemplate>
  <ItemTemplate>
    ...
  </ItemTemplate>
  <FooterTemplate>
    ...
  </FooterTemplate>


where the items inside the HeaderTemplate and the FooterTemplate will appear once, while the items in the ItemTemplate will repeat many times.

For example:


<asp:Repeater ID="Repeater1" runat="server">
  <HeaderTemplate>
    <table border="1" width="100%">
      <tr>
        <th> Title </th>
        <th> Artist </th>
        <th> Year </th>
      </tr>
  </HeaderTemplate>
  <ItemTemplate>
      <tr>
        <td> <%# DataBinder.Eval(Container.DataItem, "title") %> </td>
        <td> <%# DataBinder.Eval(Container.DataItem, "artist")%> </td>
        <td> <%# DataBinder.Eval(Container.DataItem, "year")%> </td>
      </tr>
  </ItemTemplate>
  <FooterTemplate>
    </table>
  </FooterTemplate>
</asp:Repeater>


There is also the AlternatingItemTemplate and the Separator template. Read more on the MSDN help pages

Tuesday, July 08, 2008

ASP: Inline scripting

At the top of a ASPX file, there will be a line which starts like this:
   <%@ Page Language="C#" ...
This tells you which language you can use for embedded scripting (i.e. functionality that is not in the code-behind file.
For example, the following embedded script is placed before the opening tag:
   <script runat="server">
      void change(object sender, EventArgs e)
      {
         TextBox1.Text = "Radjenovic";
      }
   </script>

The change() function can be then assigned to, e.g., the ontextchanged event handler for the TextBox1. If the AutoPostBack attribute is also set to True then the text in the TextBox1 will change to "Some other text" as soon as the textbox loses focus (i.e. the user clicks elsewhere, or presses the TAB key).

Thursday, July 03, 2008

ASP: Launch new window on button click

One way to launch a new window when a button is clicked is by associating a javascript with the onlick event. This is best done when the page is loaded:

protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "javascript:window.open ('http://www.microsoft.com/'); return false;");
}


where Button1 is an 'asp:button'.
Also, note the 'return false;' part of the script: this makes sure that the page is not posted back to itself.