1 回答
![?](http://img1.sycdn.imooc.com/533e4d00000171e602000200-100-100.jpg)
TA贡献1816条经验 获得超4个赞
我在本地创建了一个项目,这没问题。
我有一个名为 TestUserControl.ascx 的控件。我在设计模式下将一个 GridView 控件拖到用户控件上,并将其命名为“grdControlsGrid”。
这生成了以下标记。
<%@ 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。当您点击 equals 时,它会为您提供为此事件创建方法的选项。双击“创建方法”选项,这将为您将事件连接到方法。
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="TestASPNet.TestUserControl" %>
<asp:GridView ID="grdControlsGrid" runat="server" OnRowDataBound="grdControlsGrid_OnRowDataBound">
</asp:GridView>
根据下面的代码,此代码现在位于您的用户控件中。
或者,您可以在用户控件负载中自行连接事件。
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
}
}
出于某种原因,当通过标记自动连接时,您会收到事件“OnRowDataBound”..但是在手动后面的代码中完成时,您会得到“RowDataBound”。我的猜测是它们是相同的..但也许其他人可以阐明这一点。
希望有帮助。
- 1 回答
- 0 关注
- 143 浏览
添加回答
举报