如何处理用户控制网格视图的事件?

时间:2018-06-01 11:34:04

标签: c# asp.net gridview user-controls

我有一个用户控件作为网格视图。 我正在动态添加它...我已经为数据源创建了方法,但我无法使用其他网格视图事件,如“Row_Data_Bound”等。

我在net上看到了代码创建委托并添加以下内容的代码

protected void Simple_DGV_RowDataBound(object sender, GridViewRowEventArgs e)   
{
OnRowDataBound(e);
}

但我在此处收到错误当前上下文中不存在OnRowDataBound名称

任何人都可以帮助我访问父页面中用户控制网格视图的事件

编辑:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl1.ascx.cs" Inherits="ProjectManagement.UserControl.User1" %>
<div>
<asp:GridView ID="ucGridView" runat="server" SkinID="gvSchedulerSkin" AllowSorting="false" OnRowDataBound="ucGridView_RowDataBound">
   <RowStyle Width="20px" /> 
</asp:GridView>
</div>

这是我的ascx页面...我想在多个地方(运行时)使用此网格视图,所以我创建了一个用户控件... 基本上我想从我的代码后面调用这个用户控件然后使用它的rowdatabound我希望数据与gridview绑定..

我在网站上看到它说使用事件冒泡......但我不知道如何实现它。

所以你可以帮忙..因为我可以正常使用rowdatabound

提前thnx

1 个答案:

答案 0 :(得分:0)

我已经在本地创建了一个项目,这没问题。

我有一个名为TestUserControl.ascx的控件。我在设计模式下将GridView控件拖到用户控件上并调用它&#34; grdControlsGrid&#34;。

这产生了以下标记。

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="TestASPNet.TestUserControl" %>
<asp:GridView ID="grdControlsGrid" runat="server">
</asp:GridView>

然后我通过键入&#34; OnRowDataBound =&#34;添加了事件OnRowDataBound。在runat =&#34; server&#34;之后到hmtl。当您点击等于它时,它为您提供了为此事件创建方法的选项。双击&#34;创建方法&#34;选项,这会将事件连接到方法。

<%@ 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
        }
    }

出于某种原因,当通过标记自动接线时,你得到事件&#34; OnRowDataBound&#34; ..但是当在手动后面的代码中完成时,你得到&#34; RowDataBound&#34;。我的猜测是,他们是同一个......但也许其他人可以阐明这一点。

希望有所帮助。

相关问题