BulletedList.OnClick(BulletedListEventArgs) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
引發 BulletedList 控制項的 Click 事件。
protected:
virtual void OnClick(System::Web::UI::WebControls::BulletedListEventArgs ^ e);
protected virtual void OnClick (System.Web.UI.WebControls.BulletedListEventArgs e);
abstract member OnClick : System.Web.UI.WebControls.BulletedListEventArgs -> unit
override this.OnClick : System.Web.UI.WebControls.BulletedListEventArgs -> unit
Protected Overridable Sub OnClick (e As BulletedListEventArgs)
參數
BulletedListEventArgs,其中包含事件資料。
範例
下列程式代碼範例示範如何為控件的事件BulletedList指定和撰寫事件處理程式Click的程序代碼。 按兩下清單專案時,會在頁面上的控件中 Label 顯示訊息。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>BulletedList Click Example</title>
<script runat="server">
void ItemsBulletedList_Click(object sender, System.Web.UI.WebControls.BulletedListEventArgs e)
{
// Change the message displayed in the label based on the index
// of the list item that was clicked.
switch (e.Index)
{
case 0:
Message.Text = "You clicked list item 1.";
break;
case 1:
Message.Text = "You clicked list item 2.";
break;
case 2:
Message.Text = "You clicked list item 3.";
break;
default:
throw new Exception("You did not click a valid list item.");
break;
}
}
</script>
</head>
<body>
<h3>BulletedList Click Example</h3>
<form id="form1" runat="server">
<p>Click on an item in the list to raise the Click event.</p>
<asp:BulletedList id="ItemsBulletedList"
BulletStyle="Disc"
DisplayMode="LinkButton"
OnClick="ItemsBulletedList_Click"
runat="server">
<asp:ListItem Value="http://www.cohowinery.com">Coho Winery</asp:ListItem>
<asp:ListItem Value="http://www.contoso.com">Contoso, Ltd.</asp:ListItem>
<asp:ListItem Value="http://www.tailspintoys.com">Tailspin Toys</asp:ListItem>
</asp:BulletedList>
<asp:Label id="Message"
Font-Size="12"
Width="168px"
Font-Bold="True"
runat="server"
AssociatedControlID="ItemsBulletedList"/>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>BulletedList Click Example</title>
<script runat="server">
Sub ItemsBulletedList_Click(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.BulletedListEventArgs)
' Change the message displayed in the label based on the index
' of the list item that was clicked.
Select Case (e.Index)
Case 0
Message.Text = "You clicked list item 1."
Case 1
Message.Text = "You clicked list item 2."
Case 2
Message.Text = "You clicked list item 3."
Case Else
Throw New Exception("You did not click a valid list item.")
End Select
End Sub
</script>
</head>
<body>
<h3>BulletedList Click Example</h3>
<form id="form1" runat="server">
<p>Click on an item in the list to raise the Click event.</p>
<asp:BulletedList id="ItemsBulletedList"
BulletStyle="Disc"
DisplayMode="LinkButton"
OnClick="ItemsBulletedList_Click"
runat="server">
<asp:ListItem Value="http://www.cohowinery.com">Coho Winery</asp:ListItem>
<asp:ListItem Value="http://www.contoso.com">Contoso, Ltd.</asp:ListItem>
<asp:ListItem Value="http://www.tailspintoys.com">Tailspin Toys</asp:ListItem>
</asp:BulletedList>
<asp:Label id="Message"
Font-Size="12"
Width="168px"
Font-Bold="True"
runat="server"
AssociatedControlID="ItemsBulletedList"/>
</form>
</body>
</html>
備註
Click按兩下 控制項中的BulletedList清單專案時,就會引發事件。 若要讓控制項中的 BulletedList 清單項目引發 Click 事件,您必須先將 DisplayMode 屬性設定為 值 LinkButton。
引發事件會透過委派叫用此事件處理常式。 如需如何處理事件的詳細資訊,請參閱 處理和引發事件。
OnClick 方法也允許衍生類別處理事件,而不用附加委派。 這是在衍生類別中處理事件的慣用技巧。
給繼承者的注意事項
在 OnClick(BulletedListEventArgs) 衍生類別中覆寫 方法時,請務必呼叫 OnClick(BulletedListEventArgs) 基類的方法,讓已註冊的委派接收 事件。