Solved

GridView RowCommand is not firing

Posted on 2013-01-07
4
828 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 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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 …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

635 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