简体   繁体   中英

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.

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

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

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

thnx in advance

I've created a project locally and this is ok.

I've a control called TestUserControl.ascx. I dragged a GridView control onto the user control in design mode and called it "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". When you hit equals it gives you the option to create the method for this event. 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". my guess is that they are one of the same.. but maybe someone else can shed light on that.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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