Avatar of KavyaVS
KavyaVS

asked on 

GridView RowCommand is not firing

Hi,
This is the GridView markup and Grid_RowCommand is not firing.
When LinkButton is clicked it has to fire the Grid_RowCommand. But it  is not.

Please look into the markup and code and let me know your suggestions.

<asp:GridView ID="grd1" runat="server" SkinID="GridView10RowsSortAndPage" OnRowCommand="Grid_RowCommand"
                                                                            OnRowDataBound="Grid_RowDataBound">
                                                                            <Columns>
                                                                                <asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number" />
                                                                                <asp:TemplateField HeaderText="Amount">
                                                                                    <ItemTemplate>
                                                                                        <asp:LinkButton ID="btnAmt" runat="server" Text='<%# Bind("AMOUNT")%>'
                                                                                            CommandName="Select"></asp:LinkButton>
                                                                                    </ItemTemplate>
                                                                                   
                                                                                </asp:TemplateField>
                                                                                <asp:TemplateField HeaderText="Type">
                                                                                    <ItemTemplate>
                                                                                        <asp:Label ID="lblDesc" runat="server" Text='<%# Bind("DESCRIPTION")%>'></asp:Label>
                                                                                    </ItemTemplate>
                                                                                 
                                                                                </asp:TemplateField>
                                                                                <asp:TemplateField HeaderText="Date">
                                                                                    <ItemTemplate>
                                                                                        <asp:Label ID="lbldate" runat="server" Text='<%# Bind("Date")%>'></asp:Label>
                                                                                    </ItemTemplate>
                                                                                 
                                                                                </asp:TemplateField>
                                                                                <asp:TemplateField HeaderText="Time">
                                                                                    <ItemTemplate>
                                                                                        <asp:Label ID="lbltime" runat="server" Text='<%# Bind("TIME")%>'></asp:Label>
                                                                                    </ItemTemplate>
                                                                                 
                                                                                </asp:TemplateField>
                                                                               
                                                                                <asp:TemplateField HeaderText="Name">
                                                                                    <ItemTemplate>
                                                                                        <asp:Label ID="lblname" runat="server" Text='<%# Bind("Name")%>'></asp:Label>
                                                                                    </ItemTemplate>
                                                                                   
                                                                                </asp:TemplateField>
                                                                            </Columns>
                                                                        </asp:GridView>



        protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)

        {

            GridView grd = (GridView)sender;

            try

            {

                int iRowIndex = Convert.ToInt32(e.CommandArgument);

                switch (grd.ID.ToString())

                {

                   
                    case "grd1":

                        {

                            LinkButton lnk = (LinkButton)grd1.Rows[iRowIndex].Cells[1].Controls[0];

                            ViewState["SelectedRow"] = grd1.Rows[iRowIndex].Cells[0].Text.Trim();

                            txtDate.Text = grd1.Rows[iRowIndex].Cells[3].Text.Trim();

                            txtAmount.Text = lnk.Text.Trim();

                            txtType.Text = grdTrans.Rows[iRowIndex].Cells[2].Text.Trim();

                           
                            break;

                        }

                }

            }

            catch (Exception ex)

            {

                string s = ex.Message;

            }

            finally

            {

               

            }

        }
 
Do I need to change anything please.

Thanks
ASP.NETC#

Avatar of undefined
Last Comment
KavyaVS
ASKER CERTIFIED SOLUTION
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of KavyaVS
KavyaVS

ASKER

I wired up the ItemDataBound event to Grid. But it is not even firing the ItemDataBound event.And Link button click doesn't firing the Grid_RowCommand.
<asp:GridView ID="grd1" runat="server" SkinID="GridView10RowsSortAndPage" OnRowCommand="Grid_RowCommand"
                                                                            OnRowDataBound="Grid_RowDataBound" onItemDataBound="Grid_ItemDataBound">

    protected void Grid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {

           
             GridView grd = (GridView)sender;
             try
             {
               
                 switch (grd.ID.ToString())
                 {
                     case "grd1":
                         {
                             if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                             {
                                 LinkButton lnkAmt= e.Item.FindControl("btnAmt") as LinkButton;
                                 lnkAmt.Attributes.Add("onclick", "return true;");
                             }
                             break;
                         }
                 }
             }
             catch (Exception ex)
             {
                 string s = ex.Message;
             }
             finally
             {
                 
             }
        }


Any suggestions please.

Thanks.
SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of KavyaVS
KavyaVS

ASKER

Thanks
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo