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);
});


Asp.Net: Using a Usercontrol property with ObjectDataSource selectparameter

I was using a ObjectDataSource to generate a listing including paging. The ObjectDataSource used the GetItems method of my usercontrol. See this previous post. This construction works great. But now I needed to do some custom filtering in the GetItems method based on a property of the usercontrol. At first I tried to use the property directly in the GetItems method, this didn’t work the property the property returned NULL. After a small search on the internet I found the following blog post.

The solution is to dynamically add a Selectparameter that will be passed to the GetItems method. You can dynamically add Selectparameters at the OnSelecting event of the asp:ObjectDataSource,  like this:






And in you codebehind:


protected void odsListing_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
 e.InputParameters["ListingDataSourceId"] = <em>YourUsercontrolProperty</em>;
}


Now you can use YourUsercontrolProperty in the Select method of your ObjectDataSource.


public Sitecore.Collections.ItemList getItems(string <strong>myProperty</strong>)
{
  // Your selection logica
  // You can use the string <strong>myProperty</strong> for filtering
}


Hope it helps.

Sitecore: itemlist with ObjectDataSource, Listview and DataPager

For a new project I started working with Sitecore again.

I needed to create a listing of items with paging. I implemented a Listview with a ObjectDataSource and the DataPager control.




    
         <li class="clearfix">
            <a class="thumb" ID="thumnailLink"> 
                
            </a>
            <h2></a></h2>
            <span class="date">  </span>
            
            
     </li>
    
    
        Geen artikelen
    
    
        <ul ID="itemPlaceholderContainer">
            <li ID="itemPlaceholder" />
        </ul>
    
    
        <br />
    


<ul>
  <li class="paging">
        
         
             
                
             
         
        
    </li>
</ul> 





In the code behind I created the public method GetItems which returns a ChildListCollection.


namespace MyNameSpace
{
    public partial class MyUsercontrol: System.Web.UI.UserControl
    {
     
       public Sitecore.Collections.ChildList GetItems()
       {
          return Sitecore.Context.Item.GetChildren();
        }
     }
} 


The PagedControlID property of the datapager control is set to the ID of the Listview.