错误 - 数据绑定方法,例如 Eval()、XPath() 和 Bind()

use*_*472 5 c# asp.net data-binding drop-down-menu

在我的详细信息视图上按下提交按钮后,我收到此错误,该视图有两个数据绑定下拉列表。我不在代码中的任何地方使用 Eval 或 XPath,但经常使用 Bind。

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Source Error: 


[No relevant source lines]

Source File: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\9b1781c5\212dfe37\App_Web_newrequestform.aspx.1420f3bc.0khdma4p.0.cs    Line: 0 

Stack Trace: 


[InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.]
   System.Web.UI.Page.GetDataItem() +2802118
   ASP.authorized_leaverequest_newrequestform_aspx.__DataBinding__control69(Object sender, EventArgs e) in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\9b1781c5\212dfe37\App_Web_newrequestform.aspx.1420f3bc.0khdma4p.0.cs:0
   System.Web.UI.Control.OnDataBinding(EventArgs e) +91
   System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +14
   System.Web.UI.WebControls.ListControl.PerformSelect() +34
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66
   System.Web.UI.WebControls.ListControl.OnPreRender(EventArgs e) +29
   System.Web.UI.Control.PreRenderRecursiveInternal() +103
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496
Run Code Online (Sandbox Code Playgroud)

反汇编指向 ---> mov dword ptr [ebp-58h],eax

我已经搜索这个错误好几天了,但没有运气。我已经尝试过WEBSWAPP并搜索了很多论坛。请帮忙。

我正在尝试使用下拉列表让用户选择值来填充数据库。我的表单中的每个字段都是一个模板并且是数据绑定的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Web.Services;
using System.Web.Services.Protocols;
using AjaxControlToolkit;

namespace TimeAndAttendanceUI.Authorized.LeaveRequest
{
    public partial class newRequestForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            {
                DetailsView1.DataBind();
            }
        }

        protected void DetailsView1_DataBound(object sender, EventArgs e)
        {
            TextBox employeeID = (TextBox)(DetailsView1.FindControl("TextBox17"));
            DropDownList approverID = (DropDownList)(DetailsView1.FindControl("DropDownList4"));

            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
            {
                var CurrUser = "";

                Account.Login uusr = new Account.Login();
                CurrUser = uusr.User.Identity.Name.ToString().ToUpper();

                employeeID.Text = CurrUser.Substring(3, 3);

                string supusrid = "";

                DataHandler.DataHandler Dt = new DataHandler.DataHandler();
                supusrid = Dt.SelectRequestApproverForm(supusrid);

                approverID.SelectedValue = supusrid;
            }
        }

        protected void DropDownList6_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList lvtype = (DropDownList)(DetailsView1.FindControl("DropDownList6"));
            string selectedValue = lvtype.SelectedValue.ToString().ToUpper();
            if (selectedValue == "4 " || selectedValue == "1F")
            {
                DropDownList lvreason = (DropDownList)(DetailsView1.FindControl("DropDownList5"));
                SqlDataSource1.SelectParameters["LEAVECODE"].DefaultValue = selectedValue;

                lvreason.Visible = true;
            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            DetailsView1.DataSource = null;
            DetailsView1.DataBind();
        }

        protected void Button2_Click1(object sender, EventArgs e)
        {
            Button3.Visible = true;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            DetailsView1.Visible = true;
            DetailsView1.ChangeMode(DetailsViewMode.Insert);
            if (DetailsView1.CurrentMode.Equals(DetailsViewMode.Insert))
            {
                Button3.Visible = false;
                Button4.Visible = false;
                DetailsView2.Visible = true;
                DetailsView3.Visible = true;
            }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

抱歉用代码轰炸。

    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:Label ID="HoursLabel" runat="server"></asp:Label>
    <br />
    <table class="style1">
        <tr>
            <td class="style2" colspan="2">
                <asp:DetailsView ID="DetailsView1" runat="server" 
                DataSourceID="ObjectDataSource1" DataKeyNames="bwrequestid"
                Height="29px" Width="928px" AutoGenerateRows="False" CellPadding="4" ForeColor="#333333"
                GridLines="None" Style="margin-right: 0px; text-align: left; margin-top: 0px;" 
                    ondatabound="DetailsView1_DataBound" 
                    oniteminserted="DetailsView1_ItemInserted" 
                    onitemcommand="DetailsView1_ItemCommand" >
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
                <EditRowStyle BackColor="#999999" />
                <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" Width="180px" />
                <Fields>
                    <asp:TemplateField AccessibleHeaderText="Request ID" HeaderText="Leave Request ID" Visible="False">
                        <EditItemTemplate>
                            <asp:Label ID="Label18" runat="server" Text='<%# Bind("bwrequestid") %>'></asp:Label>
                            <asp:TextBox ID="TextBox169" runat="server" Text='<%# bind("bwrequestid") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:Label ID="Label19" runat="server" Text='<%# Bind("bwrequestid") %>'></asp:Label>
                            <asp:TextBox ID="TextBox165" runat="server" Text='<%# bind("bwrequestid") %>'></asp:TextBox>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("bwrequestid") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField AccessibleHeaderText="Employee ID" HeaderText="Employee ID" >
                        <EditItemTemplate>
                            <asp:Label ID="Label20" runat="server" Text='<%# bind("emppid") %>'></asp:Label>           
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:Label ID="Label21" runat="server" Text='<%# bind("emppid") %>'></asp:Label>
                            <asp:TextBox ID="TextBox17" runat="server" Text='<%# bind("emppid") %>' 
                               ></asp:TextBox>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# bind("emppid") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField AccessibleHeaderText="Approver ID" HeaderText="Approver ID">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("approverid") %>' Height="21px"
                                Width="68px"></asp:TextBox>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:DropDownList ID="DropDownList4" runat="server" style="text-transform:uppercase;" DataSourceID="SqlDataSourceApprover"
                                DataTextField="APPROVNAME" DataValueField="APPROVERID" 
                                SelectedValue='<%# Bind("approverid") %>'>
                            </asp:DropDownList>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("approverid") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Leave Type" SortExpression="reqleavecode">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList7" runat="server" AppendDataBoundItems="True" 
                                AutoPostBack="True" DataSourceID="SQLLEAVECODE" 
                                style="text-transform:uppercase;" DataTextField="LEAVETYPE" 
                                DataValueField="LEAVECODE" 
                                onselectedindexchanged="DropDownList6_SelectedIndexChanged" 
                                SelectedValue='<%# BIND("REQLEAVECODE") %>' ViewStateMode="Enabled" 
                                Width="165px" Height="19px">
                                <asp:ListItem></asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="SQLLEAVECODE" 
                                DataTextField="LEAVETYPE" style="text-transform:uppercase;"  DataValueField="LEAVECODE"
                                onselectedindexchanged="DropDownList6_SelectedIndexChanged" 
                                AppendDataBoundItems="True" ViewStateMode="Enabled" AutoPostBack="True" 
                                SelectedValue='<%# BIND("REQLEAVECODE") %>' Width="166px" Height="19px"><asp:ListItem></asp:ListItem>
                            </asp:DropDownList>
                            <br />
                            <asp:Label ID="Label26" runat="server" Text='<%# bind("reqleavecode") %>'></asp:Label>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label9" runat="server" Text='<%# Bind("reqleavecode") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField AccessibleHeaderText="Leave Type" HeaderText="Leave Reason" 
                        Visible="False">
                        <ItemTemplate>
                            <asp:Label ID="Label4" runat="server" Text='<%# Bind("leavetype") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("leavetype") %>' Height="21px"
                                Width="43px"></asp:TextBox>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:DropDownList ID="DropDownList5" runat="server" AppendDataBoundItems="True" 
                                AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="REASON" 
                                DataValueField="REASON" Height="19px" 
                                onselectedindexchanged="DropDownList5_SelectedIndexChanged1" 
                                SelectedValue='<%# Bind("leavetype") %>' Width="163px">
                            </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1Reason" runat="server" 
                                ControlToValidate="DropDownList5" ErrorMessage="*Leave Reason Required" 
                                ForeColor="Red"></asp:RequiredFieldValidator>
                        </InsertItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField AccessibleHeaderText="start date" HeaderText="Leave Date">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox5" runat="server" 
                                Text='<%# Bind("lstdate",  "{0:MM/dd/yyyy}") %>' Height="21px"
                                Width="72px"></asp:TextBox>
                            <asp:CalendarExtender ID="TextBox5_CalendarExtender" runat="server" Enabled="True" TargetControlID="TextBox5">
                            </asp:CalendarExtender>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:TextBox ID="TextBox5" runat="server" 
                                Text='<%# Bind("lstdate",  "{0:MM/dd/yyyy}") %>' Height="21px"
                                Width="67px"></asp:TextBox>
                            <asp:CalendarExtender ID="TextBox5_CalendarExtender" runat="server" Enabled="True"
                                TargetControlID="TextBox5">
                            </asp:CalendarExtender>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                                ControlToValidate="TextBox5" ErrorMessage="*Leave Date Required" 
                                ForeColor="Red"></asp:RequiredFieldValidator>
                            <br />
                            <asp:CompareValidator ID="CompareValidator18" runat="server" 
                                ControlToCompare="TextBox7" ControlToValidate="TextBox5" 
                                ErrorMessage="Leave date cannot be after start date" ForeColor="Red" 
                                Operator="LessThanEqual" ToolTip="Must choose start date before end date"></asp:CompareValidator>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label7" runat="server" Text='<%# Bind("lenddate") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField AccessibleHeaderText="lenddate" HeaderText="Return Date">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox7" runat="server" 
                                Text='<%# Bind("lenddate",  "{0:MM/dd/yyyy}") %>' Height="21px" Width="67px"></asp:TextBox>
                            <asp:CalendarExtender ID="TextBox7_CalendarExtender" runat="server" Enabled="True"
                                TargetControlID="TextBox7">
                            </asp:CalendarExtender>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:TextBox ID="TextBox7" runat="server" 
                                Text='<%# Bind("lenddate",  "{0:MM/dd/yyyy}") %>' Height="21px"
                                Width="67px"></asp:TextBox>
                            <asp:CalendarExtender ID="TextBox7_CalendarExtender" runat="server" Enabled="True" TargetControlID="TextBox7">
                            </asp:CalendarExtender>
                            <asp:CompareValidator ID="CompareValidator1" runat="server" 
                                ControlToCompare="TextBox5" ControlToValidate="TextBox7" 
                                ErrorMessage="Return date cannot be before start date" ForeColor="Red" 
                                Operator="GreaterThanEqual"></asp:CompareValidator>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label15" runat="server" Text='<%# Bind("lsttime") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField AccessibleHeaderText="Leave Time" HeaderText="Leave Time">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox15" runat="server" Height="21px" 
                                Text='<%# Bind("lsttime") %>' ToolTip="Type 'A' or 'P' to switch AM/PM" 
                                Width="85px"></asp:TextBox>
                            <asp:MaskedEditExtender ID="TextBox15_MaskedEditExtender" runat="server" 
                                AcceptAMPM="True" CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder="" 
                                CultureDateFormat="" CultureDatePlaceholder="" CultureDecimalPlaceholder="" 
                                CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True" 
                                ErrorTooltipEnabled="True" Mask="99:99" MaskType="Time" 
                                TargetControlID="TextBox15" />
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:TextBox ID="TextBox15" runat="server" Text='<%# Bind("lsttime") %>' Width="85px"
                                Height="22px" ToolTip="Type 'A' or 'P' to switch AM/PM"></asp:TextBox>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label8" runat="server" Text='<%# Bind("lendtime") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField AccessibleHeaderText="Return Time" HeaderText="Return Time">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("lendtime") %>' Width="85px"></asp:TextBox>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("lsttime") %>' Width="85px"
                                Height="22px" ToolTip="Type 'A' or 'P' to s

Kir*_*irk 4

我注意到的一件事是,我怀疑复制和粘贴中缺少了第一个不见了。我认为它无法编译,所以我猜测您的代码中不存在它。然而,值得检查一下。

SqlDataSource您的错误可能是由于TemplateField. 将它们移到外部DetailsView并尝试一下。

<asp:DetailsView ....
....
<InsertItemTemplate>
    <asp:DropDownList ID="DropDownList4" runat="server" style="text-transform:uppercase;" DataSourceID="SqlDataSourceApprover"
        DataTextField="APPROVNAME" DataValueField="APPROVERID" 
        SelectedValue='<%# Bind("approverid") %>'>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSourceApprover" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>"
        ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>" 
        SelectCommand="SELECT APPROVERID, APPROVNAME FROM BWAPPROVER ORDER BY APPROVERID">
    </asp:SqlDataSource>
</InsertItemTemplate>
....
<EditItemTemplate>
    <asp:DropDownList ID="DropDownList7" runat="server" AppendDataBoundItems="True" 
        AutoPostBack="True" DataSourceID="SQLLEAVECODE" 
        style="text-transform:uppercase;" DataTextField="LEAVETYPE" 
        DataValueField="LEAVECODE" 
        onselectedindexchanged="DropDownList6_SelectedIndexChanged" 
        SelectedValue='<%# BIND("REQLEAVECODE") %>' ViewStateMode="Enabled" 
        Width="165px" Height="19px">
        <asp:ListItem></asp:ListItem>
    </asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
    <asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="SQLLEAVECODE" 
        DataTextField="LEAVETYPE" style="text-transform:uppercase;"  DataValueField="LEAVECODE"
        onselectedindexchanged="DropDownList6_SelectedIndexChanged" 
        AppendDataBoundItems="True" ViewStateMode="Enabled" AutoPostBack="True" 
        SelectedValue='<%# BIND("REQLEAVECODE") %>' Width="166px" Height="19px"><asp:ListItem></asp:ListItem>
    </asp:DropDownList>
    <asp:SqlDataSource ID="SQLLEAVECODE" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>" 
        SelectCommand="SELECT LEAVETYPE, LEAVECODE FROM BWLVTYPE ORDER BY LEAVECODE">
    </asp:SqlDataSource>
    <asp:DropDownList ID="DropDownList5" runat="server" AppendDataBoundItems="True" 
        AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="REASON" 
        DataValueField="REASON" Height="19px" 
        onselectedindexchanged="DropDownList5_SelectedIndexChanged1" 
        SelectedValue='<%# Bind("leavetype") %>' Visible="False" Width="163px">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>" 
        ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>" 
        SelectCommand="SELECT REASON, LEAVECODE FROM BWLEAVRSN">
        <SelectParameters>
            <asp:ControlParameter ControlID="Label27" Name="LEAVECODE" PropertyName="Text" 
                Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
....
</asp:DetailsView>
Run Code Online (Sandbox Code Playgroud)

<asp:DetailsView ....
....
<InsertItemTemplate>
    <asp:DropDownList ID="DropDownList4" runat="server" style="text-transform:uppercase;" DataSourceID="SqlDataSourceApprover"
        DataTextField="APPROVNAME" DataValueField="APPROVERID" 
        SelectedValue='<%# Bind("approverid") %>'>
    </asp:DropDownList>
</InsertItemTemplate>
....
<EditItemTemplate>
    <asp:DropDownList ID="DropDownList7" runat="server" AppendDataBoundItems="True" 
        AutoPostBack="True" DataSourceID="SQLLEAVECODE" 
        style="text-transform:uppercase;" DataTextField="LEAVETYPE" 
        DataValueField="LEAVECODE" 
        onselectedindexchanged="DropDownList6_SelectedIndexChanged" 
        SelectedValue='<%# BIND("REQLEAVECODE") %>' ViewStateMode="Enabled" 
        Width="165px" Height="19px">
        <asp:ListItem></asp:ListItem>
    </asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
    <asp:DropDownList ID="DropDownList6" runat="server" DataSourceID="SQLLEAVECODE" 
        DataTextField="LEAVETYPE" style="text-transform:uppercase;"  DataValueField="LEAVECODE"
        onselectedindexchanged="DropDownList6_SelectedIndexChanged" 
        AppendDataBoundItems="True" ViewStateMode="Enabled" AutoPostBack="True" 
        SelectedValue='<%# BIND("REQLEAVECODE") %>' Width="166px" Height="19px"><asp:ListItem></asp:ListItem>
    </asp:DropDownList>
    <asp:DropDownList ID="DropDownList5" runat="server" AppendDataBoundItems="True" 
        AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="REASON" 
        DataValueField="REASON" Height="19px" 
        onselectedindexchanged="DropDownList5_SelectedIndexChanged1" 
        SelectedValue='<%# Bind("leavetype") %>' Visible="False" Width="163px">
    </asp:DropDownList>
....
</asp:DetailsView>

<asp:SqlDataSource ID="SqlDataSourceApprover" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>"
    ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>" 
    SelectCommand="SELECT APPROVERID, APPROVNAME FROM BWAPPROVER ORDER BY APPROVERID">
</asp:SqlDataSource>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>" 
    ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>" 
    SelectCommand="SELECT REASON, LEAVECODE FROM BWLEAVRSN">
    <SelectParameters>
        <asp:ControlParameter ControlID="Label27" Name="LEAVECODE" PropertyName="Text" 
            Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

<asp:SqlDataSource ID="SQLLEAVECODE" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>" 
    ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>" 
    SelectCommand="SELECT LEAVETYPE, LEAVECODE FROM BWLVTYPE ORDER BY LEAVECODE">
</asp:SqlDataSource>
Run Code Online (Sandbox Code Playgroud)

直接更改您的 SqlDataSource 为 DropDownList5

您的错误现在可能是由SqlDataSource1尝试LabelDetailsView. 这实际上不可能发生,因此直接更改参数。

更改您的参数类型

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>" 
    ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>" 
    SelectCommand="SELECT REASON, LEAVECODE FROM BWLEAVRSN">
    <SelectParameters>
        <asp:Parameter Name="LEAVECODE" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>
Run Code Online (Sandbox Code Playgroud)

改变Parameter

protected void DropDownList6_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList lvtype = (DropDownList)(DetailsView1.FindControl("DropDownList6"));
    string selectedValue = lvtype.SelectedValue.ToString().ToUpper();
    if (selectedValue == "4 " || selectedValue == "1F")
    {
        DropDownList lvreason = (DropDownList)(DetailsView1.FindControl("DropDownList5"));
        SqlDataSource1.SelectParameters["LEAVECODE"].DefaultValue = selectedValue;

        lvreason.Visible = true;
    }
}
Run Code Online (Sandbox Code Playgroud)