Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

GridView1_RowCommand vs GridView1_RowDeleting or GridView1_RowEditing

$
0
0

Hi,

appreciate your feedback.

When using <asp:ButtonField,  the only properties I have is CommandName. also by default, the ButtonField once clicked it fires Gridview_RowCommand  and depending on the  CommandName
it execute the code.However, it also fires the following methods after the GridView1_RowCommand is executed !!
       a. protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
       b. protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

So my question:
  how  do I know where to put the edit code and delete code. Is it inside GridView1_RowCommand
  or in a & b above?

Thanks
Bob

The code
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="contactId"
            OnRowDeleting="GridView1_RowDeleting"
            OnRowEditing="GridView1_RowEditing"
            OnRowCommand="GridView1_RowCommand"
            >
            <Columns>
<asp:ButtonField ButtonType="Button"  Text="Select" HeaderText="ButtonField"  CommandName="Select" />
<asp:ButtonField ButtonType="Button" Text="Edit" HeaderText="ButtonField" CommandName="Edit" />
<asp:ButtonField ButtonType="Button" Text="Delete" HeaderText="ButtonField" CommandName="Delete" />
<asp:TemplateField HeaderText="contactId">
    <ItemTemplate>
        <asp:Label ID="lblcontactId" runat="server" Text='<%# Bind("contactId") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtcontactId" runat="server" Text='<%# Bind("contactId") %>'></asp:TextBox>
    </EditItemTemplate>
</asp:TemplateField>
            </Columns>
        </asp:GridView>

Codebehind:
------------
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Edit")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = GridView1.Rows[index];

        // ONE approach via findcontrol to get the values from the row
        Label lblContactId = selectedRow.FindControl("lblcontactId") as Label;
    }
{
    if (e.CommandName == "Delete")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = GridView1.Rows[index];

        // ONE approach via findcontrol to get the values from the row
        Label lblContactId = selectedRow.FindControl("lblcontactId") as Label;
        // passs the id to a method to delete
    }

}

 


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>