简体   繁体   English

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

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

I Have a user control as a grid view. 我有一个用户控件作为网格视图。 I am adding it dynamically...i have made the method for data source but i am unable to use others events of grid view like "Row_Data_Bound" etc. 我正在动态添加它...我已经为数据源创建了方法,但是我无法使用网格视图的其他事件,例如“ Row_Data_Bound”等。

I have seen the code on net which says create delegates and add following 我已经看过网上的代码,上面写着创建委托并添加以下内容

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

but i get an error here saying The name OnRowDataBound does not exist in the current context 但我在这里得到一个错误,说名称OnRowDataBound在当前上下文中不存在

Can anyone help me out with accessing the events of user control grid view in the parent page 谁能帮助我访问父页面中用户控件网格视图的事件

EDIT: 编辑:

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

this is my ascx page...i want to use this grid view at more than one place(at run time) so i have created a user-control... Basically i want to call this user control from my code behind and then using its rowdatabound i want to data to be bind with the gridview.. 这是我的ascx页面。使用它的rowdatabound我想将数据与gridview绑定。

i saw on websites it says use events bubbling...but i do not know how to implement that. 我在网站上看到它说使用事件冒泡...但是我不知道如何实现。

so can u help in that matter..so that i can use rowdatabound normally as i do 所以你能在那件事上帮忙..以便我可以像我一样正常使用rowdatabound

thnx in advance 提前thnx

I've created a project locally and this is ok. 我在本地创建了一个项目,没关系。

I've a control called TestUserControl.ascx. 我有一个名为TestUserControl.ascx的控件。 I dragged a GridView control onto the user control in design mode and called it "grdControlsGrid". 我在设计模式下将GridView控件拖到用户控件上,并将其称为“ grdControlsGrid”。

This generated the below mark up. 这产生了以下标记。

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

Then I added the event OnRowDataBound by typing "OnRowDataBound=" to the hmtl after runat="server". 然后,我通过在runat =“ server”之后向hmtl输入“ OnRowDataBound =”来添加事件OnRowDataBound。 When you hit equals it gives you the option to create the method for this event. 当您按下equals时,它将为您提供创建此事件的方法的选项。 Double click the "Create method" option and this will wire up the event to the method for you. 双击“创建方法”选项,这会将事件关联到该方法。

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

This code now resides in your user control as per the code below. 现在,根据以下代码,此代码位于用户控件中。

Alternatively you can wire the event up yourself in the users controls load. 或者,您可以在用户控件负载中自行关联事件。

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

for some reason when auto wiring via the markup you get the event "OnRowDataBound".. but when done in the code behind manually you get "RowDataBound". 出于某种原因,当通过标记自动接线时,会得到事件“ OnRowDataBound”。但是,在手动执行的背后代码中,则会得到“ RowDataBound”。 my guess is that they are one of the same.. but maybe someone else can shed light on that. 我的猜测是它们是相同的..但是也许其他人可以对此有所了解。

Hope that helps. 希望能有所帮助。

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

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