标签:formview sqldatasource dropdownlist联动
在InsertItemTemplate中DropDownList联动和绑定不能同时实现,需要去掉SelectedValue=‘<%# Bind("CompanyID") %>即可实现联动,另外SqlDataSource应该放在InsertItemTemplate中。
<%@ Page Title="" Language="VB" MasterPageFile="~/Manage/Site.master" AutoEventWireup="false"
CodeFile="PositionAdjust.aspx.vb" Inherits="Manage_PositionAdjust" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:FormView ID="FormView1" runat="server" DataSourceID="PositionAdjustSqlDataSource"
EnableModelValidation="True" Width="257px">
<InsertItemTemplate>
StartDate:
<asp:TextBox ID="StartDateTextBox" runat="server" Text=‘<%# Bind("StartDate") %>‘ />
<br />
EndDate:
<asp:TextBox ID="EndDateTextBox" runat="server" Text=‘<%# Bind("EndDate") %>‘ />
<br />
Remark:
<asp:TextBox ID="RemarkTextBox" runat="server" Text=‘<%# Bind("Remark") %>‘ />
<br />
Company:
<asp:DropDownList ID="CompanyDropDownList" runat="server" AutoPostBack="True" DataSourceID="CompanySqlDataSource"
DataTextField="Company" DataValueField="CompanyID">
</asp:DropDownList>
<br />
Depart:<asp:DropDownList ID="DepartmentDropDownList" runat="server" AutoPostBack="True"
EnableViewState="False" DataSourceID="DepartmentSqlDataSource" DataTextField="Depart"
DataValueField="DepartID">
</asp:DropDownList>
<br />
Position:
<asp:DropDownList ID="PositionDropDownList" runat="server" DataSourceID="PositionSqlDataSource"
DataTextField="Position" DataValueField="PositionID">
</asp:DropDownList>
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
Text="插入" OnClick="InsertButton_Click" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="取消" />
<br />
<asp:SqlDataSource ID="CompanySqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:HRM_DATAConnectionString %>"
SelectCommand="SELECT [CompanyID], [Company] FROM [tblCompany]"></asp:SqlDataSource>
<asp:SqlDataSource ID="DepartmentSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:HRM_DATAConnectionString %>"
SelectCommand="SELECT [DepartID], [Depart], [CompanyID] FROM [tblDepart] WHERE ([CompanyID] = @CompanyID)">
<SelectParameters>
<asp:ControlParameter ControlID="CompanyDropDownList" Name="CompanyID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="PositionSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:HRM_DATAConnectionString %>"
SelectCommand="SELECT [PositionID], [Position], [DepartID] FROM [tblPosition] WHERE ([DepartID] = @DepartID)">
<SelectParameters>
<asp:ControlParameter ControlID="DepartmentDropDownList" Name="DepartID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</InsertItemTemplate>
<ItemTemplate>
StartDate:
<asp:Label ID="StartDateLabel" runat="server" Text=‘<%# Bind("StartDate") %>‘ />
<br />
EndDate:
<asp:Label ID="EndDateLabel" runat="server" Text=‘<%# Bind("EndDate") %>‘ />
<br />
Remark:
<asp:Label ID="RemarkLabel" runat="server" Text=‘<%# Bind("Remark") %>‘ />
<br />
Company:
<asp:Label ID="CompanyLabel" runat="server" Text=‘<%# Bind("Company") %>‘ />
<br />
Depart:
<asp:Label ID="DepartLabel" runat="server" Text=‘<%# Bind("Depart") %>‘ />
<br />
Position:
<asp:Label ID="PositionLabel" runat="server" Text=‘<%# Bind("Position") %>‘ />
<br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="编辑" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="***" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
Text="新建" />
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="PositionAdjustSqlDataSource" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:HRM_DATAConnectionString %>" ProviderName="<%$ ConnectionStrings:HRM_DATAConnectionString.ProviderName %>"
InsertCommand="INSERT INTO tblPositionAdjust(StaffID, StartDate, EndDate, CompanyID, DepartID, PositionID, Remark) VALUES (@StaffID,@StartDate,@EndDate,@CompanyID,@DepartID,@PositionID,@Remark)">
<InsertParameters>
<asp:SessionParameter Name="StaffID" SessionField="strStaffID" Type="String" />
<asp:Parameter Name="StartDate" Type="DateTime" />
<asp:Parameter Name="EndDate" Type="DateTime" />
<asp:Parameter Name="CompanyID" />
<asp:Parameter Name="DepartID" Type="Int32" />
<asp:Parameter Name="PositionID" Type="Int32" />
<asp:Parameter Name="Remark" Type="String" />
<asp:Parameter Name="StafffID" />
</InsertParameters>
<SelectParameters>
<asp:QueryStringParameter Name="Sn" QueryStringField="Sn" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
‘后台代码中绑定
Protected Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
PositionAdjustSqlDataSource.InsertParameters("StaffID").DefaultValue = Session("strStaffID")
PositionAdjustSqlDataSource.InsertParameters("CompanyID").DefaultValue = CType(FormView1.FindControl("CompanyDropDownList"), DropDownList).SelectedValue
PositionAdjustSqlDataSource.InsertParameters("DepartID").DefaultValue = CType(FormView1.FindControl("DepartmentDropDownList"), DropDownList).SelectedValue
PositionAdjustSqlDataSource.InsertParameters("PositionID").DefaultValue = CType(FormView1.FindControl("PositionDropDownList"), DropDownList).SelectedValue
End Sub
本文出自 “chul72” 博客,请务必保留此出处http://chul72.blog.51cto.com/7924367/1423818
FormView控件的InsertItemTemplate中3个DropDownList联动及绑定问题,布布扣,bubuko.com
FormView控件的InsertItemTemplate中3个DropDownList联动及绑定问题
标签:formview sqldatasource dropdownlist联动
原文地址:http://chul72.blog.51cto.com/7924367/1423818