无法通过将参数值从C#发送到asp:gridview update命令

时间:2015-12-06 04:23:49

标签: c# asp.net gridview

我想使用asp:gridview的默认编辑/更新功能来更新sql中的数据。我写的代码如下:

<asp:GridView ID="gvEditProfile" runat="server" CellPadding="4" 
              ForeColor="#333333" GridLines="None" 
              DataSourceID="DSAdminEdit" 
              DataKeyNames="pid, name, email, address, phone" 
              AutoGenerateColumns="false"
              AutoGenerateEditButton="true" AutoGenerateDeleteButton ="true" >
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                <Columns>
                    <asp:BoundField DataField="pid" HeaderText="pid"
                        SortExpression="pid"  ReadOnly="true"/>
                    <asp:BoundField DataField="name" HeaderText="Name"
                        SortExpression="name" />
                    <asp:BoundField DataField="email" HeaderText="Email"
                        SortExpression="email" />
                    <asp:BoundField DataField="address" HeaderText="Address"
                        SortExpression="address" />
                    <asp:BoundField DataField="phone" HeaderText="Phone"
                        SortExpression="phone" />
                </Columns>

            </asp:GridView>

            <asp:SqlDataSource ID="DSAdminEdit" runat="server"
                ConnectionString="<%$ ConnectionStrings:UniversityDB %>"
                SelectCommand = "SELECT DISTINCT * FROM Profile"
                UpdateCommand = "UPDATE [Profile] SET [name] = @name, [email] = @email, [address] = @address, [phone] = @phone WHERE [pid]=@pid"
                DeleteCommand = "DELETE FROM [Profile] WHERE [pid] = @pid">

                 <UpdateParameters>
                <asp:Parameter Type="String" Name="name"></asp:Parameter>
                <asp:Parameter Type="String" Name="email"></asp:Parameter>
                <asp:Parameter Type="String" Name="address"></asp:Parameter>
                <asp:Parameter Type="String" Name="phone"></asp:Parameter>
                <asp:Parameter Type="Int32" Name="pid"></asp:Parameter>
            </UpdateParameters>
                <DeleteParameters>
                     <asp:Parameter Type="Int32" Name="pid"></asp:Parameter>
                </DeleteParameters>
            </asp:SqlDataSource>

C#方面是:

int pid = -1;
protected void gvEditProfile_Updating(object sender, GridViewUpdateEventArgs e)
{
    Int32.TryParse(gvEditProfile.DataKeys[e.RowIndex]["pid"].ToString(), out pid);
}
protected void gvEditProfile_Updated(object sender, GridViewUpdatedEventArgs e)
{
    String s = e.NewValues[1].ToString();
    DSAdminEdit.UpdateParameters["name"].DefaultValue = e.NewValues[0].ToString();
    DSAdminEdit.UpdateParameters["email"].DefaultValue =  e.NewValues[1].ToString();
    DSAdminEdit.UpdateParameters["address"].DefaultValue = e.NewValues[2].ToString();
    DSAdminEdit.UpdateParameters["phone"].DefaultValue = e.NewValues[3].ToString();
    DSAdminEdit.UpdateParameters["pid"].DefaultValue = pid.ToString() ;
}

但是,这不起作用,即使我在RowUpdated方法中分配更新的值,也没有收到更新。我无法找出问题所在。任何帮助将不胜感激!

编辑:已经从Rahul请求的asp:Gridview定义中删除了更新和更新的方法。然而,问题仍然存在..

1 个答案:

答案 0 :(得分:1)

正如评论中所提到的,因为您正在使用内置的SQLDataSource控件来自动处理gridview中记录的更新,因此无需为Gridview rowupdating显式附加事件处理程序&安培; rowupdated

此外,由于DataKeyNames属性用于唯一标识表中的行,因此您应该在其中具有主键列名称,并且可能通过查看您的命名约定来pid。您可以将其更改为: -

DataSourceID="DSAdminEdit" DataKeyNames="pid"