Solved

GridView RowCommand is not firing

Posted on 2013-01-07
4
814 Views
Last Modified: 2013-01-07
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
0
Comment
Question by:KavyaVS
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 350 total points
ID: 38751678
Add commandargument on the button,
<asp:LinkButton ID="btnAmt" runat="server" Text="Click"
                                                                                            CommandName="Select" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' ></asp:LinkButton>

Open in new window


Make sure, you display any error  on catch block .


  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;
                Response.Write(s.toString());

            }

            finally

            {

                

            }

        }

Open in new window

0
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 150 total points
ID: 38751917
check to make sure ViewState is not disabled
0
 

Author Comment

by:KavyaVS
ID: 38752031
Hi,
I added the CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'. But it is still not firing Grid_RowCommand.
How can we display the error in the catch block,it's not entering into try block.

ViewState is enabled.

I changed the link button from template column to buttonfield in the .aspx page.Now the row command is firing.How to access the button filed in the row command.
 I tried the following.
<asp:ButtonField  runat="server" ButtonType="Link" headerText="Amount" datatextfield="AMOUNT" CommandName="Select" />

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":

                        {
                             int index = Convert.ToInt32(e.CommandArgument);
                                GridViewRow mySelectedRow = (GridViewRow)grdTrans.Rows[index];
                                txtAmount.Text = ((LinkButton)mySelectedRow.Cells[1].Controls[0]).Text;
                                txtType.Text = ((TextBox)mySelectedRow.FindControl("lblDesc")).Text;
                                txtDate.Text = ((TextBox)mySelectedRow.FindControl("lbldate")).Text;
                               
                            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

            {

               

            }

        }

I am getting the argumentoutofrange exception.
How to access the selected row in gridview row command and button filed in the selected row.


Thanks
0
 

Author Closing Comment

by:KavyaVS
ID: 38752705
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now