我有一个网格视图,我已经绑定了sql数据源。在页面上我有下拉,一旦下拉的选定值得到更改,我更改sql数据源的选择查询并再次将数据绑定到网格视图。在执行此操作之前,如果我更新网格视图行,它会更新,但在执行上一个过程后,我的更新无效。它没有给我任何错误。它没有采用编辑的值。
我的网格视图
<asp:GridView ID="gvTests" runat="server" AutoGenerateColumns="False" EmptyDataText="Testes are not assigned to this sample type."
CellPadding="4" CssClass="border" DataKeyNames="TestId" DataSourceID="SqlDS"
AlternatingRowStyle-BackColor="#E0ECF8" HeaderStyle-Height="20px" ForeColor="#333333"
HeaderStyle-HorizontalAlign="Left" GridLines="None" Width="100%" OnRowCommand="gvTests_RowCommand"
AllowPaging="True" AllowSorting="True" OnRowDataBound="gvTests_RowDataBound">
<AlternatingRowStyle />
<Columns>
<asp:TemplateField HeaderText="TestId" InsertVisible="False" SortExpression="TestId"
Visible="False">
<EditItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("Id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test" SortExpression="TestName" HeaderStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:TextBox CssClass="smallinput_t200" Width="100px" Text='<%# Bind("Name") %>'
ID="txtTestName" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButtonUpdate" runat="server" CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButtonCancel" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButtonEdit" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButtonDelete" runat="server" CausesValidation="False" CommandName="Del"
Text="Delete" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButtonSelect" runat="server" CausesValidation="False" CommandName="Select"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#6B89AD" ForeColor="White" HorizontalAlign="Center" />
<RowStyle CssClass="mytr" />
<SelectedRowStyle BackColor="#6B89AD" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
sql数据源就像这样
<asp:SqlDataSource ID="SqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="select * from tests"
UpdateCommand="Update [Tests] set [Name]=@TestName where [Id]=@TestId"
>
<FilterParameters>
</FilterParameters>
<UpdateParameters>
<asp:Parameter Name="Name" DbType="String" />
<asp:Parameter Name="Id" DbType="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
我的下拉是
<asp:DropDownList ID="ddlType" runat="server"
DataSourceID="SqlDS" DataTextField="Name"
DataValueField="Id" OnSelectedIndexChanged="ddlType_SelectedIndexChanged"
AutoPostBack="True" OnDataBound="ddlSampleType_DataBound">
</asp:DropDownList>
索引更改代码为
protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDS.SelectCommand = "select * from Tests t where t.Id in (1,2,3,4,5)";
SqlDS.Select(DataSourceSelectArguments.Empty);
gvTests.EditIndex = -1;
gvTests.DataBind();
}
行编辑代码为
protected void gvTests_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Update")
{
GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
SqlDSTests.UpdateParameters["Name"].DefaultValue = (row.FindControl("txtName") as TextBox).Text;
SqlDSTests.UpdateParameters["Id"].DefaultValue = (row.FindControl("lblId") as Label).Text;
SqlDSTests.Update();
SqlDS.Select(DataSourceSelectArguments.Empty);
gvTests.EditIndex = -1;
gvTests.DataBind();
}
}
答案 0 :(得分:1)
我怀疑发生了这种情况,因为Sql Data源的SelectCommand属性没有由视图状态备份(出于安全目的)。因此,当您在其中一个事件中更改select命令时,更改仅反映在该请求中。在后续的回发后(对于更新事件),将使用设计时选择命令,它可能会更改绑定到网格视图的数据,从而导致更新失败。
作为解决方案,如果在您的方案中有意义,可以尝试使用FilterExpression和FilterParameters
(请查看FilterExpression帮助中的示例)。