gpt4 book ai didi

asp.net - 使用代码隐藏文件中的 VB.Net 编码填充 ASP.Net DropDownList

转载 作者:行者123 更新时间:2023-12-02 17:47:07 26 4
gpt4 key购买 nike

我们在 ASP.Net/VB.Net Web 表单的标记中有一个 DropDownList。

我们希望使用数据集设计器创建的数据集中的数据填充 DropDownList,但我们在代码隐藏文件中使用的编码无法使用 FindControl 找到 DropDownList ID。

您能否检查我的编码并让我知道我还需要做什么才能填充 DropDownList?

DropDownList 的标记:

<% '-- DetailsView (Grid) for details of the GridView -- %>
<% '---------------------------------------------------- %>
<asp:DetailsView
ID="DetailsView"
runat="server"
AutoGenerateRows="False"
Height="50px"
Width="207px"
DataSourceID="SqlDataSourceDetails"
DataKeyNames="ID"
OnItemCommand="DetailsViewDetails_ItemCommand"
OnDataBound="DetailsView_DataBound">

<Fields>

<asp:TemplateField HeaderText="Class:" SortExpression="ClassID">

<EditItemTemplate>
<asp:DropDownList ID="DropDownListClass" Runat="server"> </asp:DropDownList>

<asp:RequiredFieldValidator ID="RequiredFieldValidatorEditDropDownListClass" runat="server"
ControlToValidate="DropDownListClass"
ErrorMessage="Please select a class." Font-Bold="True" Font-Italic="True" ForeColor="Red"
SetFocusOnError="True" Display="Dynamic">
</asp:RequiredFieldValidator>
</EditItemTemplate>

<ItemTemplate>
<asp:Literal ID="LiteralClass" runat="server"
Text='<%# FormatAsMixedCase(Eval("ClassName").ToString())%>' />
</ItemTemplate>

<ItemStyle ForeColor="Blue" />
</asp:TemplateField>

</Fields>

代码隐藏文件中的编码:

Protected Sub DetailsView_DataBound(sender As Object, e As EventArgs)

Dim theClassesTableAdapter As New DataSetClassesTableAdapters.ClassesTableAdapter
Dim ddlTheDropDownList = DirectCast(FindControl("DropDownListClass"), DropDownList)

ddlTheDropDownList.DataSource = theClassesTableAdapter.GetDataByAllClasses
ddlTheDropDownList.DataTextField = "ClassName"
ddlTheDropDownList.DataValueField = "ClassID"
ddlTheDropDownList.SelectedValue = "ClassID"
ddlTheDropDownList.DataBind()
End Sub

DetailsView 的 DataSouce 标记:

<% '-- Datasources -- %>
<% '----------------- %>
<asp:SqlDataSource
ID="SqlDataSourceDetails"
runat="server"

ConnectionString="<%$ ConnectionStrings:Knowledge Academy %>"

DeleteCommand=
"DELETE FROM [TeacherSchedule]
WHERE [ID] = @ID"

InsertCommand=
"INSERT INTO [TeacherSchedule]
([DayOfWeek],
[Grade],
[StartTime],
[EndTime],
[ClassID])
VALUES (@DayOfWeek,
@Grade,
@StartTime,
@EndTime,
@ClassID)"

SelectCommand=
"SELECT TeacherSchedule.ID, TeacherSchedule.Grade, TeacherSchedule.StartTime, TeacherSchedule.EndTime, TeacherSchedule.TeacherID, TeacherSchedule.ClassID,
TeacherSchedule.DayOfWeek, Classes.ClassName, Teachers.Forename, Teachers.Surname
FROM TeacherSchedule INNER JOIN
Classes ON TeacherSchedule.ID = Classes.ID INNER JOIN
Teachers ON TeacherSchedule.ID = Teachers.ID
WHERE (TeacherSchedule.ID = @ID)"

UpdateCommand=
"UPDATE [TeacherSchedule]
SET [DayOfWeek] = @DayOfWeek,
[Grade] = @Grade,
[StartTime] = @StartTime,
[EndTime] = @EndTime,
[ClassID] = @ClassID
WHERE [ID] = @ID">

<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>

<InsertParameters>
<asp:Parameter Name="DayOfWeek" Type="String" />
<asp:Parameter Name="Grade" Type="String" />
<asp:Parameter Name="StartTime" Type="String" />
<asp:Parameter Name="EndTime" Type="String" />
<asp:Parameter Name="ClassID" Type="Int32" />
</InsertParameters>

<SelectParameters>
<asp:ControlParameter ControlID="GridViewSummary" Name="ID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>

<UpdateParameters>
<asp:Parameter Name="DayOfWeek" Type="String" />
<asp:Parameter Name="Grade" Type="String" />
<asp:Parameter Name="StartTime" Type="String" />
<asp:Parameter Name="EndTime" Type="String" />
<asp:Parameter Name="ClassID" Type="Int32" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>

最佳答案

尝试在 DropDownList_Init 事件处理程序中填充 DropDownList。

标记:

<asp:DropDownList ID="ddlTheDropDownList" runat="server" OnInit="ddlTheDropDownList_Init">

后面的代码应该是这样的,我更习惯 C#,但我希望你明白这一点:

Protected Sub ddlTheDropDownList_Init(sender As Object, e As EventArgs)
Dim ddl As DropDownList
ddl = sender As DropDownList
ddl.Datasource = theClassesTableAdapter.GetDataByAllClasses
ddl.DataTextField = "ClassName"
ddl.DataValueField = "ClassID"
ddl.SelectedValue = "ClassID"
ddl.DataBind()
End Sub

关于asp.net - 使用代码隐藏文件中的 VB.Net 编码填充 ASP.Net DropDownList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13710292/

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