我有以下SQL下拉列表中的GridView數據綁定行爲不正確ASP.NET C#
SELECT Equipment.* ,cast(EquipmentId as varchar(100)) + ' ' + EquipmentName as Name
FROM Equipment
WHERE Equipment.EquipmentCategoryID = @EquipmentCategoryID
AND (Equipment.EquipmentSubCategoryID = @EquipmentSubCategoryID
OR (@EquipmentSubCategoryID is null OR @EquipmentSubCategoryID = ''))
在SQL Server如預期其行爲。當@EquipmentCategoryId
是12並且@EquipmentSubCategoryID
是空它將返回所有我想要的值。而當@EquipmentCategoryId
是12並且@EquipmentSubCategoryID
是另一個數字時,它返回的行數也是我想要的。
在它不表現爲預期的,返回的GridView
每個dropdownlist
所有行一個GridView的ASP.NET下拉列表,即使@EquipmentSubCategoryID
有不同的數字,在SQL Server中的工作。 dropdownlist
是這樣綁定的。
protected void ServiceFormsGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField EquipmentCategoryIDHF = (HiddenField) e.Row.FindControl("EquipmentCategoryIDHF");
HiddenField EquipmentSubCategoryIDHF = (HiddenField) e.Row.FindControl("EquipmentSubCategoryIDHF");
DropDownList EquipmentDD = (DropDownList) e.Row.FindControl("EquipmentDD");
EquipmentDS.SelectParameters["EquipmentCategoryID"].DefaultValue = EquipmentCategoryIDHF.Value;
EquipmentDS.SelectParameters["EquipmentSubCategoryID"].DefaultValue = EquipmentSubCategoryIDHF.Value;
EquipmentDD.Items.Clear();
EquipmentDD.Items.Add(new ListItem());
EquipmentDD.DataBind();
}
}
我可以確認,通過該HiddenFields正在顯示正確的值,但ServiceFormsGV
GridView
方法每次在每個DropDownList
所有行。 GridView如下。
<asp:GridView ID="ServiceFormsGV" runat="server" AutoGenerateColumns="False" DataKeyNames="ServiceFormID,EquipmentCategoryID1" DataSourceID="ServiceFormsDS" OnDataBound="ServiceFormsGV_DataBound" OnRowDataBound="ServiceFormsGV_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Machine Id">
<ItemTemplate>
<asp:DropDownList ID="EquipmentDD" runat="server" AutoPostBack="True" DataSourceID="EquipmentDS" DataTextField="EquipmentName" AppendDataBoundItems="True"
DataValueField="EquipmentID" OnSelectedIndexChanged="EquipmentDD_SelectedIndexChanged">
而在我的SqlDataSource有
CancelSelectOnNullParameter="False"
這是必要的。
我已經在配置數據源中測試了該查詢,它的行爲如預期。
是否只有一個sqlDataSource用於所有下拉列表? –
是的,我只使用1個sqlDataSource作爲下拉列表。雖然我在gridview中有其他下拉菜單,似乎在工作 –
其他下拉菜單也會在gridview的每一行中加載不同的項目?你有沒有嘗試調試,看看是否正確的值傳遞給sqlDataSource? –