首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SqlDataSource UpdateCommand参数未更改

SqlDataSource UpdateCommand参数未更改
EN

Stack Overflow用户
提问于 2013-07-22 15:13:45
回答 1查看 870关注 0票数 2

我有一个带有UpdateButtonSqlDataSourceFormView,如下所示。我的updatebutton总是用相同的值(网格视图第一行的值)更新Sil表,我不知道为什么。

代码语言:javascript
复制
<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>

这是我的网格视图:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
    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>");
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-22 18:53:27

看到你的标记并接受它,问题出在FormView上。

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

请注意,窗体视图一次只显示一条记录。

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

对于FormView,将AllowPaging属性设置为true。

代码语言:javascript
复制
<asp:FormView ID="frm_Benefit" runat="server" DataKeyNames="CustomerID"
 DataSourceID="sds_Benefits" OnItemUpdated="frm_Benefit_ItemUpdated" 
AllowPaging="true">

在使用分页索引之后,导航到您想要更改的任何记录并编辑它,如下所示。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17781858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档