开发者

C# : RowUpdating Method not called when Update is clicked in a gridView

开发者 https://www.devze.com 2023-03-02 18:57 出处:网络
I am having a gridView which has a Edit, Delete, Update and Cancellink buttons to perform the respected functionalities.

I am having a gridView which has a Edit, Delete, Update and Cancel link buttons to perform the respected functionalities.

Edit, Delete and Cancel commands work fine.

But the problem is that the RowUpdating Event is not fired when I click Update Link Button on the gridView.

What could be wrong?

Here is my code:

<asp:GridView ID="gvCompanies" runat="server" AutoGenerateColumns="False" AllowPaging="True" 
            onpageindexchanging="gvCompanies_PageIndexChanging" 
            onrowediting="gvCompanies_RowEditing" PageSize="20" 
            onrowcancelingedit="gvCompanies_RowCancelingEdit" 
            onrowdeleting="gvCompanies_RowDeleting" 
            onrowupdating="gvCompanies_RowUpdating" >
            <Columns>
                <asp:BoundField DataField="id" HeaderText="ID" ReadOnly="True" />
                <asp:BoundField DataField="name" ItemStyle-Width="200px" 
                    ItemStyle-HorizontalAlign="Left" HeaderText="Name" >
               <ItemStyle HorizontalAlign="Left" Width="200px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="address" HeaderText="Address">
                    <ItemStyle HorizontalAlign="Left" Width="200px" />
                </asp:BoundField>
                <asp:BoundField DataField="city" ItemStyle-Width="200px" 
                    ItemStyle-HorizontalAlign="Left" HeaderText="City" >
                 <ItemStyle HorizontalAlign="Left" Width="200px"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="state" ItemStyle-Width="200px" 
                    ItemStyle-HorizontalAlign="Left" HeaderText="State" >
                    <ItemStyle HorizontalAlign="Left" Width="200px" />
                </asp:BoundField>
                <asp:BoundField DataField="zipcode" HeaderText="Zip Code" />
                <asp:BoundField DataField="telephone" HeaderText="Telephone" />
                <asp:BoundField DataField="fax" HeaderText="Fax" />
                <asp:BoundField DataField="mobile" HeaderText="Mobile" />
                <asp:BoundField DataField="website" HeaderText="Website" />
                <asp:BoundField DataField="email" HeaderText="Email" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            </Columns>
        </asp:GridView>

In .cs file My RowUpdating Event is as below

    protected void gvCompanies_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //My Code for Update
    }

NOTE: I had checked out all the开发者_StackOverflow中文版 similar questions on Stackoverflow but none seem to work for me. So I had posted this question


Got the answer.

Thanks to those who tried to help me with this.

Had to change CausesValidation property of my commandButton to false.

Hope this helps all.


Edit

Try replacing:

<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />

With:

<asp:TemplateField>
    <ItemTemplate>
        <asp:LinkButton CommandArgument="" CommandName="Edit" runat="server" Text="Edit"/>
    </ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="False" ShowDeleteButton="true" />

ADD OnRowCommand="GridView1_RowCommand1" to your datagrid:

<asp:GridView ID="gvCompanies" runat="server" AutoGenerateColumns="False" AllowPaging="True"
        OnPageIndexChanging="gvCompanies_PageIndexChanging" OnRowEditing="gvCompanies_RowEditing"
        PageSize="20" OnRowCancelingEdit="gvCompanies_RowCancelingEdit" OnRowDeleting="gvCompanies_RowDeleting"
        OnRowUpdating="gvCompanies_RowUpdating"
        OnRowCommand="GridView1_RowCommand1">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="ID" ReadOnly="True" />
            <asp:BoundField DataField="name" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Left"
                HeaderText="Name">
                <ItemStyle HorizontalAlign="Left" Width="200px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="address" HeaderText="Address">
                <ItemStyle HorizontalAlign="Left" Width="200px" />
            </asp:BoundField>
            <asp:BoundField DataField="city" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Left"
                HeaderText="City">
                <ItemStyle HorizontalAlign="Left" Width="200px"></ItemStyle>
            </asp:BoundField>
            <asp:BoundField DataField="state" ItemStyle-Width="200px" ItemStyle-HorizontalAlign="Left"
                HeaderText="State">
                <ItemStyle HorizontalAlign="Left" Width="200px" />
            </asp:BoundField>
            <asp:BoundField DataField="zipcode" HeaderText="Zip Code" />
            <asp:BoundField DataField="telephone" HeaderText="Telephone" />
            <asp:BoundField DataField="fax" HeaderText="Fax" />
            <asp:BoundField DataField="mobile" HeaderText="Mobile" />
            <asp:BoundField DataField="website" HeaderText="Website" />
            <asp:BoundField DataField="email" HeaderText="Email" />
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
        </Columns>
</asp:GridView>

and then add below:

protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
{
        string command = e.CommandName; //use this to get the button that was pressed i.e. edit/delete
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号