1 回答

TA貢獻(xiàn)1816條經(jīng)驗 獲得超4個贊
我在本地創(chuàng)建了一個項目,這沒問題。
我有一個名為 TestUserControl.ascx 的控件。我在設(shè)計模式下將一個 GridView 控件拖到用戶控件上,并將其命名為“grdControlsGrid”。
這生成了以下標(biāo)記。
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="TestASPNet.TestUserControl" %>
<asp:GridView ID="grdControlsGrid" runat="server">
</asp:GridView>
然后我通過在 runat="server" 之后向 hmtl 鍵入“OnRowDataBound=”來添加事件 OnRowDataBound。當(dāng)您點擊 equals 時,它會為您提供為此事件創(chuàng)建方法的選項。雙擊“創(chuàng)建方法”選項,這將為您將事件連接到方法。
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="TestASPNet.TestUserControl" %>
<asp:GridView ID="grdControlsGrid" runat="server" OnRowDataBound="grdControlsGrid_OnRowDataBound">
</asp:GridView>
根據(jù)下面的代碼,此代碼現(xiàn)在位于您的用戶控件中。
或者,您可以在用戶控件負(fù)載中自行連接事件。
public partial class TestUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
//Manually Add event handler
grdControlsGrid.RowDataBound += GrdControlsGrid_RowDataBound;
}
private void GrdControlsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Manually bound event
}
protected void grdControlsGrid_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
//Auto wired event
}
}
出于某種原因,當(dāng)通過標(biāo)記自動連接時,您會收到事件“OnRowDataBound”..但是在手動后面的代碼中完成時,您會得到“RowDataBound”。我的猜測是它們是相同的..但也許其他人可以闡明這一點。
希望有幫助。
- 1 回答
- 0 關(guān)注
- 160 瀏覽
添加回答
舉報