1 回答

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊
我在本地創(chuàng)建了一個(gè)項(xiàng)目,這沒(méi)問(wèn)題。
我有一個(gè)名為 TestUserControl.ascx 的控件。我在設(shè)計(jì)模式下將一個(gè) GridView 控件拖到用戶控件上,并將其命名為“grdControlsGrid”。
這生成了以下標(biāo)記。
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="TestASPNet.TestUserControl" %>
<asp:GridView ID="grdControlsGrid" runat="server">
</asp:GridView>
然后我通過(guò)在 runat="server" 之后向 hmtl 鍵入“OnRowDataBound=”來(lái)添加事件 OnRowDataBound。當(dāng)您點(diǎn)擊 equals 時(shí),它會(huì)為您提供為此事件創(chuàng)建方法的選項(xiàng)。雙擊“創(chuàng)建方法”選項(xià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)通過(guò)標(biāo)記自動(dòng)連接時(shí),您會(huì)收到事件“OnRowDataBound”..但是在手動(dòng)后面的代碼中完成時(shí),您會(huì)得到“RowDataBound”。我的猜測(cè)是它們是相同的..但也許其他人可以闡明這一點(diǎn)。
希望有幫助。
- 1 回答
- 0 關(guān)注
- 167 瀏覽
添加回答
舉報(bào)