繁体   English   中英

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

[英]How to Handle Events of User Control Grid View?

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

我已经看过网上的代码,上面写着创建委托并添加以下内容

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

我在本地创建了一个项目,没关系。

我有一个名为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”。 我的猜测是它们是相同的..但是也许其他人可以对此有所了解。

希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM