Solved

GridView RowCommand is not firing

Posted on 2013-01-07
4
812 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
Comment Utility
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
Comment Utility
check to make sure ViewState is not disabled
0
 

Author Comment

by:KavyaVS
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 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

13 Experts available now in Live!

Get 1:1 Help Now