我有两个级联下拉列表,我试图将每个列表绑定(bind)到两个单独的 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>
在表单 View 之外,存在 SqlDataSource,它将所有信息绑定(bind)到更新语句中的 Employee 表。我将所有其他信息保留在此 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>
我唯一不知道如何绑定(bind)的部分是部门和工作。其他一切都正常。我尝试在 DropDownList 控件中使用以下代码...
SelectedValue='<%# Bind("CurrentDeptID") %>'
SelectedValue='<%# Bind("CurrentJobID") %>'
...但是这些会导致错误。
摘要
当用户单击编辑时,我需要两个下拉框中的值从主 sqlDSProfile 数据源中提取选定的值,但我需要它们可更新。我已经达到了可以更新和绑定(bind)同事所属职位的程度,但由于下拉列表级联,当我尝试更改部门时,AutoPostBack 会破坏 sqlDSProfile - CurrentJobID 和 ddlJobs 之间的绑定(bind)。
更新
我将 tblEmployee.CurrentDeptID
和 tblEmployee.CurrentJobID
添加到 select 语句,并将 Bind() 语句添加到 DropDownList 控件。
SelectedValue='<%# Bind("CurrentDeptID") %>'
SelectedValue='<%# Bind("CurrentJobID") %>'
这两个 DropDownList 现在填充了从 Employee 表中提取的准确信息,显示了该员工所属的部门和职位。
这两个 DropDownList 也由 FormView 内的两个 SqlDataSource 填充,为我提供了更改部门和更改工作的选项。
当我更改工作时,它会起作用并且员工的工作也会更新。
当我更改部门时,它会中断说 Eval()、XPath() 和 Bind() 等 DataBinding 方法只能在数据绑定(bind)控件的上下文中使用。
即将完成
我从 ddlJobs 中删除了数据绑定(bind),并在后台对其进行了编码。
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
非常接近!
再次更新
这是我开发的松散绑定(bind)的代码。在 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 的行上返回空引用异常
最佳答案
我遇到了类似的问题,并找到了一个非常简单的解决方案(并且在 C# 中)。想象一个数据库,其中有一个表 Questions,与类别表和子类别表相关(它们也是相关且受约束的)。当尝试更新现有记录时,asp 会抛出错误。这是我根据 Lucretius 等人的上述信息找到的解决方案。
- 仅对父下拉列表进行数据绑定(bind)
- 找到一种方法,在数据源的更新事件上插入子下拉列表所选值。
如:
protected void odseditquestion_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
//dynamically assign value from ddlsubcategory to odseditquestion on updating event
//you really should not have to do this
DropDownList ddlsubcategory = (DropDownList)fveditquestion.FindControl("ddlsubcategory");
e.InputParameters["subcatid"] = (ddlsubcategory.SelectedValue);
}
它适用于我的应用程序。希望对大家有帮助,这个花了我半天时间,就是asp!!
关于asp.net - 双数据绑定(bind)级联 DropDownList 到 FormView 中的两个 SqlDataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9182030/