Playing with JQuery (fixing Intellisense in VS2008)

The last few years I spend a lot of time working with Asp.Net AJAX. It all worked pretty good, the only downside is that you do not have control the generated HTML. With jQuery you can manipulate generated HTML. This HTML can be generated fully controlled with a ListView.

I'm a lazy programmer so the first thing I needed to do is fix jQuery Intellisense in VS2008. I found my information for doing this on Scott Gu's blog (jQuery Intellisense in Vs 2008). Everything worked great the only bad thing is that my computer needed a restart after installing a Visual Studio Patch…

Now the Intellisense is working the only thing I need to do is blog some nice examples of my work! 😉

Cheers,

Pieter

 

Using the ViewState within the SelectMethod of a ObjectDataSource

By default it is not possible to use the ViewState within methods of a ObjectDataSource. Because the DataSource doesn’t run within the current page instance, it just fires the method that you specified.

You can set the page instance for the DataSourceby setting the ObjectInstance property in the ObjectCreating event of the DataSource.


<asp:ObjectDataSource ID="odsListing" runat="server" SelectMethod="getItems" TypeName="YOUR.NAMESPACE" OnObjectCreating="ObjectDataSource_ObjectCreating" DataObjectTypeName="List">



protected void ObjectDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
 e.ObjectInstance = this;
}


Now you can use the ViewState in you DataSourceMethod (getItems() in this example).

WCF webservice error with DBML objects

When deploying a WCF webservice for a Silverligh application I got the following error:


An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.DataContractSerializerOperationBehavior
 contract: <a href="http://tempuri.org/:IWebService">http://tempuri.org/:IWebService</a> ----&gt; System.Runtime.Serialization.InvalidDataContractException: Type 'Project.service.HU_BACH.ScPlacemark' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.


As you can see from the error you need to add the [Serializable] attribute to all objects that are used within the webservice. After I did this I got the same error for my Linq to Sql objects generated within my DBML. You can make your Linq to Sql objects serializable by changing the dbml setting Serialization Mode to Unidirectional.

 
Hope it helps.

Asp.Net: Clear inputfields after form submit

Every time a form is successful summited I need to clear all input-fields to the default values and give feedback to the user. I wrote a method ClearControl that can have a control as input parameter. This method will set the control based on his type back to the defaultvalue.


 public static void ClearControl(Control control)
{
 switch (control.GetType().Name)
 {
  case "TextBox":
   TextBox txtBox = (TextBox)control;
   txtBox.Text = "";
   break;
  case "DropDownList":
   DropDownList ddl = (DropDownList)control;
   ddl.SelectedIndex = 0;
   break;
  case "CheckBox":
   CheckBox chk = (CheckBox)control;
   chk.Checked = false;
   break;
  case "CheckBoxList":
   CheckBoxList chkList = (CheckBoxList)control;
   foreach (ListItem li in chkList.Items)
    li.Selected = false;
   break;
  case "Panel":
   ClearFields((Panel)control);
   break;
  case "RadioButtonList":
   RadioButtonList rbl = (RadioButtonList)control;
   rbl.SelectedIndex = -1;
   break;
 }
}


To use this method I wrote a method ‘ClearFields’ that accepts a View or Container control. You can add every type of control that has the .Controls property.


public static void ClearFields(Panel container)
{
 foreach (Control control in container.Controls)
 {
  ClearControl(control);
 }
}

public static void ClearFields(View container)
{
 foreach (Control control in container.Controls)
 {
  ClearControl(control);
 }
}


When I have some time left I will try to implement the ClearFields Method as a ExtensionMethod.

Cheers,

Pieter

Asp.net: Sort generic list with a delegate.

You can use a delegate to sort a generic list.

Example

 

GenericList ObjectItem = new GenericList ObjectItem()

GenericList.Sort(delegate(ObjectItem i1,ObjectItem i2)
{
 return i1.SortProperty.CompareTo(i2.SortProperty);
});