2013年6月26日 星期三

GridView 總是顯示 Pager ( Force Pager Visible )

加入 OnPreRender 事件:
<asp:GridView ID="gv1" OnPreRender="GridView1_PreRender" runat="server">

in code-behind:
Protected Sub gv1_PreRender(ByVal sender As Object, ByVal e As EventArgs)
    Dim pagerRow As GridViewRow = GridView1.BottomPagerRow
    If pagerRow IsNot Nothing AndAlso pagerRow.Visible = False Then
         pagerRow.Visible = True
    End If
End Sub

or:
Protected Sub GridView1_PreRender(ByVal sender As Object, ByVal e As EventArgs)
    gv1.BottomPagerRow.Visible = True
End Sub

來源: http://stackoverflow.com/questions/1057114/forcing-asp-net-gridviews-pager-to-show

GridView 加入控制項至Pager Row ( Adding Controls To Pager Row )

在 GridView 加入 OnRowDataBound 事件:
<asp:gridview id="gv1" runat="server" autogeneratecolumns="False" onrowdatabound="gv1_RowDataBound" allowpaging="True">

in code-behind:
Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.Pager Then
        Dim myLabel As Label = New Label
        Dim table As Table = e.Row.Cells(0).Controls(0)
        Dim parentCell As TableCell = table.Rows(0).Cells(table.Rows(0).Cells.Count - 1)
        parentCell.Controls.Add(myLabel)
        myLabel.Text = "Some Text"
    End If
End Sub

2013年6月2日 星期日

jQuery parent() && children() ( Select input from next td )

html:
<tr>
    <td>
        <input type="text" id="txtPrev" />
    </td>
    <td>
        <input type="text" id="txtNext" />
    </td>
</tr>
找到下一個 td 裡的 input:
var flag = 0;
$("input").keypress(function(){
    var target = $(this).parent("td").next().children("input");
    if(flag%2)
        target.css("background-color", "blue");
    else
        target.css("background-color", "green");
    ++flag;
});

Demo

jQuery 關閉功能鍵 ( Disable Function Key )

function disableFKey(e){
    if( (e.which || e.keyCode) == 116 ){         // F5
        e.preventDefault();
    }else if( (e.which || e.keyCode) == 120 ){   // F9
        e.preventDefault();
        location.href = 'www.google.com'  // do something
    }
});

$(document).on("keydown", disableFKey);

2013年5月26日 星期日

Objective-C 字串相加 (stringByAppendingString )

使用 stringByAppendingString:
#import <Foundation/Foundation.h>
int main(){
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString *firstName = @"Wwfwwf";
    NSString *lastName = @"Liao";
    NSString *fullName = [[firstName stringByAppendingString: @" "] stringByAppendingString: lastName];
    NSLog(@"my name is %@\n", fullName);
    [pool drain];
    return 0;
}
result:
my name is Wwfwwf Liao
使用 stringWithFormat:
NSString *fullName = [NSString stringWithFormat: @"%@ %@", firstName, lastName];

Objective-C unsigned int ( NSUInteger )

輸出 NSUInteger,格式指定符為 %u 或是 %lu (long unsigned)
取得字串長度:
NSString *city = @"Taichung";
NSUInteger cityLength = [city length];
NSLog(@"City has %u characters", cityLength)
result:
City has 8 characters

數學運算:
NSNumber *price = [NSNumber numberWithInt: 20];
NSNumber *amount = [NSNumber numberWithInt: 6];

NSUInteger priceInt = [price unsignedIntValue];
NSUInteger amountInt = [amount unsignedIntValue];
NSUInteger total = priceInt * amountInt;

NSLog(@"Total price is %lu", total);
result:
Total price is 120
NSUInteger 的宣告無 * 符號

輸出 NSInteger,格式指定符為 %d 或是 %ld (long),取值訊息為 intValue。

Objective-C 陣列 ( NSArray )

宣告一個 NSArray 物件,並透過傳遞 description 訊息給 NSArray 物件,印出陣列元素:
#import <Foundation/Foundation.h>

int main(){
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSArray *foods =  [NSArray arrayWithObjects:@"tacos", @"burgers",nil];
    NSString *result = [foods description];

    NSLog(@"%@", result);
   
    [pool drain];
    return 0;
}
result:
(tacos, burgers)
陣列結尾須為 nil。

Objective-C 訊息傳遞 ( message passing )

在 C++ 裡呼叫方法,通常寫成:
myObj.method(argument);
或是:
myObj->method(argumennt);

而在 Objective-C裡,稱為傳遞訊息,語法為:
[myObj method: argument];

2013年4月30日 星期二

VB.net 數字補0(leading zero)

Dim myInt as Int32 = 9
Dim myString as String = myInt.ToString("D2") // 09
myString = String.Format("{0:000}", myInt) // 009
下面的寫法無效:
Dim myInt as Int32 = 9
Dim myString as String = myInt.ToString()
myString = myString.ToString("D2") // 編譯錯誤
myString = String.Format("{0:00}", myString) // 9

2013年3月6日 星期三

JavaScript 保留字 (Reserved Keyword)

在我的 js 裡有一行:
var history = ["ele1", "ele2"];
在 Chrome 沒問題,但 IE9 失效,
查了一下才發現,是使用了保留字的關係(忘了history.back()這東東...)

JS 保留字列表: http://www.quackit.com/javascript/javascript_reserved_words.cfm

不過試了一下也不是每個都不能用,但總之 history 不能用就是了。

2013年3月4日 星期一

JavaScript 產生亂數

Math.floor((Math.random()*10)+1); // 1 ~ 10 之間的亂數

Math.floor((Math.random()*100)+1); // 1 ~ 100 之間的亂數
Math.random();  // 傳回 0 - 1 之間的數

Math.floor(x);  // 傳回小於或等於 x 的最大整數

參考: http://www.w3schools.com/jsref/jsref_random.asp

2013年3月3日 星期日

TextBox 垂直置中

css:
input[type=text]{
    heigth:24px;
    line-height:24px;
}

2013年3月1日 星期五

Repeater 取得 Header 控制項

假設一個 Repeater 的 HeaderTemplate 包含一個 TextBox 控制項
<asp:Repeater ID="Repeater1" runat="server" EnableViewState="False" >
    <HeaderTemplate>
        <asp:TextBox ID="TextBox1" runat="server" />
    </HeaderTemplate>
</asp:Repeater>
in code-behind:
var myBox = (TextBox)Repeater1.Controls[0].Controls[0].FindControl("TextBox1");
myBox.Text = "some text";
注意需設定 EnableViewState="False"

參考: stackoverflow

後來 google 到另一個方法: http://readily-notes.blogspot.com/

2013年2月26日 星期二

ASP.NET 彈出視窗

string myMessage = "popup me";
Page.ClientScript.RegisterStartupScript(this.GetType(), "popup", "alert(' " +myMessage+ " ');", true);

2013年2月25日 星期一

Repeater 綁定資料

in code-behind:
string cnString = System.Configuration.ConfigurationManager.ConnectionStrings["cnString"].ConnectionString;
string queryString = "select * from employees";
SqlConnection con = new SqlConnection(cnString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, con);

DataSet ds = new DataSet();
adapter.Fill(ds, "employees");
Repeater1.DataSource = ds.Tables["employees"];
Repeater1.DataBind();

default.aspx:
<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <table>
           <tr>
               <th>ID</th>
               <th>Last Name</th>
               <th>First Name</th>
               <th>Hired Date</th>
           </tr>
</HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><%# Eval("employeeid") %></td>
            <td><%# Eval("lastname") %></td>
            <td><%# Eval("firstname") %></td>
            <td><%# Eval("hiredate") %></td>
        </tr>
   </ItemTemplate>

   <FooterTemplate>
       </table>
   </FooterTemplate>
</asp:Repeater>

在 web.config 設定連線字串

in web.config:
<configuration>
    <connectionStrings>
        <add name="cnString" connectionString="myConnectionString">
    </connectionStrings>
</configuration>
in code-behind:
using System.Configuration;
string connectString = ConfigurationManager.ConnectionStrings["cnString"].ConnectionString;

2013年2月23日 星期六

取得 Repeater 裡的控制項

假設一個 Repeater 裡包含一個超連結:
<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:HyperLink ID="linkInRepeater" runat="server">link</asp:HyperLink>
    </ItemTemplate>
</asp:Repeater>

要取得 HyperLink 控制項,需添加 OnItemDataBound 屬性給 Repeater :
<asp:repeater ID="Repeater1" runat="server" OnItemDataBound="getControl" >

接著在 .cs 裡設定 getControl :
protected void getControl(object sender, RepeaterItemEventArgs e) {
    HyperLink myLink = (HyperLink)e.Item.FindControl("linkInRepeater");
    myLink.Text = "change text"; // 變更連結文字
}

2013年2月19日 星期二

CalendarExtender with BoundField

假設在GridView中有個輸入欄位,格式為日期:
<asp:BoundField DataField="hireDate" HeaderText="Date Hired" />

2013年2月17日 星期日

C# Double Output Digit

double number = 1.83333;
label1.Text = sum.ToString("0.##");
result:
1.83

C# 產生亂數

Random rnd1 = new Random();
int month = rnd1.Next(1, 13);    // 產生介於 1~12 的亂數
int number = rnd1.Next(100);    // 產生介於 0~99 的亂數