码迷,mamicode.com
首页 > 其他好文 > 详细

FormView控件的InsertItemTemplate中3个DropDownList联动及绑定问题

时间:2014-06-11 00:01:19      阅读:453      评论:0      收藏:0      [点我收藏+]

标签: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" />
            &nbsp;<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="编辑" />
            &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
                Text="***" />
            &nbsp;<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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!