locked
GridViewRow help! RRS feed

  • Question

  • User-1778293958 posted
    protected void Edit(object sender, EventArgs e)
    {
        using (GridViewRow row = (GridViewRow)((LinkButton)sender).Parent.Parent)
        {
            txtCustomerID.ReadOnly = true;
            txtCustomerID.Text = row.Cells[0].Text;
            txtContactName.Text = row.Cells[1].Text;
            txtCompany.Text = row.Cells[2].Text;            
            popup.Show();
        }
    }

    Can we convert above code block into GridViewRowEventArgs?
    It should be something like this:

    protected void GridView1_RowCommand(object sender, GridViewRowEventArgs e)
        {
            
            {
            }
        }

    Please hElp!!
    Thanks
    Thursday, June 2, 2011 2:51 PM

Answers

  • User626880745 posted

    I'm not sure about .NET 4.0, but in .NET 3.5, e.CommandSource is the GridView, not the LinkButton.

    not correct, it would work (cast to the control from a TemplateField).

    a GetType on e.CommandSource would show:

    e.CommandSource.GetType() {Name = "LinkButton" FullName = "System.Web.UI.WebControls.LinkButton"} System.Type {System.RuntimeType}

    Here's the sample (3.5, 4.0):

    <%@ Page Language="C#" AutoEventWireup="true" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = new[] {
                        new{ID = 1, Name="Name1"}
                    };
                GridView1.DataBind();
            }
        }
    
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "MyCommand")
            {
                LinkButton lbtn = e.CommandSource as LinkButton;
                if (lbtn != null)
                {
                    Response.Write("LinkButton - reference from ROWCOMMAND!");
    
                    GridViewRow row = lbtn.NamingContainer as GridViewRow;
                }
            }
        }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false"
                DataKeyNames="ID">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="MyCommand" Text='Link' />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
        </form>
    </body>
    </html>

    * It would be the GridView for others (CommandField, ButtonField)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 3, 2011 8:07 PM

All replies

  • User626880745 posted
    In the RowCommand event, e.CommandSource could be casted to a LinkButton. Take it's NamingContainer proper to cast to a GridViewRow. Also, being the RowCommand event, it would be good to check for e.CommandName prio (you would set the CommandName declaratively on the Link button). If the CommandName was edit, the RowEditing event too would give you the row to refer.
    Thursday, June 2, 2011 3:08 PM
  • User16212438 posted

    There is no easy way to get to the Row in the RowCommand handler. I'm not sure about .NET 4.0, but in .NET 3.5, e.CommandSource is the GridView, not the LinkButton.

    Typically, CommandArgument of the LinkButton is set to the RowIndex, either in markup, or in the RowDataBound handler. Then, in the RowCommand handler, that is read and used to find the Row again.

    Friday, June 3, 2011 6:29 AM
  • User626880745 posted

    I'm not sure about .NET 4.0, but in .NET 3.5, e.CommandSource is the GridView, not the LinkButton.

    not correct, it would work (cast to the control from a TemplateField).

    a GetType on e.CommandSource would show:

    e.CommandSource.GetType() {Name = "LinkButton" FullName = "System.Web.UI.WebControls.LinkButton"} System.Type {System.RuntimeType}

    Here's the sample (3.5, 4.0):

    <%@ Page Language="C#" AutoEventWireup="true" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = new[] {
                        new{ID = 1, Name="Name1"}
                    };
                GridView1.DataBind();
            }
        }
    
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "MyCommand")
            {
                LinkButton lbtn = e.CommandSource as LinkButton;
                if (lbtn != null)
                {
                    Response.Write("LinkButton - reference from ROWCOMMAND!");
    
                    GridViewRow row = lbtn.NamingContainer as GridViewRow;
                }
            }
        }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false"
                DataKeyNames="ID">
                <Columns>
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="MyCommand" Text='Link' />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
        </form>
    </body>
    </html>

    * It would be the GridView for others (CommandField, ButtonField)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 3, 2011 8:07 PM
  • User-1778293958 posted

    Thanks PeteNet, you are superb. Your answer make me understand the problems.
    also for superguppie, thanks for the reply, it helped. Now, I can use this code below and it works as I wanted it to be.

    protected void gvMemo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToString().ToUpper() == "CHANGE")
            {
                LinkButton lnkbtn = e.CommandSource as LinkButton;
                if (lnkbtn != null)
                {
                    GridViewRow row = lnkbtn.NamingContainer as GridViewRow;
                    objMemo.ID = Convert.ToInt32(row.Cells[0].Text);
                    objMemo.SelectById();
                    txtID.ReadOnly = true;
                    txtID.Text = objMemo.ID.ToString();
                    txtMemo.Text = objMemo.Memo;
                    popup.Show();
                }
            }
        }
    Saturday, June 4, 2011 5:02 PM
  • User16212438 posted

    Nice undocumented feature.....look what the msdn page about it says...They even use LinkButton as the source of the event in the example...

    Sunday, June 5, 2011 2:28 PM