Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GridView RowCommand is not firing

Posted on 2013-01-07
4
Medium Priority
?
834 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 1400 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 600 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

670 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