我有两个级联下拉列表,我试图分别绑定到两个单独的 SqlDataSource。
这些下拉列表存在于 FormView 的 EditItemTemplate 中。在 EditItemTemplate 内部存在两个填充部门和作业名的 sqldatasource 控件。DeptID 和 JobID 是这些表中的主键。这在部门和工作之间产生了“级联效应”。选择部门后,仅显示与该部门关联的工作。
这件作品工作正常。
<asp:FormView ID="frmProfile" runat="server" DataSourceID="sqlDSProfile"
DataKeyNames="EUID" style="margin-top: 0px">
<EditItemTemplate>
<asp:DropDownList ID="ddlDepartments" runat="server" Width="135px"
DataSourceID="sqlDSDepartments"
DataTextField="Department"
DataValueField="DeptID" AutoPostBack="True"
SelectedValue='<%# Bind("CurrentDeptID") %>'
AppendDataBoundItems="true" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlJobNames" runat="server" Width="185px"
DataSourceID="sqlDSJobs" DataTextField="JobName" DataValueField="JobID"
SelectedValue='<%# Bind("CurrentJobID") %>'
AppendDataBoundItems="true" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="sqlDSDepartments" runat="server"
ConnectionString="<%$ ConnectionStrings:JobsDB %>"
SelectCommand="SELECT tblDepartments.DeptID,
tblDepartments.Department
FROM tblDepartments" />
<asp:SqlDataSource ID="sqlDSJobs" runat="server"
ConnectionString="<%$ ConnectionStrings:JobsDB %>"
SelectCommand="SELECT tblJobs.JobID, tblJobs.JobName FROM tblJobs
INNER JOIN tblDeptsJobs ON tblDeptsJobs.JobID = tblJobs.JobID
WHERE tblDeptsJobs.DeptID = @DeptID" >
<SelectParameters>
<asp:ControlParameter ControlID="ddlDepartments" Name="DeptID"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
</asp:FormView>
在窗体视图之外,存在将所有信息绑定到更新语句中的 Employee 表的 SqlDataSource。我将在此 SqlDataSource 中保留所有其他信息,即使它已从上面的 FormView 中省略。
<asp:SqlDataSource ID="sqlDSProfile" runat="server"
ConnectionString="<%$ ConnectionStrings:JobsDB %>"
SelectCommand="SELECT tblEmployee.EUID,
tblEmployee.DateHired,
tblEmployee.LastName,
tblEmployee.HiredLastName,
tblEmployee.FirstName,
tblEmployee.Role,
tblEmployee.JobGrade,
tblEmployee.CurrentDeptID,
tblDepartments.Department,
tblDepartments.DeptID,
tblEmployee.CurrentJobID,
tblJobs.JobName,
tblJobs.JobID,
tblEmployee.CurrentShift,
tblEmployee.JobDate,
tblEmployee.IsDisplaced,
tblEmployee.EligibilityDate
FROM tblEmployee
LEFT OUTER JOIN tblDepartments ON tblEmployee.CurrentDeptID = tblDepartments.DeptID
EFT OUTER JOIN tblJobs ON tblEmployee.CurrentJobID = tblJobs.JobID
WHERE (tblEmployee.EUID = @EUID)"
UpdateCommand="UPDATE [tblEmployee]
SET [tblEmployee].[DateHired] = @DateHired,
[tblEmployee].[LastName] = @LastName,
[tblEmployee].[HiredLastName] = @HiredLastName,
[tblEmployee].[FirstName] = @FirstName,
[tblEmployee].[Role] = @Role,
[tblEmployee].[JobGrade] = @JobGrade,
[tblEmployee].[CurrentDeptID] = @CurrentDeptID,
[tblEmployee].[CurrentJobID] = @CurrentJobID,
[tblEmployee].[CurrentShift] = @CurrentShift,
[tblEmployee].[JobDate] = @JobDate,
[tblEmployee].[IsDisplaced] = @IsDisplaced,
[tblEmployee].[EligibilityDate] = @EligibilityDate
WHERE [tblEmployee].[EUID] = @EUID"
ProviderName="System.Data.SqlClient">
<SelectParameters>
<asp:SessionParameter Name="EUID" SessionField="sProfileEUID" DbType="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="DateHired" DbType="Date" />
<asp:Parameter Name="LastName" DbType="String" />
<asp:Parameter Name="HiredLastName" DbType="String" />
<asp:Parameter Name="FirstName" DbType="String" />
<asp:Parameter Name="Role" DbType="String" />
<asp:Parameter Name="JobGrade" DbType="Byte" />
<asp:Parameter Name="CurrentDeptID" DbType="Int32" />
<asp:Parameter Name="CurrentJobID" DbType="Int32" />
<asp:Parameter Name="CurrentShift" DbType="Int32" />
<asp:Parameter Name="JobDate" DbType="Date" />
<asp:Parameter Name="IsDisplaced" DbType="Boolean"/>
<asp:Parameter Name="EligibilityDate" DbType="Date"/>
<asp:SessionParameter Name="EUID" SessionField="sProfileEUID" DbType="String" />
</UpdateParameters>
</asp:SqlDataSource>
我不知道如何绑定的唯一部分是部门和工作。其他一切都在工作。我尝试在 DropDownList 控件中使用以下代码...
SelectedValue='<%# Bind("CurrentDeptID") %>'
SelectedValue='<%# Bind("CurrentJobID") %>'
...但是这些会导致错误。
概括
当用户单击编辑时,我需要两个下拉框中的值从主 sqlDSProfile 数据源中提取它们的选定值,但我需要它们是可更新的。我已经达到了可以更新和绑定员工所属工作的地步,但是由于下拉列表级联,当我尝试更改部门时,AutoPostBack 会破坏 sqlDSProfile - CurrentJobID 和 ddlJobs 之间的绑定。
更新
我在 select 语句中添加了tblEmployee.CurrentDeptID
和tblEmployee.CurrentJobID
,并将 Bind() 语句添加到 DropDownList 控件中。
SelectedValue='<%# Bind("CurrentDeptID") %>'
SelectedValue='<%# Bind("CurrentJobID") %>'
现在,两个 DropDownLists 都填充了从 Employee 表中提取的准确信息,显示了员工所属的部门和工作。
这两个 DropDownList 也由 FormView 内的两个 SqlDataSource 填充,为我提供了更改部门和更改工作的选项。
当我更改工作时,它会起作用并且员工的工作也会更新。
当我改变部门时,它会说DataBinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
接近完成
我从 ddlJobs 中删除了数据绑定,并在后台对其进行了编码。
Protected Sub frmProfile_ItemUpdating(sender As Object, e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles frmProfile.ItemUpdating
If frmProfile.CurrentMode = FormViewMode.Edit Then
e.NewValues("CurrentJobID") = DirectCast(DirectCast(sender, FormView).FindControl("ddlJobs"), DropDownList).SelectedValue
End If
End Sub
剩下的唯一部分是构建 ddlDepartments 更改时的代码。
伪代码...
' If Item exists in ddlJobs Then
' select item (CurrentJobID)
' else
' select index 0 and make them pick something new
' end if
很近!
再次更新
这是我为松散绑定而开发的代码。在 page_load 中,我试图从 sqlDSProfile 中提取 CurrentJobID 的内容,并检查该值是否存在于 ddlJobs 中。如果是,我想将 ddlJobs.SelectedValue = 设置为该 CurrentJobID。如果不是,我想将 selectedindex 设置为 0,这是一条消息,说“选择一个”或其他内容。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If frmProfile.CurrentMode = FormViewMode.Edit Then
' Need to determine if the CurrentJobID returned in the select statement
' exists in the ddlJobs dropdownlist. If it does, set that to the
' selectedvalue, if not set it to 0 so the user can select a new job.
Dim ddlJobs As DropDownList = frmProfile.FindControl("ddlJobs")
Dim dvProfile As DataView = sqlDSProfile.Select(DataSourceSelectArguments.Empty)
Dim drvProfile As DataRowView = dvProfile(0)
If ddlJobs.Items.FindByValue(drvProfile("CurrentJobID")) Is DBNull.Value Then
ddlJobs.SelectedIndex = 0
Else
ddlJobs.SelectedValue = drvProfile("CurrentJobID")
End If
End If
End Sub
它在我检查 dbnull.value 的行上返回一个空引用异常