Asp.Net: Clear inputfields after form submit

by Pieter Brinkman 15. April 2009 02:56

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

 

Categories: ASP.Net | Controls

Asp.net: Sort generic list with a delegate.

by Pieter Brinkman 10. April 2009 04:57

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

 

Categories: ASP.Net

Asp.Net: Using a Usercontrol property with ObjectDataSource selectparameter

by Pieter Brinkman 9. April 2009 09:34

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:

<asp:ObjectDataSource ID="odsListing" runat="server" SelectMethod="getItems"
    TypeName="HUWeb.layouts.HU_GLOBAL.NodeListingSmall"
    DataObjectTypeName="Sitecore.Collections.ChildList"
    onselecting="odsListing_Selecting">
</asp:ObjectDataSource>

 

And in you codebehind:

[code:c#]

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

 

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

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


Hope it helps.

Categories:

Sitecore: itemlist with ObjectDataSource, Listview and DataPager

by Pieter Brinkman 8. April 2009 05:22

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.

 

<asp:ListView ID="lvListing" runat="server" DataSourceID="odsListing" onitemdatabound="lvListing_ItemDataBound">
    <ItemTemplate>
         <li class="clearfix">
            <a class="thumb" ID="thumnailLink" runat="server" > 
                <sc:Image Field="Thumbnail" ID="thumbnailImage" MaxWidth="120" runat="server" />
            </a>
            <h2><asp:HyperLink runat="server" ID="hlTitle" /></a></h2>
            <span class="date"> <sc:Date ID="listingDate" runat="server" Format="dd-MM-yyyy" /> <asp:Literal Visible="false" runat="server" ID="litDateField" /></span>
            <%# Eval("Fields[\"IntroText\"]") %>
            <asp:HyperLink runat="server" CssClass="url" ID="hlNavigationUrl" />
     </li>
    </ItemTemplate>
    <EmptyDataTemplate>
        Geen artikelen
    </EmptyDataTemplate>
    <LayoutTemplate>
        <ul ID="itemPlaceholderContainer" runat="server" style="">
            <li ID="itemPlaceholder" runat="server" />
        </ul>
    </LayoutTemplate>
    <ItemSeparatorTemplate>
        <br />
    </ItemSeparatorTemplate>
</asp:ListView>

<ul>
  <li class="paging">
        <asp:DataPager ID="DataPager1" PagedControlID="lvListing" runat="server" PageSize="10" >
         <Fields>
             <asp:nextpreviouspagerfield ButtonCssClass="paging-previous" PreviousPageText="Vorige" ButtonType="Link"  ShowPreviousPageButton="True" ShowNextPageButton="False"/>
                <asp:nextpreviouspagerfield ButtonCssClass="paging-next" NextPageText="Volgende" ButtonType="Link" ShowPreviousPageButton="False" ShowNextPageButton="True" />
             <asp:NumericPagerField ButtonType="Link" NextPageText="VOLGENDE" PreviousPageText="VORIGE" RenderNonBreakingSpacesBetweenControls="true" />
         </Fields>
        </asp:DataPager>
    </li>
</ul> 

<asp:ObjectDataSource ID="odsListing" runat="server" SelectMethod="GetItems"
        TypeName="MyNameSpace.MyUsercontrol"
        DataObjectTypeName="Sitecore.Collections.ChildList">
</asp:ObjectDataSource>

 

 


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.  

 

 

 

Categories: ASP.Net | Controls

Portfolio: Using Openlayers and WMS

by Pieter Brinkman 2. April 2009 04:32

For our customer Prorail I prototyped a Google Map control with all dutch train-rails and stations rendered on it.

The map is build with OpenLayers it renders the rails based on WMS images and the stations from KML, both sources come from Arcgis Server.

Categories: Portfolio