Protected ReadOnly Property Events() As System.ComponentModel.EventHandlerList
ControlsのイベントリストはProtected Readonlyなので、
コントロールにイベントハンドラを追加する場合、直接Add、Remove、Clearはできないです。
これで、Eventの処理順は確保できます。
だが、Eventの処理順を動的に変わりたい場合になると、
簡単な方法はControlに継承して、Eventsに操作方法を追加するしかないでしょう。
2011年2月19日星期六
【VB.net】AddEventHandler事件处理方法的设定
‘通用按钮事件处理方法的设定
<System.Runtime.CompilerServices.Extension()> _
Public Sub RegistButtonClick(ctrs As Control(), btnClick As EventHandler)
Dim lstButtons = ctrs.Where(Function(btn) TypeOf btn is Button)
For Each btn As Button In lstButtons
AddHandler btn.Click, btnClick
Next
End Sub
'画面启动时设定所有按钮的click处理
<System.Runtime.CompilerServices.Extension()>是net3.5的方法扩展功能、
需要在Module中设定。
<System.Runtime.CompilerServices.Extension()> _
Public Sub RegistButtonClick(ctrs As Control(), btnClick As EventHandler)
Dim lstButtons = ctrs.Where(Function(btn) TypeOf btn is Button)
For Each btn As Button In lstButtons
AddHandler btn.Click, btnClick
Next
End Sub
'画面启动时设定所有按钮的click处理
private sub Onload()
me.controls.RegistButtonClick(AddressOf CommonClick )
end sub
’处理方法的先后顺序参照设定的顺序。
end sub
’处理方法的先后顺序参照设定的顺序。
‘本方法在initionalizeComponent里优先设定过,所以先处理。
private sub btn1_Click (sender As object , e As EventArgs) Handler btn1.Click
Console.WriteLine("1")
end sub
'共通処理方法
private sub CommonClick (sender As object , e As EventArgs) Handler btn1.Click
Console.WriteLine("99")
end sub
处理結果顺序:
1
99
private sub btn1_Click (sender As object , e As EventArgs) Handler btn1.Click
Console.WriteLine("1")
end sub
'共通処理方法
private sub CommonClick (sender As object , e As EventArgs) Handler btn1.Click
Console.WriteLine("99")
end sub
处理結果顺序:
1
99
<System.Runtime.CompilerServices.Extension()>是net3.5的方法扩展功能、
需要在Module中设定。
2011年2月14日星期一
UpdatePanel的精髓
UpdatePanel,最近才算是明白该怎么用,我觉得算是明白其根本,暂且叫成精髓吧。
其实就是一句话: 外层变全部,里层变部分。想让变全部,就不要把trigger放到里头去。
例如
年龄变更的时候,父子都得变,而给儿子改名字,就没必要通知爸爸年龄变了。
<updatepanl id ="father" updatemode="congdition" runat="server">
<ContentTemplate>
<input type="button" Text="ReAge">Father age:30
<updatepanl id ="son" updatemode="congdition" runat="server">
<ContentTemplate>
<input type="buttonSon" Text="ReName">Son name: abc, age:1
<ContentTemplate>
</updatepanl>
<ContentTemplate>
</updatepanl>
其实就是一句话: 外层变全部,里层变部分。想让变全部,就不要把trigger放到里头去。
例如
年龄变更的时候,父子都得变,而给儿子改名字,就没必要通知爸爸年龄变了。
<updatepanl id ="father" updatemode="congdition" runat="server">
<ContentTemplate>
<input type="button" Text="ReAge">Father age:30
<updatepanl id ="son" updatemode="congdition" runat="server">
<ContentTemplate>
<input type="buttonSon" Text="ReName">Son name: abc, age:1
<ContentTemplate>
</updatepanl>
<ContentTemplate>
</updatepanl>
2011年2月10日星期四
【UpdatePanel RegisterStartupScript】Ajax部分刷新的封装
RegisterStartupScript and RegisterClientScriptBlock can work with UpdatePanel?!
=============================================================
Imagine you have a control which registers some trivial script, which has to be executed during loading of the page. To do that you will usually use following method:
page.ClientScript.RegisterStartupScript(…)
After some time, you decide to use the same control inside of UpdatePanel and find out that the script is not executed. To understand what the problem is please take a look on the following post. I investigated the problem with the release version of ATLAS and found out that the script is now even not rendered at all. The problem with UpdatePanel and registering of scripts is a little more trivial than expected.
The rendering of scripts by using of AJAX (ATLAS) works different way if UpdatePanel (or AJAX at all) is used. This is, because AJAX uses different rendering methods. For example, consider following code:
page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), “alert(‘hello’)”, true);
This code ensures that word ‘hello’ is alerted when the request to page ends up. However, if this code is used inside of UpdatePanel, the message ‘hello’ will not be alerted. To work around the problem there are related static methods in the class ScriptManager which should be used, when the control is used inside of UpdatePanel.
Following example shows how to do that:
ScriptManager.RegisterStartupScript(page, page.GetType(), Guid.NewGuid().ToString(), “alert(‘hello’)”, true);
The Second Method Also OK, "someAlert" is a function:
Dim sm As ScriptManager = ScriptManager.GetCurrent(Page)
If sm.IsInAsyncPostBack then
Page.ClientScript.RegisterStartupScript(Me.GetType(),"anotherKey","someAlert("hello");",true)
Else
Page.ClientScript.RegisterStartupScript(Me.GetType(), "anotherKey", "Sys.Application.add_load(someAlert);", true)
End If
=============================================================
Imagine you have a control which registers some trivial script, which has to be executed during loading of the page. To do that you will usually use following method:
page.ClientScript.RegisterStartupScript(…)
After some time, you decide to use the same control inside of UpdatePanel and find out that the script is not executed. To understand what the problem is please take a look on the following post. I investigated the problem with the release version of ATLAS and found out that the script is now even not rendered at all. The problem with UpdatePanel and registering of scripts is a little more trivial than expected.
The rendering of scripts by using of AJAX (ATLAS) works different way if UpdatePanel (or AJAX at all) is used. This is, because AJAX uses different rendering methods. For example, consider following code:
This code ensures that word ‘hello’ is alerted when the request to page ends up. However, if this code is used inside of UpdatePanel, the message ‘hello’ will not be alerted. To work around the problem there are related static methods in the class ScriptManager which should be used, when the control is used inside of UpdatePanel.
Following example shows how to do that:
ScriptManager.RegisterStartupScript(page, page.GetType(), Guid.NewGuid().ToString(), “alert(‘hello’)”, true);
The Second Method Also OK, "someAlert" is a function:
Dim sm As ScriptManager = ScriptManager.GetCurrent(Page)
If sm.IsInAsyncPostBack then
Page.ClientScript.RegisterStartupScript(Me.GetType(),"anotherKey","someAlert("hello");",true)
Else
Page.ClientScript.RegisterStartupScript(Me.GetType(), "anotherKey", "Sys.Application.add_load(someAlert);", true)
End If
2011年2月8日星期二
DateTextFormatString
Data Formatting for DataTextFormatString
Format String Description
{0:Cn} Currency. Displays numeric values in currency format with a leading dollar sign; n indicates the number of decimal places. If n is omitted, the default currency precision is two decimal digits.
{0:Dn} Decimal. Displays integer values; n indicates the minimum number of digits desired in the resulting string. If necessary, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.
{0:Fn} Fixed-point. Displays numeric values in fixed format; n indicates the desired number of decimal places.
{0:Nn} Number. The number is converted to a string; n indicates the desired number of decimal places. Commas are inserted between each group of three digits to the left of the decimal point.
{0:Pn} Percent. Displays numeric values in percentage format; n indicates the desired number of decimal places.
{0} Text. Displays a text string.
Custom Format Strings string.Format
If the standard numeric format specifiers do not provide the type of formatting required, you can use custom format strings, in effect, drawing a picture of the format using special characters to indicate number positions and precisions. The characters used to build a format string are shown in the following table.
Format Character Description
0 Zero placeholder. Displays a number in the position if one is present; otherwise, displays a leading or trailing zero. Significant digits to the left of a decimal point are always displayed in the absence of the placeholder.
# Digit placeholder. Displays a number in the position if one is present; otherwise, displays no decimal digit. Significant digits to the left of a decimal point are always displayed in the absence of the placeholder.
. Decimal point. Indicates the location of a decimal point in a formatted value.
, Thousand separator. Indicates the insertion of comma separators every three positions to the left of a decimal point.
% Percent placeholder. Converts a number to a percentage and displays percent sign in indicated position.
$ Dollar sign. Indicates position of a dollar sign to format currency values.
' ' Text characters enclosed as quotes are treated as literal strings and are inserted into the format at the indicated position.
Examples of using custom format strings to format numeric values are shown in the following table. For example, to use a format string for the ItemPrice BoundField in the above GridView, the specification can be DataFormatString="{0:$#,#.##}" to produce the same display as the standard currency string DataFormatString="{0:C}".
Value Format String Result
1234.567 {0:0.00} 1234.57
1234.567 {0:00000.0000} 01234.5670
1234.567 {0:#####.##} 1234.57
1234.567 {0:#.###} 1234.567
1234.567 {0:#.#} 1234.6
1234.567 {0:#,#.##} 1,234.57
1234.567 {0:$#,#.##} $1,234.57
1234.567 {0:$ #,#.##} $ 1,234.57
1234.567 {0:($ #,#.##)} ($ 1,234.57)
-1234.567 {0:#,#.##} -1,234.57
.1234 {0:#%} 12%
.1234 {0:Percent = #.0%} Percent = 12.3%
Format String Description
{0:Cn} Currency. Displays numeric values in currency format with a leading dollar sign; n indicates the number of decimal places. If n is omitted, the default currency precision is two decimal digits.
{0:Dn} Decimal. Displays integer values; n indicates the minimum number of digits desired in the resulting string. If necessary, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.
{0:Fn} Fixed-point. Displays numeric values in fixed format; n indicates the desired number of decimal places.
{0:Nn} Number. The number is converted to a string; n indicates the desired number of decimal places. Commas are inserted between each group of three digits to the left of the decimal point.
{0:Pn} Percent. Displays numeric values in percentage format; n indicates the desired number of decimal places.
{0} Text. Displays a text string.
Custom Format Strings string.Format
If the standard numeric format specifiers do not provide the type of formatting required, you can use custom format strings, in effect, drawing a picture of the format using special characters to indicate number positions and precisions. The characters used to build a format string are shown in the following table.
Format Character Description
0 Zero placeholder. Displays a number in the position if one is present; otherwise, displays a leading or trailing zero. Significant digits to the left of a decimal point are always displayed in the absence of the placeholder.
# Digit placeholder. Displays a number in the position if one is present; otherwise, displays no decimal digit. Significant digits to the left of a decimal point are always displayed in the absence of the placeholder.
. Decimal point. Indicates the location of a decimal point in a formatted value.
, Thousand separator. Indicates the insertion of comma separators every three positions to the left of a decimal point.
% Percent placeholder. Converts a number to a percentage and displays percent sign in indicated position.
$ Dollar sign. Indicates position of a dollar sign to format currency values.
' ' Text characters enclosed as quotes are treated as literal strings and are inserted into the format at the indicated position.
Examples of using custom format strings to format numeric values are shown in the following table. For example, to use a format string for the ItemPrice BoundField in the above GridView, the specification can be DataFormatString="{0:$#,#.##}" to produce the same display as the standard currency string DataFormatString="{0:C}".
Value Format String Result
1234.567 {0:0.00} 1234.57
1234.567 {0:00000.0000} 01234.5670
1234.567 {0:#####.##} 1234.57
1234.567 {0:#.###} 1234.567
1234.567 {0:#.#} 1234.6
1234.567 {0:#,#.##} 1,234.57
1234.567 {0:$#,#.##} $1,234.57
1234.567 {0:$ #,#.##} $ 1,234.57
1234.567 {0:($ #,#.##)} ($ 1,234.57)
-1234.567 {0:#,#.##} -1,234.57
.1234 {0:#%} 12%
.1234 {0:Percent = #.0%} Percent = 12.3%
2011年2月4日星期五
[VB.NET] Select Case示例
’可以使用比较符号
Select Case NUM
Case Is < 10
Do0
Case Is <100 '=10 TO 99
Do1
Case 100 To 999 '= Is <1000
Do2
Do2
Case 1000 To 9999, 20000, 30000 '複数でOK
Do3
Case Else
DoElse
End Select
‘甚至像IF一样。
Select Case true
Case (value >= 0 andalso value <= 49.99)
Debug.WriteLine("first group")
Case (value >= 50 andalso value <= 99.99)
Debug.WriteLine("second group")
Case (value >= 100 andalso value <= 199.99)
Debug.WriteLine("third group")
Case Else
Debug.WriteLine("fourth group")
End Select
Case (value >= 0 andalso value <= 49.99)
Debug.WriteLine("first group")
Case (value >= 50 andalso value <= 99.99)
Debug.WriteLine("second group")
Case (value >= 100 andalso value <= 199.99)
Debug.WriteLine("third group")
Case Else
Debug.WriteLine("fourth group")
End Select
订阅:
博文 (Atom)