The LinkButton in Nested GridView is not responding to any event. How to solve this

Anoop Mathur 0 Reputation points
2024-01-12T11:04:16.3833333+00:00

The LinkButton in Nested GridView is not responding to any event. How to solve this code.pdf

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,407 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,619 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Albert Kallal 5,231 Reputation points
    2024-01-12T17:04:44.75+00:00

    Ok, you look to have a nested set of GridViews. However, before we delve deeper here? As a general rule, you don't have to use the built in GridView events, and in fact most cases the efforts to do so are not worth your time. In other words, just "ignore" the command name options and event model of the GridView. Just drop in a plane Jane regular button or even a link button. (no real need to use a link button, the regular button can be used). The "magic trick" then becomes how do you wire up a click event for these nested controls inside of the GridView? You can't double click on the button, so the approach is to THEN simply enter the event in the markup. Say this simple GridView with a link button (but, we can use a regular button if we wish here. In fact, most controls, even a check box, or combo box can be used and to setup the controls events, then just type in the event and "=" then then hit ctrl-space. That brings up the "magic" context menu, and thus you can now add a simple regular click event to the button, or link button.

    So, our markup looks like this:

        <asp:GridView ID="GVHotels" runat="server" AutoGenerateColumns="False"
            DataKeyNames="ID" CssClass="table table-hover"
            width="40%">
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" />
                <asp:BoundField DataField="City" HeaderText="City" />
                <asp:BoundField DataField="HotelName" HeaderText="HotelName" />
                <asp:BoundField DataField="Description" HeaderText="Description" />
                <asp:TemplateField HeaderText="View"
                    ItemStyle-HorizontalAlign="Center"
                    HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="120px" >
                    <ItemTemplate>
                        <asp:LinkButton ID="cmdView" runat="server"
                            CssClass="btn myshadow"
                            >                
                            <span aria-hidden="true" class="glyphicon glyphicon-file"></span>View
                        </asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle CssClass="GridPager" />
        </asp:GridView>
    
    

    The process looks like this:

    gclick So, now with above, we are free to write a simple regular click event. The next part is how to get the current row click in that event? The code looks like this:

            protected void cmdView_Click(object sender, EventArgs e)
            {
                LinkButton btn = (LinkButton)sender;
                GridViewRow gRow = (GridViewRow)btn.NamingContainer;
    
                int PK = (int)GVHotels.DataKeys[gRow.RowIndex]["ID"];
    
                string sInfo = $@"Database PK = {PK} <br/>
                                Row index click = {gRow.RowIndex}<br/>
                                Hotel Name = {gRow.Cells[3].Text}";
    
                LabelClickInfo.Text = sInfo;
            }
    
    

    And our code to load the GridView looks like this:

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                    LoadGrid();
            }
    
            void LoadGrid()
            {
                string strSQL = @"SELECT * FROM tblhotelsA
                                ORDER BY HotelName";
    
                GVHotels.DataSource = General.MyRst(strSQL);
                GVHotels.DataBind();    
            }
    
    

    And the end result looks like this: gclick2

    So note how we don't bother with the GridView "command" stuff at all. We ignore all that, drop in a button, or link button. Add a standard "regular" click event as per above. Then we use the "trick" of the "naming container" which allows you to pick up the current row click.


  2. Lan Huang-MSFT 28,831 Reputation points Microsoft Vendor
    2024-01-15T07:41:04.7733333+00:00

    Hi @Anoop Mathur, Maybe you can change the LinkButton to a ButtonField to successfully call the CommandName.

     <asp:ButtonField  HeaderText="update" Text="update"
         ButtonType="Button" CommandName="reportingUpdate" />
     <asp:ButtonField  HeaderText="Cancel" Text="Cancel"
         ButtonType="Button" CommandName="Cancel" />
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.