gpt4 book ai didi

c# - SqlDataSource UpdateCommand 参数没有改变

转载 作者:行者123 更新时间:2023-11-30 22:15:47 29 4
gpt4 key购买 nike

我有一个 FormView,下面有一个 UpdateButton 和一个 SqlDataSource。我的 updatebutton 总是用相同的值(我的 gridview 第一行的值)更新 Sil 表,我不明白为什么。

<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>

这是我的 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>

行命令:

    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>");
}
}

最佳答案

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

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

[请注意,Form View 一次只显示一条记录]。

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

将 FormView 的 AllowPaging 属性设置为 true。

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

使用分页索引后,导航到您要更改的任何记录并进行编辑,如下所示。 enter image description here

关于c# - SqlDataSource UpdateCommand 参数没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17781858/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com