2009年1月7日 星期三

solve!! jquery datepicker problem

昨天遇到有關 datepicker 的問題
今天解決了!! 先來大笑三聲 哈哈哈~~~

先來還原現場

index.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
 
    <script src="jquery-1.2.6.min.js" type="text/javascript"></script>
 
    <script src="jquery-ui-packed.js" type="text/javascript"></script>
 
    <script type="text/javascript">
        $(function() {
            $("#btnLoad").click(function() {
                $("#pnlToLoad").load("form.html");
                $("form", "#pnlToLoad").dialog();
            });
        });
    </script>
 
</head>
<body>
    <div id="pnlToLoad" style="display: none">
        load</div>
    <input id="btnLoad" type="button" value="load" />
</body>
</html>


form.html
<form>
<input type="text" id="text1" onfocus="attachDatepicker(this)" />
</form>
 
<script type="text/javascript">
    function attachDatepicker(sender) {
        alert("!!");
        $(sender).datepicker();
    }
</script>
 



看出問題了嗎??

問題出現在 這

$("form", "#pnlToLoad").dialog();


就是在 index.html 中的這一行

因為我想讓 form load 完成後變成一個 dialog 出現
結果沒想到 dialog 會將 form 在 dom 裡面的位置改變
結果等於我按一下 load 在 dom 中就會再多一個 form 出現而且裡面 textbox 的 id 都是相同的
這樣子 jquery 的 selector 當然找不到想要的 input


改成這樣子就好了

$("#pnlToLoad").dialog();

jquery datepicker problem

最近試著使用 jquery 結合 asp.net 的專案
使用了大量的 $().load() 來載入 form 及 grid

但遇到了一個問題
datepicker 在第一次載入時可以正常執行

但在 form 第二次 load 之後
datepicker 怎樣都無法正確的將值放入 Textbox 中

似乎是因為 form 裡面Input 的 id 全都是一樣的。
所以jQuery 的 Selector 無法正確的找到新的 Textbox

目前還不知該如何解決

2009年1月5日 星期一

ASP.NET MVC on iis 6

Using ASP.NET MVC with Different Versions of IIS

1. Right-click a website and select Properties
2. Select the Home Directory tab
3. Click the Configuration button
4. Select the Mappings tab
5. Click the Insert button (see Figure 4)
6. Paste the path to the aspnet_isapi.dll into the Executable field (you can copy this path from the script map for .aspx files)
7. Uncheck the checkbox labeled Verify that file exists
8. Click the OK button

2008年12月23日 星期二

Nullable data type conversion

今天寫了一個 function 來將 client post back 中 form 的資料寫回至物件中

不囉唆,直接看 code

        public static FormCollection FillTo(this FormCollection form, object target, Func<FormCollection, string, object> getValue)
        {
            Type targetType = target.GetType();
            foreach (var property in targetType.GetProperties())
            {
                //檢查 form 中有沒有值
                if (Array.IndexOf(form.AllKeys, property.Name) != -1)
                {
                    object value = getValue(form, property.Name);
 
                    if ((property.PropertyType == typeof(System.Guid)) || (property.PropertyType == typeof(System.Guid?)))
                        value = new Guid(value as string);
                    else if ((property.PropertyType == typeof(System.DateTime)) || (property.PropertyType == typeof(System.DateTime?)))
                        value = Convert.ToDateTime(value);
                    else if ((property.PropertyType == typeof(System.Int16)) || (property.PropertyType == typeof(System.Int16?)))
                        value = Convert.ToInt16(value);
                    else if ((property.PropertyType == typeof(System.Int32)) || (property.PropertyType == typeof(System.Int32?)))
                        value = Convert.ToInt32(value);
                    else if ((property.PropertyType == typeof(System.Int64)) || (property.PropertyType == typeof(System.Int64?)))
                        value = Convert.ToInt64(value);
                    else if ((property.PropertyType == typeof(System.Byte)) || (property.PropertyType == typeof(System.Byte?)))
                        value = Convert.ToByte(value);
                    else if ((property.PropertyType == typeof(System.Boolean)) || (property.PropertyType == typeof(System.Boolean?)))
                        value = BoolParseEX(value as string);
                    else if ((property.PropertyType == typeof(System.Char)) || (property.PropertyType == typeof(System.Char?)))
                        value = Convert.ToChar(value);
                    else if ((property.PropertyType == typeof(System.Decimal)) || (property.PropertyType == typeof(System.Decimal?)))
                        value = Convert.ToDecimal(value);
                    else if ((property.PropertyType == typeof(System.Double)) || (property.PropertyType == typeof(System.Double?)))
                        value = Convert.ToDouble(value);
                    else if ((property.PropertyType == typeof(string))) // || (property.PropertyType == typeof(string?)))
                        value = Convert.ToString(value);
                    else
                        throw new Exception(string.Format("value type not support!!   {0}", property.PropertyType.FullName));
 
                    try
                    {
                        property.SetValue(target, value, null);
                    }
                    catch
                    {
                        //ignore
                    }
                }
            }
            return form;
        }


因為 c# 2.0 加入了 Nullable 的原故,所以特別要做 Nullable 的判斷

2008年12月22日 星期一

Linq to Entity Problem


var query = DataEntities.CLD_CLASSDEF.Where(p => p.ID == new Guid(form["ID"])).FirstOrDefault();


會造成 Only parameterless constructors and initializers are supported in LINQ to Entities

解法為

var query = (from p in dataContext.CLD_CLASSDEF
where p.ID.CompareTo(cldDetailID) == 0
select p).FirstOrDefault();


原因 ??

我也不知道

下面這兩篇好像有討論到,有時間再看

http://mosesofegypt.net/post/2008/08/24/LINQ-to-Entities-what-is-not-supported.aspx
http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/9dee0d13-490c-48aa-bad5-50768d3cedf1/

2008年12月12日 星期五

Generic Type Casting

class c1
{
public Department Department{get;set;}
}

class c2:c1
{
public new Department Department{get;set;}
}

class Department
{
}

class Department : Department
{
}


在這個狀況下 Department 該如何轉型呢??


public new Department Department
{
get
{
Object temp = base.Department;
return temp as Department;
}
set { }
}

2008年11月17日 星期一

GetChanges() Merge() DIY 版

發想:
由於之前是開發 Delphi 的程式。在 .net 2.0 中的 DataSet 我覺得就有 Delphi 的味道。所以就一直想找個在 Delphi 程式中的功能 DataSet.Delta。這個功能主要是要取出 DataSet 中改變的部份。以減少 size 方便上傳。
而在 .net 中對應的 Function 就是 GetChanges() 及 Merge() 這兩個 function
在實際使用後只想發出 亙亙亙 的聲音~
在目前開發的方式是採 MVC 的架構 Client 新增 Record只有一個Temp PK,需上傳至Server後才可以取得實際的 PK。但 GetChange() 及 Merge() 需要由 PK 才可以找出原先的 Record (為何不用 Record 的 RowCode 咧) 才 Merge 的起來。要不然就再多一筆資料給你看。要不然連 Modified 的 Row 也 Merge 不起來。 看來是 M$ 版本的功能太強大了,我實在不會用
試了老半天,決定開發 DIY 版的 Code。

需求:
ViewModel GetChanges() 回傳改變的 DataSet 集合
void Merge(ViewModel result) 將 result 中的資料寫回


用法:




TestVM actual= (TestVM)target.GetChanges();


//the new row
actual.TypedDataSet.DataTable3.Rows[0][2] = "newRow";
actual.TypedDataSet.DataTable3.Rows[0][1] = 8;
actual.TypedDataSet.DataTable3.Rows[0][0] = "i'm pk"; //this is PK

actual.TypedDataSet.DataTable2.Rows[0][2] = "dt2new";
actual.TypedDataSet.DataTable2.Rows[0][1] = 2;

actual.TypedDataSet.DataTable3.Rows[0][2] = "dt3new";
actual.TypedDataSet.DataTable3.Rows[0][1] = 3;

target.Merge(actual);