2

I have a FormView with an UpdateButton and a SqlDataSource which are below. My updatebutton always update the Sil table with the same values (the values at the first line of my gridview), I couldn't figure out why.

<asp:FormView ID="frm_Benefit" runat="server" DataKeyNames="ID" DataSourceID="sds_Benefits"
            OnItemInserted="frm_Benefit_ItemInserted" OnItemUpdated="frm_Benefit_ItemUpdated">
    <EditItemTemplate>
        <table class="formview">
            <tr>
                <td>
                    Code:
                </td>
                <td>
                    <asp:TextBox ID="CodeTextBox" runat="server" Text='<%# Bind("Code") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Name:
                </td>
                <td>
                    <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    HRName:
                </td>
                <td>
                    <asp:TextBox ID="HRNameTextBox" runat="server" Text='<%# Bind("HRName") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Description:
                </td>
                <td>
                    <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>'
                        TextMode="MultiLine" MaxLength="200" />
                </td>
            </tr>
            <tr>
                <td>
                    Associated Url:
                </td>
                <td>
                    <asp:TextBox ID="UrlTextBox" runat="server" Text='<%# Bind("Url") %>' MaxLength="100" />
                </td>
            </tr>
            <tr>
                <td>
                    BenefitType:
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="sds_BenefitTypes"
                        DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("BenefitTypeID") %>'>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    Cost:
                </td>
                <td>
                    <asp:TextBox ID="CostTextBox" runat="server" Text='<%# Bind("Cost") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Cost Type:
                </td>
                <td>
                    <asp:DropDownList ID="CostTypeDropDown" runat="server" DataSourceID="sds_CostTypes"
                        DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("CostTypeID") %>'>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    Quantity Type:
                </td>
                <td>
                    <asp:DropDownList ID="QuantityTypeDropDown" runat="server" DataSourceID="sds_QuantityTypes"
                        DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("QuantityTypeID") %>'>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    Quantity Entry On Add:
                </td>
                <td>
                    <asp:CheckBox ID="QuantityEntryOnAddCheckBox" runat="server" Checked='<%# Bind("QuantityEntryOnAdd") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Quantity Entry On Remove:
                </td>
                <td>
                    <asp:CheckBox ID="QuantityEntryOnRemoveCheckBox" runat="server" Checked='<%# Bind("QuantityEntryOnRemove") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Bonus Ratio:
                </td>
                <td>
                    <asp:TextBox ID="BonusRatioTextBox" runat="server" Text='<%# Bind("BonusRatio") %>'></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    ApplyIncomeTax:
                </td>
                <td>
                    <asp:CheckBox ID="ApplyIncomeTaxCheckBox" runat="server" Checked='<%# Bind("ApplyIncomeTax") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    ApplyStampTax:
                </td>
                <td>
                    <asp:CheckBox ID="ApplyStampTaxCheckBox" runat="server" Checked='<%# Bind("ApplyStampTax") %>' />
                </td>
            </tr>
            <tr>
                <td class="style1">
                    ApplySocialSecurityTax:
                </td>
                <td class="style1">
                    <asp:CheckBox ID="ApplySocialSecurityTaxCheckBox" runat="server" Checked='<%# Bind("ApplySocialSecurityTax") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Applicable Location:
                </td>
                <td>
                    <asp:DropDownList ID="CostTypeDropDown1" runat="server" DataSourceID="sds_Locations"
                        DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("ApplicableLocationID") %>'>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                        Text="Update" Style="display: none" />
                    &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False"
                        CommandName="Cancel" Text="Cancel" Style="display: none" />
                </td>
            </tr>
        </table>
    </EditItemTemplate>
</asp:FormView>

<asp:SqlDataSource ID="sds_Benefits" runat="server"  ConnectionString="<%$ ConnectionStrings:ConnFlexibleBenefitsDB %>"
    SelectCommand="SELECT * FROM [View_Benefits]" 
    UpdateCommand="UPDATE [Sil] SET [Code] = @Code, [HRName] = @HRName WHERE [Id] = @Id">
    <UpdateParameters>
        <asp:Parameter Name="Code" Type="String" />
        <asp:Parameter Name="HRName" Type="String" />
        <asp:Parameter Name="Id" Type="Int32" />
    </UpdateParameters>

</asp:SqlDataSource>

And this is my gridview:

<asp:GridView ID="grd_Benefits" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
    CssClass="gridTable" DataSourceID="sds_Benefits" OnRowCommand="grd_Benefits_RowCommand">
    <RowStyle CssClass="gridRow" />
    <FooterStyle CssClass="gridFooter" />
    <SelectedRowStyle CssClass="gridSelectedRow" />
    <HeaderStyle CssClass="gridHeader" />
    <AlternatingRowStyle CssClass="gridAlternatingRow" />
    <EmptyDataTemplate>
        No records found.
    </EmptyDataTemplate>
    <Columns>
        <asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
        <asp:BoundField DataField="HRName" HeaderText="HRName" SortExpression="HRName" />
        <asp:TemplateField HeaderText="Benefit Type" SortExpression="BenefitType.Name">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("BenefitTypeName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Cost" HeaderText="Cost" SortExpression="Cost" />
        <asp:TemplateField HeaderText="Cost Type">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("CostTypeName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Quantity Type">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("QuantityTypeName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lbSelect" runat="server" CausesValidation="False" CommandName="ViewRecord"
                    Text="&lt;img border='0' alt='Edit' src='../images/zoom.gif' /&gt;" CommandArgument='<%# Container.DataItemIndex %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="False">
            <HeaderTemplate>
                <asp:LinkButton ID="lbInsert" runat="server" CausesValidation="false" CommandName="NewRecord"
                    Text="&lt;img border='0' alt='New' src='../images/new.gif'  /&gt;" CommandArgument='<%# Container.DataItemIndex %>'
                    ForeColor="White"></asp:LinkButton>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="EditRecord"
                    Text="&lt;img border='0' alt='Edit' src='../images/edit.gif' /&gt;" CommandArgument='<%# Container.DataItemIndex %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

RowCommand:

    protected void grd_Benefits_RowCommand(object sender, GridViewCommandEventArgs e)
    {            
        if (e.CommandName == "ViewRecord")
        {
            grd_Benefits.SelectedIndex = int.Parse(e.CommandArgument.ToString());
            frm_Benefit.ChangeMode(FormViewMode.ReadOnly);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowFormViewView", "<script type=\"text/javascript\">showFormViewView();</script>");
        }
        else if (e.CommandName == "NewRecord")
        {
            frm_Benefit.ChangeMode(FormViewMode.Insert);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowFormViewInsert", "<script type=\"text/javascript\">showFormViewInsert();</script>");
        }
        else if (e.CommandName == "EditRecord")
        {
            grd_Benefits.SelectedIndex = int.Parse(e.CommandArgument.ToString());
            frm_Benefit.ChangeMode(FormViewMode.Edit);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowFormViewEdit", "<script type=\"text/javascript\">showFormViewEdit();</script>");
        }
    }
4

1 回答 1

3

查看您的标记并按原样处理,问题出在 FormView 上。

您尚未为 FormView 设置 AllowPaging="true"。因此,每次 FormView 显示时,它只会显示检索到的所有数据记录的第一行。

[请注意,表单视图一次仅显示一条记录]。

因此,每次您在表单视图中选择“编辑”时,您实际上只是在编辑第一条记录(这确实是 GridView 的第一行)。

将 FormView 的 AllowPaging 属性设置为 true。

<asp:FormView ID="frm_Benefit" runat="server" DataKeyNames="CustomerID"
 DataSourceID="sds_Benefits" OnItemUpdated="frm_Benefit_ItemUpdated" 
AllowPaging="true">

之后使用分页索引,导航到您想要更改的任何记录并对其进行编辑,如下所示。 在此处输入图像描述

于 2013-07-22T10:53:27.427 回答