我有一个网格视图,并且我已将 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();
}
}