我在asp.net detailview“PlantingHatchery”和“CatalogNames”中有两个下拉列表。第一个结果确定第二个值。第一个填充正常但不是第二个。我看了几个建议,但没有一个适合我。我哪里错了?
以下是相关代码:
<asp:DetailsView ID="dvSMasterCurrentYear" runat="server" AutoGenerateRows="False" align="center" DataKeyNames="SMasterid" DataSourceID="SqlDataSource1"
Height="50px" Width="437px" AutoGenerateInsertButton="True" AllowPaging="True" insertcommand =" Insert Into [SMasterCurrentYear}
([HatcheryCodePlant],[CatalogNo],[DataPlant],[TimePlant],[SpeciesCode],[BroodYear],[SizeCode],[MethodCode],[Length], [PoundFish],[NumberPlanted],[NumberPerLB],[HaulMort],
[License], [ActSites], [HatcheryCodeRear],[Comments],[H20Temp],[ConsOfficer],[TankTemp],[Marks],[Raceway])
Values (@HatcheryCodePlant, @CatalogNum, @DataPlant, @TimePlant, @SpeciesCode, @BroodYear, @SizeCode, @MethodCode, @Length, @PoundFish, @NumberPlanted, @NumberPerLB,
@HaulMort, @License, @ActSites, @HatcheryCodeRear, @Comments, @H20Temp, @ConsOfficer, @TankTemp, @Marks, @Raceway) " Font-Bold="True" BackColor="#FFCC99" Font-Size="Medium" DefaultMode="Insert">
<AlternatingRowStyle BackColor="#99FFCC" />
enter code here
<asp:TemplateField HeaderText="Planting Hatchery">
<ItemTemplate>
<asp:Label ID="lblPlantHatch" runat="server" Text='<%# Eval("HatcheryCodePlant")%>' Visible = "true"></asp:Label>
</ItemTemplate>
<insertItemTemplate>
<asp:DropDownList ID="ddPlantingHatchery" runat="server" DataSource='<%# GetPlantingHatchery()%>'
DataTextField="HatcheryNamePlant" DataValueField="HatcheryCodePlant" width= "150" AppendDataBoundItems="true">
<asp:ListItem Text="Select" Value="" />
</asp:DropDownList>
</insertItemTemplate>
<asp:TemplateField HeaderText="Water Body Name">
<ItemTemplate>
<asp:Label ID="lblStreamName" runat="server" Text='<%# Eval("CatalogNo")%>' Visible = "true"></asp:Label>
</ItemTemplate>
<insertItemTemplate>
<asp:DropDownList ID="ddCatalogName" runat="server" DataSource='<%# GetCatalogNames()%>'
DataTextField="StreamName" DataValueField="CatalogNo" AppendDataBoundItems="true">
<asp:ListItem Text="Select" Value="" />
</asp:DropDownList>
<asp:ObjectDataSource ID="dsWaterBody" runat="server" TypeName="StreamName"
SelectMethod="GetCatalogNames">
<SelectParameters>
<%--<asp:Parameter Name="HatcheryCodetName" Type="string" />--%>
<asp:ControlParameter Name="HatcheryCodePlant" Type="Int32" ControlID="ddPlantingHatchery" PropertyName="SelectedValue"/>
</SelectParameters>
</asp:ObjectDataSource>
</insertItemTemplate>enter code here
代码背后:
Private Sub DetailsView1_ItemInserting(sender as Object,e As DetailsViewInsertEventArgs)处理dvSMasterCurrentYear.ItemInserting
Dim ddPlantHatch As DropDownList = TryCast(view.FindControl(“ddPlantingHatchery”),DropDownList) e.Values.Add(“HatcheryCodePlant”,ddPlantHatch.SelectedValue)
Dim ddCatNum As DropDownList = TryCast(view.FindControl(“ddCatalogName”),DropDownList)e.Values(“HatcheryCodePlant”)= DirectCast(DirectCast(sender,DetailsView).FindControl(“ddPlantingHatchery”),DropDownList).SelectedValue
谢谢
答案 0 :(得分:0)
如果您的GetCatalogNames方法工作正常,您可能需要做的就是在第一个下拉列表中添加autopostback="true"
。
此外,您的codebehind方法在ItemInserting期间触发,在这种情况下,这是默认值。但你可能不需要它。
如果这不起作用,您应该设置方法以触发第一个下拉列表中的OnSelectedIndexChanged
。要做到这一点:
将此添加到您的第一个下拉列表中:
<asp:DropDownList ID="ddPlantingHatchery" runat="server" DataSource='<%# GetPlantingHatchery()%>'
DataTextField="HatcheryNamePlant" DataValueField="HatcheryCodePlant" width= "150" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="ddPlantingHatchery_indexchanged">
<asp:ListItem Text="Select" Value="" />
</asp:DropDownList>
然后将此方法添加到CodeBehind:
Public Sub ddPlantingHatchery_indexchanged(sender As Object, e As EventArgs)
Dim ddCatNum As DropDownList = TryCast(dvSMasterCurrentYear.FindControl("ddCatalogName"), DropDownList)
ddCatNum.SelectedValue = TryCast(dvSMasterCurrentYear.FindControl("ddPlantingHatchery"), DropDownList).SelectedValue
End Sub
此示例方法只是将ddCatNum的SelectedValue设置为ddPlant的SelectedValue。当然,您可以将其更改为获取您在ObjectDataSource方法中定义的值。
另一种方法:在您的第一个下拉列表中添加autopostback="true"
,并更改您的GetCatalognames()函数,以根据回发返回不同的值,如下所示:
Public Function GetCatalogNames() As DataSet
If Page.IsPostBack Then
Dim ddPlantHatch As DropDownList = TryCast(dvSMasterCurrentYear.FindControl("ddPlantingHatchery"), DropDownList)
Dim myConnection As New SqlConnection(ConnectionString)
Dim ad As New SqlDataAdapter("SELECT StreamName, CatalogNo, " _
+ "HatcheryNamePlant, HatcheryCodePlant, " _
+ "LLID FROM vwStockingWatersByHatchery " _
+ "where HatcherCodePlant = " + ddPlantHatch.SelectedValue _
+ " Order By StreamName", myConnection)
Dim dsWaterBody As New DataSet()
ad.Fill(dsWaterBody, "Catalog")
Return dsWaterBody
Else
Dim myConnection As New SqlConnection(ConnectionString)
Dim ad As New SqlDataAdapter("SELECT StreamName, CatalogNo, " _
+ "HatcheryNamePlant, HatcheryCodePlant, " _
+ "LLID FROM vwStockingWatersByHatchery " _
+ "Order By StreamName", myConnection)
Dim dsWaterBody As New DataSet()
ad.Fill(dsWaterBody, "Catalog")
Return dsWaterBody
End If
End Function
希望这有帮助