Site Search

Thursday, June 30, 2011

How to Pass value in to code behind when click on Gridview row - Server side

This will show how to pass key values in to code behind when you click on GridView row link button.

You have to use:
·         Command Name
·         OnRowCommand Event
·         TemplateField with Link button.
ASPX
·         Add row command in to gridview
·         OnRowCommand="GridViewRowCommand"
·         Add template field with asp link button and set the command name as you wish.put the data table column name you want to bind in to the Text ad evalue.
                <asp:TemplateField>
                    <HeaderStyle/>
                    <ItemTemplate>
                        <asp:LinkButton ID="Number" runat="server" Text='<%# Eval("Number") %>' CommandName="Number"
                            CommandArgument='<%# Eval("Number") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

ASPX.CS
·         At the row command event capture the value set by aspx and do your work..
protected void GridViewRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName != null)
            {
                if (e.CommandName == "Number")
                {
                     string Number = e.CommandSource.ToString();
                     //Your code
                }
            }

        }
cheers!

No comments:

Post a Comment