Get the value of Buttonfield in ASP.NET

4.7k Views Asked by At

Say I have only two columns in my GridView (ID & Name) and I need to get the value of ID whenever I click on it. For example, if I click on 123, I should get 123. But here... that's not the case... when I click on any ID it returns the name beside that ID right (eg; in case of 123, I get Tony Stark), but when I get the value of ID on click, it always returns me an empty string.

In other words, I get the name in id right (when X=1), but when X=0, id becomes an empty string... (int X and string id : see C# code below)

sample GridView1

XAML Code in my GridView:

                        <asp:ButtonField
                            DataTextField="ID"
                            HeaderText="ID">
                        </asp:ButtonField>

                        <asp:BoundField
                            DataField="Name"
                            HeaderText="NAME">
                        </asp:BoundField>

C#:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string id = GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[X].Text.ToString();
        Response.Write(id);
    }

What's going wrong?!! And how to fix this? Thanks!

3

There are 3 best solutions below

0
Steve On

In your RowCommand handler, I THINK this is what you want:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int id = (int)(DataBinder.Eval(e.Row.DataItem, "ID"));
        Response.Write(id);
    }
}

I'm converting from vb, so I may have missed some syntax, but you get the idea...

0
andre.rosseto.dev On

The more complex but better solution is:

In GridView, use DataKeyNames tag and RowCommand event and put the bind value in DataKeyName, example:

DataKeyNames="ID" OnRowCommand="GridView1_RowCommand" 

Use a Template Field in your gridview (you do not need use only imagebutton like as example below):

<asp:TemplateField HeaderText="Action">
                                <HeaderStyle HorizontalAlign="Center" />
                                <ItemStyle HorizontalAlign="Left" Wrap="false" />
                                <ItemTemplate>
                                    <div class="GridViewOpc">
                                        <asp:ImageButton ID="buttonDelete" runat="server" Width="17" Height="17" AlternateText="Update"
                                            CausesValidation="False" Visible="false" CommandName="Delete" Enabled="true"
                                            ImageUrl="~/yourDeletepicture.gif"  />

                                        <asp:ImageButton ID="buttonUpdate" runat="server" AlternateText="Update" Width="17"
                                            Height="17" CausesValidation="False" Visible="false" CommandName="Update" ImageUrl="~/yourUpdatePicture.gif" />
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>

And get it on CodeBehind:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

            Control ctl = e.CommandSource as Control;
            GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;

                if (e.CommandName == "Update")
                {

                    int ID = Convert.ToInt32(GridView1.DataKeys[CurrentRow.RowIndex][0]);
                }

               if (e.CommandName == "Delete")
                {

                    int ID = Convert.ToInt32(GridView1.DataKeys[CurrentRow.RowIndex][0]);

                }
    }
0
Rahul parmar On

Without using templateField you can also get the value from ButtonField. In your ButtonField you need to give it a ButtonType.

XML:

<asp:ButtonField ButtonType="Link" CommandName="PanelName" DataTextField="PANEL_NAME" HeaderText="Panel Name" runat="server" SortExpression="PANEL_NAME" text="PanleName"/>

Code:

if (e.CommandName == "PanelName")
{
    string PanleName;
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow selectedRow = dgSearchResult.Rows[index];
    PanleName = ((LinkButton)selectedRow.Cells[1].Controls[0]).Text;
}

Note: Whatever your cell index is, Cells[1] means index is 1. But Controls always start from 0 if only one control is in your GridView.