2012-08-15 20 views
4

在我的detailsview上按下提交按鈕後,我有兩個數據綁定下拉列表出現此錯誤。我不在代碼中的任何地方使用Eval或XPath,但經常使用綁定。錯誤 - 數據綁定方法,如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 

拆卸點---> MOV DWORD PTR [EBP-58H],EAX

我已經好幾天沒有運氣一直在尋找這個錯誤。我已經嘗試過WEBSWAPP以及搜索了很多論壇。請幫忙。

我想使用dropdownlists讓用戶選擇值來填充數據庫。我的表單中的每個字段都是一個模板,並且是數據綁定。

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; 
      } 
     } 

    } 
} 

抱歉代碼轟炸。

<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 switch AM/PM"></asp:TextBox> 
          <asp:MaskedEditExtender ID="TextBox8_MaskedEditExtender" runat="server" 
           AcceptAMPM="True" CultureAMPMPlaceholder="" CultureCurrencySymbolPlaceholder="" 
           CultureDateFormat="" CultureDatePlaceholder="" CultureDecimalPlaceholder="" 
           CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True" 
           ErrorTooltipEnabled="True" Mask="99:99" MaskType="Time" 
           TargetControlID="TextBox8" /> 
         </InsertItemTemplate> 
         <ItemTemplate> 
          <asp:Label ID="Label10" runat="server" Text='<%# Bind("standby") %>'></asp:Label> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField AccessibleHeaderText="Standby" HeaderText="On Standby"> 
         <EditItemTemplate> 
          <asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("standby") %>' MaxLength="1" Width="21px"></asp:TextBox> 
         </EditItemTemplate> 
         <InsertItemTemplate> 
          <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource10" 
           DataTextField="&quot;Y/N&quot;" DataValueField="&quot;Y/N&quot;" SelectedValue='<%# BIND("standby") %>'> 
          </asp:DropDownList> 
         </InsertItemTemplate> 
         <ItemTemplate> 
          <asp:Label ID="Label12" runat="server" Text='<%# Bind("rsndecline") %>'></asp:Label> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField AccessibleHeaderText="Reason Decline" 
         HeaderText="Request Decline Reason" InsertVisible="False"> 
         <EditItemTemplate> 
          <asp:TextBox ID="TextBox12" style="text-transform:uppercase;" runat="server" Text='<%# Bind("rsndecline") %>' 
           Visible="False"></asp:TextBox> 
         </EditItemTemplate> 
         <InsertItemTemplate> 
          <asp:TextBox ID="TextBox12" runat="server" Text='<%# Bind("rsndecline") %>' 
           Visible="False"></asp:TextBox> 
         </InsertItemTemplate> 
         <ItemTemplate> 
          <asp:Label ID="Label13" runat="server" Text='<%# Bind("status") %>'></asp:Label> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField AccessibleHeaderText="Status" HeaderText="Request Status" InsertVisible="False"> 
         <EditItemTemplate> 
          <asp:TextBox ID="TextBox13" runat="server" Text='<%# Bind("status") %>'></asp:TextBox> 
         </EditItemTemplate> 
         <InsertItemTemplate> 
          <asp:TextBox ID="TextBox13" runat="server" Text='<%# Bind("status") %>' Visible="False"></asp:TextBox> 
         </InsertItemTemplate> 
         <ItemTemplate> 
          <asp:Label ID="Label14" runat="server" Text='<%# Bind("empremarks") %>' 
           ToolTip="Additional information pertaining to leave request"></asp:Label> 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField AccessibleHeaderText="empremarks" HeaderText="Leave Request Remarks"> 
         <EditItemTemplate> 
          <asp:TextBox ID="TextBox14" runat="server" style="text-transform:uppercase;" Text='<%# Bind("empremarks") %>' 
           TextMode="MultiLine" Height="50px" Width="449px" 
           ToolTip="Additional information pertaining to leave request"></asp:TextBox> 
          <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" 
           ControlToValidate="TextBox14" Display="Dynamic" Enabled="False" 
           ErrorMessage="*Leave Remarks Required for &quot;Other&quot; selection" 
           ForeColor="Red"></asp:RequiredFieldValidator> 
         </EditItemTemplate> 
         <InsertItemTemplate> 
          <asp:TextBox ID="TextBox14" runat="server" style="text-transform:uppercase;" Text='<%# Bind("empremarks") %>' 
           TextMode="MultiLine" Height="47px" Width="452px" 
           ToolTip="Additional information pertaining to leave request."></asp:TextBox> 
         </InsertItemTemplate> 
         <ItemTemplate> 
          <asp:Button ID="Button1" runat="server" CausesValidation="False" 
           CommandName="Edit" Height="21px" Text="Edit" Width="65px" /> 
          &nbsp; &nbsp; 
          <asp:Button ID="Button2" runat="server" CausesValidation="False" 
           CommandName="New" Height="21px" Text="New" Width="65px" /> 
          &nbsp; &nbsp; 
         </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField ShowHeader="False"> 
         <EditItemTemplate> 
          <asp:Button ID="Button1" runat="server" CausesValidation="True" 
           CommandName="Update" Text="Update" /> 
          &nbsp; 
          <asp:Button ID="Button2" runat="server" CausesValidation="False" 
           CommandName="Cancel" Text="Cancel" /> 
         </EditItemTemplate> 
         <InsertItemTemplate> 
          <asp:Button ID="Button1" runat="server" CommandName="Insert" 
           Text="Submit Request" /> 
          &nbsp; 
          <asp:Button ID="Button2" runat="server" CausesValidation="False" 
           CommandName="Cancel" Height="21px" onclick="Button2_Click1" Text="Cancel" /> 
         </InsertItemTemplate> 
         <ItemTemplate> 
          <asp:Label ID="Label5" runat="server" Text='<%# Bind("lstdate") %>'></asp:Label> 
         </ItemTemplate> 
        </asp:TemplateField> 
         </Fields> 
         </asp:DetailsView> 
        </td> 
       </tr> 
      </table> 
    <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> 

    <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: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="SqlDataSource10" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringEvent %>" 
     ProviderName="<%$ ConnectionStrings:ConnectionStringEvent.ProviderName %>" 
     SelectCommand="SELECT * FROM BWYNSELECT ORDER BY ALPH"></asp:SqlDataSource> 

      <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> 
      </asp:ToolkitScriptManager> 
    <asp:Label ID="Label27" runat="server" ></asp:Label> 
</asp:Content> 
+0

請發表您使用eval /綁定在ASP.NET/C#代碼。這將有助於弄清楚是什麼導致你的錯誤。 – Kirk 2012-08-15 15:37:46

+0

好的,我添加了它。謝謝。 – user1596472 2012-08-15 16:01:46

回答

4

有一兩件事,我注意到,我懷疑是剛剛從某種程度上覆制和粘貼缺少的是第一缺失。我不認爲它會編譯,所以我猜你的代碼中不存在。但是,這是值得檢查的。

您的錯誤可能是由TemplateField內部的SqlDataSource造成的。移動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> 

<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> 

直接改變你的SqlDataSource爲DropDownList5

你的錯誤,現在可能是由SqlDataSource1引起試圖從DetailsView內部訪問Label。這不可能真的發生,所以直接修改參數。

更改參數類型

<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> 

改變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; 
    } 
} 
+0

謝謝你,我從模板字段中刪除了所有的SqlDataSource /不在詳情視圖中。缺失的標籤是我的複製粘貼錯誤。 – user1596472 2012-08-15 17:23:14

+0

我在答案的末尾添加了一些更改,以更改如何更改SQL參數。我相信你會需要改變查詢太btw。 – Kirk 2012-08-15 17:56:27

+0

我做了更改,並得到NullReferenceException「對象引用未設置爲對象的實例」我想這可能是爲什麼你說我需要改變我的查詢,但我不知道什麼改變。 – user1596472 2012-08-15 18:07:03

相關問題