Sunday, November 28, 2010

C# - Get child properties

On my previous post, we talked about parsing child properties. We will use that piece of code to create an extension method for type Object and easily retrieve the child properties from any class.

For instance you have this parent class:
class Parent
{
      public String Lastname  { get; set; }
}
And you have this child class:

class Child : Parent
{
      public String Petname{ get; set; }
}


You can retrieve the child properties thru this:
class Parent
{
      public String Lastname  { get; set; }
    
     public void DoSomething()
     {
           PropertyInfo[] childProperties = this.GetChildProperties();
Object propertyValue = this.GetChildProperty("Petname");
     }
}
Here's how we define our extension method.

namespace CtrlAltDelete.Library.Helpers.Dynamic
{
    public static class ObjectExtensions
    {
         public static PropertyInfo[] GetChildProperties(this Object source)
        {
            return source.GetType().UnderlyingSystemType.GetProperties();
         }
         public static Object GetChildProperty(this Object source, String propertyName)
        {
            Type childType = source.GetType().UnderlyingSystemType;
            PropertyInfo property = childType .GetProperty(propertyName);
            if (property.CanRead)
                return property.GetValue(source, null);
            return null;
            }
    }
}

C# - How to parse child properties through the parent class

Most of the times, we wanted to have a parent class that will contain all usable functions of the inheriting child classes, and thus a possibility that we might need to access custom properties of the inheriting classes. Since different class types can inherit the parent class, we need to use Reflection in order to gain access to the custom properties.

For instance this is your parent class:
class Parent
{
      public String Lastname { get; set; }
}
And this is your inheriting class:
class Child : Parent
{
     public String Name { get;  set; }
}
You wanted to have a common function on the parent class that will retrieve all custom properties of the inheriting classes. Then you will have this code:
class Parent
{
     public String Lastname { get; set; }

    public void GetChildProperties()
   {
         List<string> childProperties = new List<string>();
         Type childType = this.GetType().UnderlyingSystemType;
         PropertyInfo[] customProperties =  childType.GetProperties();
         foreach(PropertyInfo property in customProperties )
         {
            childProperties.Add(property.Name + " => " + property.Value);
         }
   }
}

Saturday, November 27, 2010

MSSQL - How to execute UPDATE with JOIN

UPDATE Table1
SET Table1field1 = 'somevalue', Table1field2 = 'somevalue'
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.id = t2.ref_id
WHERE t1.Table1field1 = 'test' AND t2.Table2field1 = 'test'
    

Friday, November 19, 2010

C# - Retrieve Class name of child instance/executing child

Let's assume we have this parent class:

namespace MyClasses.Test
{
  class Parent
  {
    public Parent()
    {
    }
  }
}

Then we have this child class:

namespace MyClasses.Test
{
  class Child : Parent
  {
    public Child()
    {
    }
  }
}

From what I've tried, there are two ways on how to retrieve the class name, namespace and type of the executing child.

First let's try the long way. :)

using System.Diagnostics;

public Parent()
{
  StackTrace st = new StackTrace(true);
  MethodBase mb = st.GetFrame(1).GetMethod();
  Type type = mb.ReflectedType;
  String className = type.Name;
  String nameSpace = type.Namespace;
}

Or we could try the easy way. :)
public Parent()
{
    Type type = this.GetType().UnderlyingSystemType;
    String className = type.Name;
    String nameSpace = type.Namespace;
}

These will return:

className = "Child"
nameSpace =  "MyClasses.Test"

Thursday, November 4, 2010

C# - MVC.net Partial View thru Ajax

There are different ways to load Partial View thru Ajax. What I am presenting here are just those that I've known and tested.

To make a partial view available thru ajax calls, it must inherit the "ViewUserControl":

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CAD.Models.SetupModel>" %>

On your controller, you can check if request is an ajax call and render the partial view:


public virtual ActionResult DetailsTable(SetupModel setup)
        {
            if (Request.IsAjaxRequest())
            {
                return PartialView("DetailsTable", setup);
            }
            else
            {
                return View(setup);
            }
        }

Suppose you have this div to display the ajax response:

<div id="otherForms">
</div>

Here are some samples how to render the partial view thru ajax:

A.

<%= Ajax.ActionLink("Show All", MVC.Setup.DetailsTable(), new AjaxOptions { UpdateTargetId = "otherForms" }) %>


B.

<%= Ajax.ActionLink("Show All", MVC.Setup.DetailsTable(), new AjaxOptions { UpdateTargetId = "otherForms", OnSuccess="refreshOtherForms" }) %>
function refreshOtherForms(response) {
            var html = response.get_data();
            var container = response.get_updateTarget();
            $(container).html(html);
            return false;
}

C.

<a href="#" onclick="refreshOtherForms();return false;">

function refreshOtherForms(response) {
            $('#otherForms').load('<%= Url.Action("DetailsTable", "Setup") %>');
}

D.

<a href="#" onclick="refreshOtherForms();return false;">
function refreshOtherForms(response) {
            $.ajax({
                url: '/Setup/DetailsTable',
                data: { variable1: value1 },
                dataType: 'html',
                success: function (data) {
                    $('#otherForms').html(data);
                }
            });
}

C# MVCContrib - Configuration/Setup

To use MVCContrib classes on your views, you have to edit your web.config:


<configuration>
  <system.web>
      <compilation debug="true" targetFramework="4.0">
          <assemblies>
             <add assembly="MvcContrib" />

          </assemblies>
      </compilation>



      <pages>
          <namespaces>
             <add namespace="MvcContrib.UI"/>
             <add namespace="MvcContrib.UI.Grid"/>
             <add namespace="MvcContrib.UI.Pager"/>
             <add namespace="MvcContrib.Pagination"/>
             <add namespace="MvcContrib.UI.Html"/>
             <add namespace="MvcContrib"/>
          </namespaces>
      </pages>
 </system.web>
</configuration>