hi

i have create one usercontrol in that i get one grid view and that usercontrol i have use in my page gridview template.............

my problem is how to handle event of user control grid view?

plase tell me:(

my code

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="topic.ascx.cs" Inherits="Admin_topic" %>


<div style="padding-left:15px">

<asp:GridView ID="gvtopic12" runat="server" AutoGenerateColumns="false" AllowSorting="True"
                                     AllowPaging="false" PageSize="20" PagerSettings-Position="TopAndBottom"
                                    PagerStyle-HorizontalAlign="Right"  
        CssClass="listTable" GridLines="Both" 

                                       DataKeyNames="TopicID"  Width="100%"  
        OnRowDataBound="SetgvCassessColumnValues" onrowdeleting="gvtopic12_RowDeleting"     
         >
                                    <Columns>
                               <asp:BoundField ItemStyle-Width="10%" DataField="topicid" HeaderText="Topic Id"  />

                                    <asp:BoundField  ItemStyle-Width="25%"  DataField="topicname"  HeaderText="Topic Name" />
                                    <asp:TemplateField HeaderText="Description"  ItemStyle-Width="65%" >
                                    <ItemTemplate >
<asp:Literal ID="litdescripitonshow" runat="server" Text='<%# Eval("Topicdescription").ToString() %>'></asp:Literal>

                                  </ItemTemplate>
                                    </asp:TemplateField>
                                       <asp:TemplateField HeaderText="Delete?">

                        <ItemTemplate>

                            <span onclick="return confirm('Are you sure to Delete the record?')">

                                <asp:LinkButton ID="lnkB" runat="Server" Text="Delete" CommandName="Delete"><img id="imglog" src="../../images/delete.gif" border="0" /></asp:LinkButton>

                            </span>

                        </ItemTemplate>

                    </asp:TemplateField>

                                    </Columns>
                                    <SelectedRowStyle BackColor="#9B9ECA" />
                                </asp:GridView>
</div>

and these user control use in parent page
that code is in

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="managetopic.aspx.cs" EnableEventValidation="false" Inherits="Admin_managetopic" MasterPageFile="~/UI/MyCassess/EMWDMain.master" %>
<%@ Register Src="~/UI/Admin/topic.ascx" TagName="Topic" TagPrefix="uc1" %>
<asp:Content ContentPlaceHolderID="head" ID="PAGEhead" runat="server">

<script language="javascript" type="text/javascript" src="../../jsgrid/effects.js"></script>    
<script language="javascript" type="text/javascript" src="../../jsgrid/prototype.js"></script>    
<script language="javascript" type="text/javascript" src="../../jsgrid/scriptaculous.js"></script>       
<script language="javascript" type="text/javascript" src="../../jsgrid/slider.js"></script> 

<link href="../../css/Stylesheet-2.css" rel="stylesheet" type="text/css" /> 

<script language="javascript" type="text/javascript">

var className = 'collapsed';    

// fires when the edit button is pushed! 
function edit() 
{
    // create the textboxes    
}

function getFileNameFromUrl(url) 
{
   wholeurl = url;
   x = wholeurl.length;
   while((wholeurl.substring(x,x-1)) != "/"){ x--; } 
   return wholeurl.substring(wholeurl.length,x);      
}

function toggleDetailsView(link) 
{
    // change HTML text to open and close

    var img = $(link).childElements()[0];     

    var imgFileName = getFileNameFromUrl(img.src); 

    img.src = imgFileName == 'tn_plus.GIF' ? 'tn_minus.GIF' : 'tn_plus.GIF';     

    var element = $(link).next("." + className);     

    $(element).toggleClassName('expanded');

} 



</script>


</asp:Content>
<asp:Content ID="gridcontent" runat="server" ContentPlaceHolderID="cphDetails">
    <div>
    <div style="text-align:left;">
    <a href="newtopic.aspx"><img src="../../images/btn_addnew.png" /> </a>
    <div style="clear:both;"></div>
    </div>
     <asp:GridView ID="gvtopic" runat="server" AutoGenerateColumns="false" AllowSorting="True"
                                     AllowPaging="false" PageSize="20" PagerSettings-Position="TopAndBottom"
                                    PagerStyle-HorizontalAlign="Right" 
                                    CssClass="listTable" GridLines="Both" 

                                       DataKeyNames="TopicID" Width="100%" 
            >
                                    <Columns>
                                    <asp:TemplateField HeaderText="Topic Name">
                                    <ItemTemplate>


                                        <a id="openCloseLink" onclick="toggleDetailsView(this)" href="#">
                                    <img src="tn_plus.GIF" style="border: 0px; padding:2px" alt="PLUS" />
                                   </a> 

                                     <span class="mainTitleStyle">
                                        <%# Eval("Topicname") %>
                                            </span>

                                      <div id="divDetailsView" runat="server" class="collapsed">
                             <uc1:Topic ID="topicchild" runat="server"  Parenttopic='<%# Bind("TopicID") %>' />
                                      </div>
                                    </ItemTemplate>

                                    </asp:TemplateField>
                                    </Columns>
                                    <SelectedRowStyle BackColor="#9B9ECA" />
                                </asp:GridView>
    </div>
  </asp:Content>

Dani AI

Generated

For : the clean, maintainable pattern is to have the user control raise a public event whenever its inner GridView fires a relevant server-side action, and have the page subscribe to that event for each child instance. This is exactly the events/delegates approach described, but using the built-in EventHandler<T> pattern with a small custom EventArgs makes the contract clearer and keeps the page and control loosely coupled.

Suggested flow (no verbatim code from the thread):

  • Inside the user control expose a public event, e.g. public event EventHandler<RowEventArgs> RowAction; and make RowEventArgs carry the TopicID/Command. When the inner GridView handles delete/command, populate those values and invoke the event (RowAction?.Invoke(this, args)). Avoid rebinding the inner grid on every postback so events are raised reliably.
  • On the page there are two practical ways to subscribe: (a) declaratively on the <uc:Topic> tag inside the ItemTemplate so each instance wires automatically (simplest), or (b) programmatically in the outer GridView’s RowCreated (preferred over RowDataBound for lifecycle safety): use FindControl("topicchild") and attach a handler. The handler receives the TopicID from the EventArgs and can refresh the outer GridView (rebind) as needed.

Note: ’s point about RowDeleted is useful inside the control for cleanup, but surface the action to the page with a custom event so the page can update its UI and maintain clear separation of responsibilities.

Recommended Answers

All 3 Replies

Hi Amar,

I'm a little confused by what you are trying to do here. Are you saying that you cannot get the row to delete? You have OnRowDeleting defined in your GridView, but the function stated there is not seen in the rest of the program.

Additionally, OnRowDeleting may not produce the results you are expecting. Try using the OnRowDeleted instead. With this, you can define a function to remove a row. Also, OnRowDeleted is meant to be used with the <asp:CommandField ShowDeleteButton /> control. Using these together may give you better results.

I'm not sure if that helps or not, but feel free to respond if it does not, and I will try to be of further (or any!) help.

hi ;
my issue is not about delete event ..........
i want how to handle event of gridview use in usercontrol and that usercontrol is use in template of gridview use in aspx (web) page.................

See you can't directly use the event of your gridview which is there in your custom user control. You muse need to use Events and Delegates to achieve this..

Handle the event in your custom control for Gridview.. Let say event for RowCommand()...
So code looks like below in your custom control for gridview...

public delegate void GridRowCommand(object sender, GridViewCommandEventArgs e);
public event GridRowCommand onGridRowCommand;

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
onGridRowCommand(sender, e);
}

Now on your file just register the event..

yourcontrolname.onGridRowCommand += new yourcontrolname.GridRowCommand(aspxRowCommand);

hi ;
my issue is not about delete event ..........
i want how to handle event of gridview use in usercontrol and that usercontrol is use in template of gridview use in aspx (web) page.................

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.